sponsor Vim development Vim logo Vim Book Ad

basic Tip #412: Easy menu-style switch between files with a simple map

 tip karma   Rating 76/31, Viewed by 2187 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   January 29, 2003 9:15      complexity:   basic
author:   Raghavan Subramaniyan      as of Vim:   6.0

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.

 rate this tip  Life Changing Helpful Unfulfilling 

<< How to initialize plugins | Drag and Drop file names into VIM's command line >>

Additional Notes

jh_sec@gmx.net, January 29, 2003 23:14
great tip!

i've never memorized how to switch between my files, this is great.
Anonymous, January 29, 2003 23:33
- ':ls' is shorter than ':buffers'

- there are some scripts in this script database here keeping track of open buffers via menu selection.
  e.g. mru.vim or FavMenu.vim.

bogleg@bogleg.org, January 30, 2003 0:31
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>
Suresh Govindachar, January 30, 2003 8:08
Useful tip!  Try also:

      :b <any substring from name of buffer>   and
      :sb <any substring from name of buffer>
Raghavan Subramaniyan, January 30, 2003 8:34
A useful side effect:
If you press <F5> and then hit return without entering any number, it goes to the previous file that was opened. Repeated <F5><CR> basically toggles between two files (which I use frequently enough to map to a single separate key)
Anonymous, January 30, 2003 19:42
Raghavan, try CTRL-^ for that.  See :help CTRL-^
zzapper@ntlworld.com, January 31, 2003 4:33

:wn      write current file goto next
:wp      write current file goto previous
:last   goto last
:first   goto first (:rew)
Anonymous, February 20, 2003 10:38
My version for the :ls-combined-with-:b mapping is:

nmap <F5> :ls<cr>:b!<space>
nmap <S-F5> :ls!<cr>:b!<space>

:e or :b, I don't know what's better...but 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!).
semovrs _at_ concord dot edu, May 31, 2005 13:56
This tip is awesome!
Thanks a lot!
Roumen Semov
neal.vim@gmail.com, April 8, 2006 12:12
Thanks for the mapping it. It made my life so easier with editing files in buffer. Thanks a lot again!!!!
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to vim@vim.org after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo