Vim Tips Wiki
(→‎Splits: corrections)
Line 21: Line 21:
   
 
==Splits==
 
==Splits==
You can use <tt>:split</tt> and <tt>:vsplit</tt> to divide the current area into two buffers with the same file. If you supply an argument then one of the new buffers is created with the argument as a filename.<br/>
+
You can use <tt>:split</tt> and <tt>:vsplit</tt> 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 filename used for the buffer in the new window
  +
To gain more control over how the splits are created, you can combine the <tt>:vertical</tt>, <tt>:leftabove</tt> and <tt>:rightbelow</tt> commands with the <tt>:sfind</tt> and <tt>:sb</tt> commands.
+
To gain more control over how the splits are created, you can combine the <tt>:vertical</tt>, <tt>:leftabove</tt> and <tt>:rightbelow</tt> commands. Also of use are the the <tt>:sfind</tt> and <tt>:sb</tt> commands.
 
===Examples===
 
===Examples===
<code>
+
<pre>
:vertical sb 3 # Create a vertical split and show buffer number 3 in the window to the left.
+
: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.
+
: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.
+
:rightbelow sfind file.txt " Create a horizontal split and read file.txt into the buffer in the bottom window.
</code>
+
</pre>
 
===Navigating splits===
 
===Navigating splits===
Prepend the hjkl keys with ^W.
+
Use CTRL-W followed by one of the hjkl movement keys.
   
 
==Managing buffers==
 
==Managing buffers==

Revision as of 14:18, 30 August 2010

One of the ways to work with several files in Vim is the use of buffers.

Each file is automatically assigned to a buffer and you can create a new buffer with a new or existing file with :e filename

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 filename associated with the buffer.

Working with buffers

The most basic command to change between buffers is :bnext and :bprev but these are just not convenient. A far nicer command is the :b command which accepts either a number or a string.

In case of a number, the buffer with this number is displayed. In case of a string, the buffer with a matching filename is displayed.

You don't need to write the full name of the file. Vim will fetch the best match for you. And you can also press Tab to let Vim complete a filename.

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 ^O and ^I command (normal mode). This also works across buffers.

To toggle between the current and the last buffer use the ^6 (normal mode) command.

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 filename 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.

Navigating splits

Use CTRL-W followed by one of the hjkl movement keys.

Managing buffers

Buffers are *the* way to manage multiple files within a project in Vim. If you are managing multiple projects, consider opening a separate Vim for each project.

Here are the essential buffer commands:

:ls
:b #
:b <partial>

The :ls command will list the current buffers within this Vim session.

The :b # command takes a buffer number (available from the :ls command)

The :b <partial> command takes a file partial based on the current file names loaded in your Vim session. If you press Enter on a unique partial, then Vim will do the Right Thing. You can also press Tab for expansion on partial names.

This form of buffer navigation is far more efficient than using :bp and :bn

Comments

Thanks for this new tip. Now we have to wonder how best to manage this information with the following existing tips:

The FAQ contains too many words, but has some good info. The buffer switching tip is in good shape. I have tweaked this new tip but haven't thought about it yet. Perhaps some merging would be in order. BTW if you register an account you will see less ads. JohnBeckett 10:45, August 30, 2010 (UTC)