Buffer is a temporary space in memory used to store data. That is how VIM works when editing multiple files.
Open Multiple files
You can open multiple files with:
vim file1.txt file2.txt
Or:
vim file*.txt
Open a new buffer without display it
:badd file5.txt
Listing buffers
To list the buffers on VIM:
:buffers
The short version is :b
or :ls
.
:ls
1 "buf-ant.txt" line 1
2 #h + "buf-bed.txt" line 1
3 %a "buf-cat.txt" line 1
4 "buf-dad.txt" line 1
Press ENTER or type command to continue
Buffer Number | Indicators | File name | Line position |
---|
Indicators:
'a' - active - The buffer is displayed in a window
'h' - hidden - The buffer is not displayed
' ' - inactive - The buffer is not displayed and does not contain anything
% - Buffer current display
# - Alternate buffer (last buffer used)
+ - Modified buffer
You can open a new file using the edit command:
:e file3.txt
Buffers Navigation
To navigate through the buffers use the command:
Next buffer:
:bnext or bn
Previous buffer:
:bprevious or bp
You can use the buffer number:
:b2
To take you to the first buffer:
:bf
To take you to the last buffer:
:bl
Switching between current and previous buffer: Ctrl^
Use the setting hidden to turn on all buffers in hidden mode, so you can navigate through them without the necessity of saving changes every time you switch buffers:
:set hidden
Closing a buffer:
:bd
Closing all buffers and VIM gives you a fresh buffer:
:%bd
To run commands in all buffers, use the buffer do command. I.e do a global substitution on all buffers:
:bufdo %s/#/@/g
If you do not have the hidden option set, VIM will show an error because you are trying to switch buffer without saving the current one. You can use the separate operator to save all changes:
:bufdo %s/#/@/g | w
VIM Explorer
Navigate through files, use the explore command or E
:
:E
Top comments (0)