sponsor Vim development Vim logo Vim Book Ad

basic Tip #821: Simplest buffer explorer ever

 tip karma   Rating 22/9, Viewed by 1887 

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

created:   November 16, 2004 1:15      complexity:   basic
author:   Kartik Agaram      as of Vim:   5.7

In the footsteps of vimscript #42 and vimscript #159, here's perhaps the simplest way to select a buffer from the list of all buffers:
    nnoremap :e :ls<CR>:e<Space>

Just type ':e' and continue typing '#' and the number of a filename from the list (or a new filename).

A minor drawback is that ':' now doesn't show up immediately (vim waits some time to see if you'll complete the mapping), but I got used to this with no trouble at all.  I imagine it'll be the same for most people who type fast - you won't notice that ':' and the next character you type show up together rather than in sequence.

I always get nervous with mappings involving ':' or '/' but this one's created no problems in a couple of months.

 rate this tip  Life Changing Helpful Unfulfilling 

<< VimTip is a one-stop resource to vim. Keep it in your $VIM directory | add java import statements easily (assume using ctags) >>

Additional Notes

Anonymous, November 16, 2004 2:47
Great tip.

However I got annoyed with the flashing up of the (potentially) large list of buffers when I didn't want it, so I changed it to:

nnoremap :bl :ls<CR>:b<Space>

I'm using b instead of e and can now jump straight to the buffer if I know it  with :b(normally # although I've altered my ruler to also display the buffer number so that I can remember it) or list them first with :bl
Perci Merci, November 16, 2004 15:28
Great tip, now how about ':prev' to go back to wherever I came from?
This has been troubling me ever since I adopted vim 5 years back, that
there is no 'going back key'.
Randall.Rice@Colorado.edu, November 16, 2004 22:05
To address the need above with regard to :prev, here is my solution.

" Save the buffer number of the last exited buffer for quick snapback
au BufLeave * let MyLastBufNum = bufnr('%')

" Return to last buffer used
nnoremap <TAB> :exe "b" . MyLastBufNum<CR>

At least I believe that that is what you were wanting.
vlmarek@volny.cz, November 17, 2004 13:04
If you want to return to previous edited file (usefull for alternating between two files) Ctrl-6 might do what you want.
extra@heselhof.de, November 17, 2004 13:08
Perci Merci, Randall Rice

Do you think of this command, which is already built in?

CTRL-^    edit Nth alternate file (equivalent to ":e #N")

:h Ctrl-^
vlmarek@volny.cz, November 18, 2004 9:22
Yes :) Ctrl-6 seems to be synonimum for Ctrl-^.
Anonymous, November 23, 2004 1:08
:b# returns you to the previously edited buffer.
perci merci, December 8, 2004 12:07
C-^ only toggles to the last buffer; what I need is a list of all
(history of) buffers visited to navigate in explorer window.
Anonymous, January 7, 2005 21:20
try:

nmap ,l :ls<cr>:e#
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