Easy menu-style switch between files with a simple map
From Vim Tips Wiki
[edit] Obsolete tip
This tip has been merged into another tip.
See VimTip686 for the current tip.
Please do not edit this tip, and do not edit the discussion page.
If anything needs to be improved, please fix VimTip686.
Tip 412 Previous Tip • Next Tip
Created: January 29, 2003 Complexity: basic Author: Raghavan Subramaniyan Minimum version: 6.0 Karma: 76/31 Imported from: Tip#412
When there are several files opened in a vim session, it becomes difficult to keep track of the files and their respective buffer numbers.
Switching to a different file is made easier using a simple map:
:map <F5> :buffers<CR>:e #
When F5 is pressed, a numbered list of file names is printed, and the user needs to type a single number based on the "menu".
Another good thing is that the "menu" disappears after choosing the number and hitting enter. So it appears only when you need it.
You can also use add-on scripts to help you track buffers and files:
[edit] Comments
':ls' is shorter than ':buffers'
Just thought I'd throw out there what I use:
" buffer navigation: " ":e <filename>" to make a buffer with that file in it (duh) " ",s" and ",f" for back and forth on the buffer list " ",d" for a list of what's in each buffer " ",1", ",2", .. ",9", ",0" to go straight to that numbered buffer (0 = 10) " ",g" to toggle between two buffers (my most used probably) map ,s :bN<CR> map ,d :buffers<CR> map ,f :bn<CR> map ,g :e#<CR> map ,1 :1b<CR> map ,2 :2b<CR> map ,3 :3b<CR> map ,4 :4b<CR> map ,5 :5b<CR> map ,6 :6b<CR> map ,7 :7b<CR> map ,8 :8b<CR> map ,9 :9b<CR> map ,0 :10b<CR>
Try also:
:b <any substring from name of buffer> :sb <any substring from name of buffer>
Also:
" write current file goto next :wn " write current file goto previous :wp " goto last :last " goto first (:rew) :first
My version for the :ls-combined-with-:b mapping is:
nmap <F5> :ls<cr>:b!<space> nmap <S-F5> :ls!<cr>:b!<space>
I like the "!" when the current buffer is modified. (modified "abandoned" buffers get a "+" in the buffer list, that's enough).
With :b, you can press <esc> or <cr> if you want to stay in the buffer (if you just needed :ls or :ls!).
