vimvim-fzf

Open file in a specific split in vim (with fzf)


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:


Solution

  • 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.