Moving between multiple files in Vim
An opinionated piece on how to best jump across project files in Vim.
This is literally the first plugin everyone installs when they get started with Vim. Despite all the hype, I am not a fan — especially for navigating to files. This article articulates how NERDTree doesn’t really play friendly with splits and I also think having a constantly open side project drawer isn’t (arguably) the style with which Vim was intended to be used. The splits/tabs/buffers movement is way too superior to have a project drawer open all the time.
However, it is what I use to create new files using the handy ‘m’ motion and get a feel of the directory structure — a good enough plugin to have around for this purpose.
A Vim power user is incomplete without a fuzzy finder. I think its a quintessential part of any person’s workflow regardless if he/she is a Vimmer or not.
I’ve used CtrlP for quite some time. Its quick, gets the job done and integrates nicely with Airline and Git.
I’ve since moved on to using fzf in conjunction with ag and I have to say it puts CtrlP to shame. There is a large array of commands you get with fzf beyond just fuzzy file finding — searching for commits, fuzzy line finding and so much more. It’s faster and better than CtrlP.
The split movement commands are clumsy at best so I’ve remapped them to something more comfortable.
" Navigating splits
map <Leader>h <C-w>h
map <Leader>j <C-w>j
map <Leader>k <C-w>k
map <Leader>l <C-w>l
You can rebind the arrow keys for navigating splits which is an amazing idea too in my opinion. You can’t expect to be taken seriously if you don’t remap or un-map your arrow keys. Seriously.
Moving between buffers
Ah, the fun part — navigating through the buffer list. This is the list that pops up when you do a
:ls<CR> and is a list of files Vim has opened for this session.
There are traditional ways to navigate through the buffer list such as
:bprevfor the next and previous buffers. These are useless.
:b <buffer-number>for navigating to a unique buffer ID. Again, useless.
:b <part-of-the-file-name>This is useful as you can tab complete on the command bar. I can do a
:b conf<Tab><CR>and I’ll get to the
The alternate buffer
I do not see the Vim’s alternate buffer exploited as much as it should be. It is probably the most used file jump command in my arsenal. The alternate buffer is denoted by a
%a on the buffer list and indicates the last buffer you were on. It has a special command to jump to it — the
I’ve given it a mapping:
map <Leader>3 :b#<CR> “ Alternate buffer
which I admit makes no sense semantically and doesn’t follow the Vim command philosophy but it’s what works for me.
Combine the alternate buffers 2 vertical splits and you get a 4 buffer workflow in a single screen.
Goto File — the
import statements are at the top of the file. Its a nifty hack that gets me to the file from where the word under cursor was imported from.
nnoremap <C-f> mz*ggn0f’f"gf
If I want to go back, I use my alternate buffer and the ‘z’ mark to get to where I was.
That’s all for this one. Let me know if you have some other Vim productivity tips!