Opening up a file from the file explorer or nerdtree always splits.

  1. anonymous

    Any way to get around this?

  2. Greg Hurrell

    Yes, bring up Command-T with the window in which you'd like to open the file already active; that way Command-T will open the file up in that window.

    Command-T will never use any "special" buffer (such as NERDTree, MiniBufExplorer etc) for opening a file, and will always split in that case, to avoid replacing the contents of the "special" buffer. (And not only that, but such buffers are usually an undesirable shape for editing anyway, like NERDTree which is tall and thin, or MiniBufExplorer which is short and wide.)

    The alternative would be for Command-T to use some other window in that case, but there is no non-arbitrary way for it to select which buffer to use (ie. if you have 2 or 3 "non-special" buffers available, which one should be used? No matter which one Command-T chooses it will seem unintuitive or wrong to at least some users at least some of the time).

  3. anonymous

    Interesting. The reason I bring this up is that I often:

    • cd ~/src/project
    • mvim .
    • leader-t to the file I want to edit

    This leaves me in an undesired split state.

  4. Greg Hurrell

    Seems that the mvim . is kind of redundant seeing as it is going to show you a list of files/directories that you are then going to ignore and use a different means of selecting a file to open (Command-T).

    The way I generally work is, at the start of a session, just :cd ~/src/project inside Vim (ie. with an empty window) and go from there.

    If you don't like this workflow and want to continue doing mvim . and find yourself with an unwanted split, about all I can think of is that you do :on to get rid of the split.

  5. Nathan Witmer

    If you're using NERDTree, you can get this to work by automatically opening an empty buffer alongside the NERDTree pane so Command-T has a regular buffer to work with. To preserve expected default behavior, this only works if you open a directory as the single argument to vim.

    Put this in after/plugins/nerd_tree_command-t.vim:

    " NERDTree and Command-T compatibility hack
    "
    " open an empty buffer and then start a real nerdtree, but only if
    " vim was opened with a single directory as the first argument.
    " The empty buffer gives command-t a buffer in which to open a
    " file, rather than having it fail to clobber the default directory browser.
    "
    " This preserves the NERDTree netrw browsing replacement, but hacks it
    " when vim is first loading.
    "
    " This script is in after/plugins since it needs to add the autocmd
    " hijacking overrides after the plugin itself has been initialized.
    
    function ReplaceNERDTreeIfDirectory()
      if argc() == 1 && isdirectory(argv(0))
        " replace the directory browser with an empty buffer
        enew
        " and open a regular nerdtree instead
        NERDTree
      end
    endfunction
    
    augroup NERDTreeHijackNetrw
      au VimEnter * call ReplaceNERDTreeIfDirectory()
    augroup END
    

Reply

This topic is now closed.