Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

vim-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:

  • 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.
like image 416
dennis Avatar asked Feb 28 '20 09:02

dennis


1 Answers

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.

like image 94
dajnz Avatar answered Nov 20 '22 05:11

dajnz