Working with buffers
#6
Introducing the buffer list, and commands for switching between buffers. This episode also covers the concept of ‘hidden’ buffers, and shows how to deal with them.
Working with buffers
These commands show and navigate through the buffer list:
| command | action |
|---|---|
:ls |
show the buffer list |
:bn |
open the next buffer in the current window (cycles from the end of the list to the beginning). |
:bp |
open the previous buffer in the current window (cycles from the start of the list to the end). |
CTRL-^ |
switch to the alternate file |
Dealing with hidden buffers
A buffer is marked as ‘hidden’ if it has unsaved changes, and it is not currently loaded in a window.
If you try and quit Vim while there are hidden buffers, you will raise an error: E162: No write since last change for buffer “a.txt”.
In this scenario, you can do any of the following:
| command | action |
|---|---|
:w |
save the changes to a file |
:e! |
restore the original file |
:bd! |
forcibly remove the buffer from the buffer list, discarding any changes |
:q! |
force Vim to quit, discarding changes to all modified buffers |
By default, Vim makes it difficult to create hidden buffers. To make Vim more liberal about hidden buffers, put the following in your .vimrc:
set hidden
blog comments powered by Disqus