Monday, May 29, 2006

VIM's buffers

Buffer is what VIM uses for holding all information related to a file open during editing session. One can think of buffer as file. The only case when buffer is not file - is when we started editing file and not yet saved it.

VIM keeps inactive files closed. Only file(s) currently displayed is(are) open. (You can have of course more than one file displayed with multiple windows).

":ls" lists all buffers. (Conveniently, common mistype ":sl" is call to function sleep with default to sleep for 1 second.)

":b1" - switch to buffer number 1 - will reopen first file open for editing in the vim session. If file in current buffer was modified, the command would fail.

":bd" deletes current buffer. Makes vim forget that it edited the file in the buffer. Buffer numbers are not reused. Trying later to switch to that buffer number will reopen the file. In other words, ":bd" only removes the buffer from output of command ":ls".

":b#" switches to previously open buffer. Something I use a lot. Similar to ":b1" command, but instead of literal buffer number it uses vim's shortcut '#' for previous active buffer. '%' is shortcut to currently active buffer and consequently ":b%" is nop, since there is nothing to be done to activate already active buffer.

So all of my .vimrc have the line:

map <F6> :b#<CR>

what allows me to work with two files very quickly: I just need to press F6 to go to second file.

Type ":help windows.txt" in VIM for more info.

P.S. The buffers is where VIM stores info about files to open, if more than single file was specified. If vim is called that way:
$ vim *.c
you would find all of .c files vim is to open by ":ls" command. Usual :next/:prev are used to browse thru all the files.

3 comments:

Ihar Filipau said...

Test of comments

Anonymous said...

Thanks for info.
I just mapped my F6 key to also switch between files.
I am using gVim for Python / html / Django files
Liam

Kedar Mhaswade said...

Very nice, thank you.