Basic Vim Commands
Working definition of a "word" in Vim: Sequence of non-whitespace characters. Words are separated by a sequence of whitespace characters, where whitespace characters are newline, tab, and space.
The Vim editor is included as a default text editor in many Linux systems. "Vim" stands for "Vi IMproved".
Vim is a full screen editor and has two modes of operation:
- Command mode: Keystrokes are interpreted as commands that affect the existing data.
- Insert mode: Keystrokes are interpreted as new data, which is inserted into the existing data.
In command mode, some keystrokes cause the Vim editor to enter insert mode. In insert mode, pressing the Escape key causes the editor to exit insert mode and re-enter command mode.
In command mode, some commands begin with a colon (:). If a colon is typed, the cursor moves to the bottom of the screen. This type of command is completed by pressing the Return / Enter key.
Notes:
- Unless otherwise specified, the commands in this list work in command mode.
- Named keys are enclosed in square brackets and their names may be abbreviated. Examples: [Return] = Return key, [Esc] = Escape key, [Ctrl] = Control key.
- The symbol ^ indicates that the [Ctrl] key should be held down while the letter key is pressed.
- "Current line" = the line where the cursor is currently present.
- In a tested instance of Vim 7.4, I found that there was a small version (invoked with the
vi
command) and a huge version (invoked with the
vim
command). - When searching, huge-version Vim highlights all results. Small-version Vim does not. Some reading indicates that there are various approaches for configuring the highlighting in huge-version Vim.
# To enter and exit the Vim editor
[(huge version of Vim) In the command line, open a file for editing]
$ vim [filepath]
Example:
$ vim test.txt
[(small version of Vim) In the command line, open a file for editing]
$ vi [filepath]
Example:
$ vi test.txt
[Quit Vim, writing out the modified file to the file named in original invocation. This is a shortcut for :wq]
:x[Return]
[Quit / exit Vim]
:q[Return]
[Quit Vim without saving the latest changes]
:q![Return]
$ vim [filepath]
Example:
$ vim test.txt
[(small version of Vim) In the command line, open a file for editing]
$ vi [filepath]
Example:
$ vi test.txt
[Quit Vim, writing out the modified file to the file named in original invocation. This is a shortcut for :wq]
:x[Return]
[Quit / exit Vim]
:q[Return]
[Quit Vim without saving the latest changes]
:q![Return]
# Moving the cursor
[Move cursor down one line]
j or down-arrow
[Move cursor down to the beginning of next line]
[Return]
[Move cursor up one line]
k or up-arrow
[Move cursor left one character]
h or left-arrow
[Move cursor left one character. If cursor is at beginning of line, move up to end of previous line.]
[Backspace]
[Move cursor right one character]
l or right-arrow
[Move cursor right one character. If cursor is at end of line, move down to beginning of next line.]
[Space]
[Move cursor to start of current line]
0 [zero]
[Move cursor to end of current line]
$
[Move cursor to beginning of next word]
w
[Move cursor back to beginning of preceding word]
b
[Move cursor to first line in file]
:0[Return] or 1G
[Move cursor to line N]
:N[Return] or NG
[Move cursor to last line in file]
:$[Return] or G
j or down-arrow
[Move cursor down to the beginning of next line]
[Return]
[Move cursor up one line]
k or up-arrow
[Move cursor left one character]
h or left-arrow
[Move cursor left one character. If cursor is at beginning of line, move up to end of previous line.]
[Backspace]
[Move cursor right one character]
l or right-arrow
[Move cursor right one character. If cursor is at end of line, move down to beginning of next line.]
[Space]
[Move cursor to start of current line]
0 [zero]
[Move cursor to end of current line]
$
[Move cursor to beginning of next word]
w
[Move cursor back to beginning of preceding word]
b
[Move cursor to first line in file]
:0[Return] or 1G
[Move cursor to line N]
:N[Return] or NG
[Move cursor to last line in file]
:$[Return] or G
# Screen manipulation
[Move forward one screen]
^f
[Move backward one screen]
^b
[Move down (forward) one half screen]
^d
[Move up (back) one half screen]
^u
[Redraws the screen]
^l
[Some commands (e.g. ^g) leave a message displayed at the bottom of the screen. The command ^l will redraw the screen using the actual data, effectively removing the message.]
^f
[Move backward one screen]
^b
[Move down (forward) one half screen]
^d
[Move up (back) one half screen]
^u
[Redraws the screen]
^l
[Some commands (e.g. ^g) leave a message displayed at the bottom of the screen. The command ^l will redraw the screen using the actual data, effectively removing the message.]
# Undo and redo
[Undo the previous action]
u
[Redo the previous action]
^r
u
[Redo the previous action]
^r
# Inserting or adding text
Each of these commands puts the Vim editor into insert mode - the [Esc] key must be pressed to terminate insert mode and to put the Vim editor back into command mode.
[Insert text before cursor]
i
[Insert text at beginning of current line]
I
[Append text after cursor]
a
[Append text to end of current line]
A
[Open and put text in a new line below current line]
o
[Open and put text in a new line above current line]
O
[Read file named "filename" and insert its contents after current line]
:r filename[Return]
i
[Insert text at beginning of current line]
I
[Append text after cursor]
a
[Append text to end of current line]
A
[Open and put text in a new line below current line]
o
[Open and put text in a new line above current line]
O
[Read file named "filename" and insert its contents after current line]
:r filename[Return]
# Changing text
I think that in this group of commands:
- "r" stands for "replace".
- "c" stands for "change".
- "w" stands for "word".
[Replace single character under cursor (no [Esc] needed)]
r
[Replace characters, starting with current cursor position, until [Esc] hit]
R
[Delete all subsequent characters until end of word and move into insert mode.]
cw
[Delete all subsequent characters until end of N words and move into insert mode. Example: c5w]
cNw
[Delete all subsequent characters until end of line and move into insert mode.]
C
[Delete entire line and move into insert mode.]
cc
[Delete next N lines, starting with the current line, and move into insert mode.]
Ncc or cNc
r
[Replace characters, starting with current cursor position, until [Esc] hit]
R
[Delete all subsequent characters until end of word and move into insert mode.]
cw
[Delete all subsequent characters until end of N words and move into insert mode. Example: c5w]
cNw
[Delete all subsequent characters until end of line and move into insert mode.]
C
[Delete entire line and move into insert mode.]
cc
[Delete next N lines, starting with the current line, and move into insert mode.]
Ncc or cNc
# Deleting text
These commands do not enter insert mode.
[Delete single character under cursor]
x
[Delete N characters, starting with character under cursor, up to end of line]
Nx
[Delete the single word beginning with character under cursor]
dw
[Delete N words beginning with character under cursor. Example: d5w deletes 5 words]
dNw
[Delete the remainder of the line, starting with current cursor position]
D
[Delete entire current line]
dd
[Delete N lines, beginning with the current line. Example: 5dd deletes 5 lines.]
Ndd or dNd
x
[Delete N characters, starting with character under cursor, up to end of line]
Nx
[Delete the single word beginning with character under cursor]
dw
[Delete N words beginning with character under cursor. Example: d5w deletes 5 words]
dNw
[Delete the remainder of the line, starting with current cursor position]
D
[Delete entire current line]
dd
[Delete N lines, beginning with the current line. Example: 5dd deletes 5 lines.]
Ndd or dNd
# Copying and pasting text
[Copy / yank the current line into the buffer]
yy
[Copy / yank the next N lines, including the current line, into the buffer]
Nyy or yNy
[Put / paste the line(s) in the buffer into the text after the current line]
p
yy
[Copy / yank the next N lines, including the current line, into the buffer]
Nyy or yNy
[Put / paste the line(s) in the buffer into the text after the current line]
p
# Searching text
[Search forward for occurrence of string "foo" in text]
/foo[Return]
[Search backward for occurrence of string "foo" in text]
?foo[Return]
[Move to next occurrence of current search string]
n
[Move to next occurrence of current search string in opposite direction]
N
/foo[Return]
[Search backward for occurrence of string "foo" in text]
?foo[Return]
[Move to next occurrence of current search string]
n
[Move to next occurrence of current search string in opposite direction]
N
# Get line number information
[Displays line number of current line (at bottom of screen)]
:.=[Return]
[Displays the total number of lines (at bottom of screen)]
:=[Return]
[Displays the current line number, along with the total number of lines in the file, at the bottom of the screen]
^g
:.=[Return]
[Displays the total number of lines (at bottom of screen)]
:=[Return]
[Displays the current line number, along with the total number of lines in the file, at the bottom of the screen]
^g
# Writing files
[Write current contents to file named in original Vim call]
:w[Return]
[Write current contents to a new file named "newfile"]
:w newfile[Return]
[Write the contents of the lines numbered 12 through 35 to a new file named "smallfile"]
:12,35w smallfile[Return]
[Write current contents over a file named "filename", even if it already exists]
:w! filename[Return]
:w[Return]
[Write current contents to a new file named "newfile"]
:w newfile[Return]
[Write the contents of the lines numbered 12 through 35 to a new file named "smallfile"]
:12,35w smallfile[Return]
[Write current contents over a file named "filename", even if it already exists]
:w! filename[Return]
Additional items:
[Untested] On some UNIX platforms, the arrow keys may be used; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi (and Vim?) and should be avoided.
Future: Find out how the history feature for the undo and redo commands works (how far back it goes, when it gets erased, etc). Does it work differently in the huge and small versions?
[start of notes]
This material was originally published in the article Testing a list of Vim commands, in the section Basic Vim Commands. It has been edited for republication.
Major changes:
1) /foo became /foo[Return]
2) ?foo became ?foo[Return]
3) :.= became :.=[Return]
4) := became :.=[Return]
[end of notes]