DEV Community

Guilherme
Guilherme

Posted on

VIM - Buffers

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
Enter fullscreen mode Exit fullscreen mode

Or:

vim file*.txt
Enter fullscreen mode Exit fullscreen mode

Open a new buffer without display it

:badd file5.txt
Enter fullscreen mode Exit fullscreen mode

Listing buffers

To list the buffers on VIM:

:buffers
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

Buffers Navigation

To navigate through the buffers use the command:

Next buffer:

:bnext or bn
Enter fullscreen mode Exit fullscreen mode

Previous buffer:

:bprevious or bp
Enter fullscreen mode Exit fullscreen mode

You can use the buffer number:

:b2
Enter fullscreen mode Exit fullscreen mode

To take you to the first buffer:

:bf
Enter fullscreen mode Exit fullscreen mode

To take you to the last buffer:

:bl
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Closing a buffer:

:bd
Enter fullscreen mode Exit fullscreen mode

Closing all buffers and VIM gives you a fresh buffer:

:%bd
Enter fullscreen mode Exit fullscreen mode

To run commands in all buffers, use the buffer do command. I.e do a global substitution on all buffers:

:bufdo %s/#/@/g
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

VIM Explorer

Navigate through files, use the explore command or E:

:E
Enter fullscreen mode Exit fullscreen mode

Top comments (0)