VIM Commonly Used Commands
5/11/2025

- VIM
Explanation of VIM commands
This section contains a very basic overview of commonly used commands.
Document Navigation
In normal mode, you can move around using the arrow buttons on your keyboard as well as page-up and page-down.
You can display the number of each line on the left-hand side by typing the command:
:set number
and you can hide the line numbers with:
:set nonumber
(Note: these commands can be abbreviated to :set nu and :set nonu)
You can move the cursor to line n by typing simply a colon followed by the line number:
:n
For example, :10
will jump the cursor to line 10
Inserting text
From the normal mode, in relation to the location of the cursor:
- I - (Capital i) insert at the beginning of the line
- i - insert one character to the left
- A - append to the end of the line
- a - append one character to the right
Deleting text
From the normal mode, in relation to the location of the cursor:
- x - delete the character beneath the cursor
- D - delete from the cursor to the end of the line
- dd - delete the line the cursor is on.
- dnd - delete n lines from the cursor down.
- dw - “delete a word” from the cursor to the next white space
- dnw - delete n words
- dG - delete from the cursor to the end of the file
- dnG - delete from the cursor to line n
While in normal mode, pressing J
causes the line where the cursor is, to be joined together with the line after it. In ASCII terms - the carriage return between the two lines is replaced with a space. To learn more about J go to Information about the J command, Vim Reference Manual.
Search and replace
In this section we explain how to search for (and change) strings using pattern matching with regular expressions.
1. Searching for strings
a) In normal mode, type
/string
for example, /hello
Then each time you type n the cursor will be relocated to the next instance of the string "hello".
b) To search for either of {"abc","aBc"}
one can type a single command:
/a[bB]c
where the square brackets mean "or".
c) One can search for any string in the set
{"ac", "abc", "abbc", "abbbc", ... }
using the command
/ab*c
where b*
means "zero or more b's".
d) In order to search for a word rather than a string you need to surround the string with the \< and the \> characters.
For example, if you search for the word "and" using
/and
will ALSO find words containing "and" such as "sandy". To ensure that you only find "and" add word boundaries to the search you need to use the following search:
/\<and\>
2. Search and replace/substitute
Substituting a string is very like a search with an extra parameter. For example, changing every occurrence in a file of "abc" to "ABC" simply requires the command:
:%s/abc/ABC/g
By way of explanation:
- : means "change to command mode"
- %s means "substitute on every line in the file"
- /g means "replace all occurrences in the line - not just the first."
In this section we explain how to set (and make use of) marks at positions within a file being edited with Vim.
In normal mode, type
m
letter
for example, ma
Then at any position in the document, you can go back to that line by typing
'letter
for example, 'a
.
Marking to copy text
Text is copied by storing ("yanking") it into a register - a component of Vim's memory - and then pasting it into a new location.
To copy a section of a document:
- move the cursor to the first line of the section, and mark it as explained above
- move to the final line in the section you wish to copy, and type
-
y'
letter
where letter is the letter with which you marked the beginning of that section -
move the cursor to the location where the copy is to be placed, and type
p
(for "paste").
Having marked the first line to be deleted, instead of typing y as in point 2 above, type:d'
letter
The deleted section of text has now been stored in the register and will be inserted wherever you type p
.