8

I often use a common layout on the screen - a few vertical splits, file tree (NerdTree) on the left.

When I need a new buffer, I typically want it to go into a specific split. For example, imagine the following window structure:

| file_tree | split1 | split2 |

Now I invoke the file listing command (in my case, fzf). When I find the file name, I'd like to press something like Ctrl+1 to open the file in split1, Ctrl+2 to open the file in split2, etc. How can I implement such shortcuts? Specifically, with fzf, if possible.

Otherwise, the experience is a little awkward:

  • I can always open a new vsplit, but then I need to close some of the other ones (because now there's too many), they get reshuffled, etc.
  • I can first focus a specific split, and then invoke the file listing command. That works, but I keep forgetting about this, and half of the time I keep opening the file in the tiny file_tree split.
dennis
  • 562
  • 6
  • 21
  • 5
    Are you using vim or neovim? Fzf from the box supports the following shortcuts for opening found file: **Ctrl+V** - opens a file in new vertical split, **Ctrl+X** - opens a file in new horizontal split, **Ctrl+T** opens a fine in a new tab. – dajnz Mar 07 '20 at 13:08
  • Neovim. Yes, I know all shortcuts that fzf provides, but I was looking for how to open a file in a specific split, not in a new split. – dennis Mar 08 '20 at 14:27
  • So you are searching your file with fzf? Then your active buffer will be the target as `dajnz` says. If you don't remember the name, well: http://vimcasts.org/blog/2013/01/oil-and-vinegar-split-windows-and-project-drawer/ says it all. the default file browser works better with splits than nerdtree and [vim-vinegar](https://github.com/tpope/vim-vinegar) may add some sugar. – Doktor OSwaldo Mar 09 '20 at 08:59

1 Answers1

3

Opening a file in a specific split looks not possible, unfortunately. This is because of splits nature, it is sort of dynamic viewport for a buffer that can be rearranged inside a window at any moment (with shortcuts like Ctrl+W, Shift+H/J/K/L), created and deleted. Also if you have an unsaved buffer in a split, Vim won't allow you to open a new file there without saving the existing one, and in this case, the approach you would like to use won't work too.

You can always switch quickly to a certain split with a shortcut N, Ctrl+WW where N is your split number. And you can navigate between adjacent splits using shortcut Ctrl+W, h/j/k/l.

This is how I work with splits: I usually have 6 splits open (2 rows and 3 columns), and NerdTree I open with my shortcut only I need it. To open a file in some split I navigate there with a shortcut N, Ctrl+WW, where N is split number and then I open a file using NerdTree (if I can't remember a file name), or Fzf (when I know file name).

Maybe you'll find it more convenient for you to use tabs, in this case, you can open each new file in a new tab with Ctrl+T shortcut after selecting it in Fzf. And then you can switch between tabs using gt (forward) and gT (backward) shortcuts.

dajnz
  • 1,178
  • 1
  • 8
  • 20
  • Thanks, I was suspecting this was the case. Maybe I can actually use a combination of window navigations to achieve what I need. For example, to open a file in a left vertical split - open new split to the right, swap with split to the left, move left and delete split. Etc. – dennis Mar 08 '20 at 14:31
  • 1
    Is there a way to select multiple files and open all the selected files each in it's own tab? Because currently if I select multiple files and press CTRL+t it will open all the selected files in the same tab. – 71GA Feb 03 '21 at 16:57