(Change <tt> to <code>, perhaps also minor tweak.) |
(→Working with buffers:
Added info about :bn, :bp and :buffers.) Tag: Visual edit |
||
(One intermediate revision by one other user not shown) | |||
Line 13: | Line 13: | ||
One of the ways to work with several files in Vim is the use of buffers. |
One of the ways to work with several files in Vim is the use of buffers. |
||
− | The |
+ | The <code>:e ''filename''</code> command can edit an existing file or a new file. When a file is edited, the file is read into a new buffer that holds a temporary copy of the file (or an empty buffer for a new file). Editing makes changes to the buffer. To save a file, the original file is replaced by writing the buffer to disk. |
To list all buffers use the :ls command. Each buffer is assigned a number that is displayed in the first column. |
To list all buffers use the :ls command. Each buffer is assigned a number that is displayed in the first column. |
||
Line 21: | Line 21: | ||
==Overview== |
==Overview== |
||
[[File:tabs-windows-buffers.png|frame|left]] |
[[File:tabs-windows-buffers.png|frame|left]] |
||
− | The default label for the current tab shows |
+ | The default label for the current tab shows <code>4 example.txt</code> because there are four windows in the tab, and the cursor is in window 3 which shows the buffer for file <code>example.txt</code>. |
{{clear}} |
{{clear}} |
||
The image shows a session of Vim created with these steps: |
The image shows a session of Vim created with these steps: |
||
Line 43: | Line 43: | ||
==Working with buffers== |
==Working with buffers== |
||
− | The commands <code>:bnext</code> and <code>:bprev</code> allow changing between buffers, but are not convenient. |
+ | The commands <code>:bnext</code> and <code>:bprev</code> allow changing between buffers, but are not convenient. Fortunately they can be shortened to <code>:bn</code> and <code>:bp</code> respectively. |
+ | The <code>:b</code> command can also accept either a number or a string argument specifying which buffer to display. The buffers are numbered as they are created and can be viewed in a list using <code>:buffers</code>. Supplying a number after <code>:b</code> brings up that buffer e.g. <code>:b2</code>. Or if you have already typed <code>:buffers</code>, you can just type the appropriate number after seeing the list. |
||
⚫ | |||
+ | |||
⚫ | |||
==Jumping around== |
==Jumping around== |
Revision as of 12:17, 26 August 2014
One of the ways to work with several files in Vim is the use of buffers.
The :e filename
command can edit an existing file or a new file. When a file is edited, the file is read into a new buffer that holds a temporary copy of the file (or an empty buffer for a new file). Editing makes changes to the buffer. To save a file, the original file is replaced by writing the buffer to disk.
To list all buffers use the :ls command. Each buffer is assigned a number that is displayed in the first column.
The second column describes the state of the buffer. The different states are explained at :help :ls. The third column is the file name associated with the buffer.
Overview
The default label for the current tab shows 4 example.txt
because there are four windows in the tab, and the cursor is in window 3 which shows the buffer for file example.txt
.
The image shows a session of Vim created with these steps:
gvim scan.py |
start GUI Vim editing file scan.py in first tab |
:tabe example.txt |
edit file example.txt in a new tab |
:vimgrep /0$/ example.txt |
search for lines ending with 0 in file example.txt |
:cwin |
open quickfix window (window 4) |
Ctrl-W w |
move cursor to other window |
:vsp |
split window vertically (windows 2 and 3 showing example.txt) |
:help quickfix |
open help (window 1) |
(commands not shown) | scroll and size each window |
Working with buffers
The commands :bnext
and :bprev
allow changing between buffers, but are not convenient. Fortunately they can be shortened to :bn
and :bp
respectively.
The :b
command can also accept either a number or a string argument specifying which buffer to display. The buffers are numbered as they are created and can be viewed in a list using :buffers
. Supplying a number after :b
brings up that buffer e.g. :b2
. Or if you have already typed :buffers
, you can just type the appropriate number after seeing the list.
If a string is supplied, the buffer with a matching file name is displayed. You do not need to write the full name of the file as the best match will be used. After typing the first couple of characters, you can press Tab for completion of the name.
Jumping around
Vim keeps a history of all the jumps you make in your buffers. You can go to any place you jumped from with the Ctrl-O and Ctrl-I command (normal mode). This also works across buffers.
To toggle between the current and the last buffer use the Ctrl-^ (normal mode) command (on most keyboards, hold down Ctrl and press the 6 key on the main keyboard).
Splits
You can use :split
and :vsplit
to divide the current area into two windows with the same buffer. If you supply an argument then one of the new windows is created with the argument as a file name used for the buffer in the new window
To gain more control over how the splits are created, you can combine the :vertical
, :leftabove
and :rightbelow
commands. Also of use are the the :sfind
and :sb
commands.
Examples
:vertical sb 3
- Create a vertical split and show buffer number 3 in the window to the left.
:vertical rightbelow sfind file.txt
- Create a vertical split and read file.txt into the buffer in the right window.
:rightbelow sfind file.txt
- Create a horizontal split and read file.txt into the buffer in the bottom window.
Use Ctrl-W followed by one of the hjkl
movement keys.
Managing buffers
Buffers are a convenient way to manage multiple files within a project in Vim. If you are managing multiple projects, consider opening a separate Vim for each project. This way each Vim instance contains only related buffers in the buffer list. This is an advantage over other methods for managing multiple files; such as a combination of buffers, windows, and tabs.
Here are the essential buffer commands:
:ls |
List the current buffers (including their numbers). |
:b <number> |
Display the buffer with the given number. |
:b <partial> |
Display the first buffer matching the partial name (or press Tab for name completion). |
There are many plugins for managing buffers, but it is often easier to just use the above commands. Enter :ls
to list the buffers, then (while the list is still displayed), enter a command like :b12
to display buffer 12 (no space is needed).