SamJansen's Introduction To Vim
This is of course not supposed to be a comprehensive guide, just enough information to be reasonably proficient with Vim.
Everything in Vim can be configured on a user basis by editing the .vimrc file in the users home directory.
The Basics
- Vim and Vi distinguish themselves by being text editors with modes. When startded up, Vim will be in 'normal' mode. Starting Vim is just like any other text editor
- vim myfile.c
To begin typing, simply hit 'i' on your keyboard. Alternatively, you can probably press your Insert key. Pressing one of these keys puts you in insert mode. This mode is simply for entering text, hit the Escape key once you are finished typing.
- After leaving insert mode, Vim returns back to normal mode. The key to normal mode is the colon key, ':'. Pressing the colon key allows commands to be typed into Vim. Some simple commands follow
- :w[rite? Write, or save the current file.
:q[uit? Quit Vim (will not exit if there are unsaved changes)
:e[dit? <file> Edit/load a new file.
:help <topic> Open up a help window displaying topic.
The above commands are usually just abbreviated ':w' and such, there isn't really any point in typing ':write'.
When navigating the help, use Ctrl-] to follow a hyperlink and Ctrl-T to go back.
Hyperlinks are denoted by the surrounding 'bars' e.g. |bars|
If the mouse is enabled (via ":set mouse=a") you may also double-click the left mouse button on a tag between |bars|.
The commands can also be forced by appending a '!'. So ':q!' will force-quit, leaving Vim and ignoring the possibility of changes in the current file. Some can also be combined; a common combination is ':wq', quiting and saving the file at the same time.
Selecting Text
All the above will work in either Vim or Vi and is enough to get started. The next logical step in learning Vim, though, are selections: we want to be able to cut and paste!
- When in 'normal' mode, pressing 'v' puts you in 'visual' mode. Now navigating by the cursor keys allows you to select text. Once some text is selected, you can press various keys to perform some operation on this selection. Pressing ':' still works, many operations can be performed this way. Some basic keys to press (without the colon) are given here
y 'Yank', or copy the current selection.
c Cut the current selection.
d Delete selection.
Indent the current selection.
< De-indent the current selection.
While in normal mode, pressing 'p' will paste.
Search and replace
In normal mode,
:s/(search regex)/(replace string)/
to replace text on the current line, or
:%s/regex/replace/
for the whole buffer.
Navigating Multiple Files
- It is possible to open multiple files in the command line with something like so
- vim *.c sam.h
- And also possible to open a lot of files via the ':edit' command. Navigating through these files is done by a few simple commands
- :bn Short for 'buffer next', switches to the next buffer.
:bp Short for 'buffer previous', switches to the previous buffer.
:b<number> Switches to buffer no. <number>.
:b <name> Switches to the buffer named <name>. Note that tab completion can be used here.
Windows
Also, it can be quite useful to have multiple 'windows' open in vim to edit/view multiple files. Vim can split both vertically and horizontally. Accessing the windows functions requires using the 'ctrl-w' combination. Some of the combinations below work if you keep holding ctrl down, for example '<ctrl-w><ctrl-w>' also navigates to the next window.
- In normal mode
- <ctrl-w>n Create a new window with a new buffer, splitting the screen with a horizontal line.
<ctrl-w>s Create a new window of the current buffer, splitting the screen with a horizontal line.
<ctrl-w>v Create a new window of the current buffer, splitting the screen with a vertical line.
<ctrl-w>c Close the current window.
<ctrl-w>o Close all the other windows (all windows but the currently selected one).
<ctrl-w>w Navigate to the next window.
More Stuff
There are many, many ways to be more productive in Vim. This is just a short and very basic introduction. Listed here are a few commands that may come in handy.
- Look at the help for the 's', or 'substitute' command. This command works similar to 'sed', except it can just work on your selection/range/motion.
- :<number> goes to line no. <number>, :$ is the end of file. Alternatively, gg is the start of the file and G is the end of the file.
- To read read from stdin invoke Vim like so: 'vim -'. This is very useful to pipe stuff into Vim for later editing.
- Best of all are the scripts and useful tips at http://vim.sf.net.
- See also: ViNotes
- LinuxGazette has a series of Vim tutorials
CategoryHowto