VI Vim Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Vi & Vim Press ESC (escape key) to get into the Vi command mode.

scape key) to get into the Vi command mode. You will need to press the RETURN key to execute a
command starting with the character colon ":", slash "/", or question mark "?". Use CTRL+c to cancel a command, the
CHEAT SHEET 101 dot "." to repeat your last command, and ":!cm d" to execute a shell command named cmd.

Insert & Replace insert mode Ex Commands vi features Search & Substitute patterns
i insert text before the cursor :q quit current open file /string search forward for string

a insert text after the cursor :q! force to quit without saving changes ?string search backward for string

I insert text at the beginning of the current :w save file n repeat the last search
line
:w file save file as file :,$s/str1/ search and substitute str1 by str2 from the
A insert text at the end of the current line str2/gc current line to the end of file. A line number
:wq! force to save file then quit can be specified before the comma “,”. The “c”
o insert text in a new line below the cursor ask for confirmation before each substitution
:x,yw file write from line x to line y into file
O insert text in a new line above the cursor :%s/str1/ replace all str1 by str2 in all the file without
:w » file append buffer to file str2/g prompting for confirmation
r replace a character at the cursor position
:e file edit another file :%s/str1/ replace str1 by str2 on the first occurrence in
R replace characters starting at the cursor str2/ each line of the file
position :e! file edit another file without saving current
changes
Undo, Delete, and Copy edit commands
:r file insert file content at the current cursor
Cursor Motion navigation position u undo last change
h or insert text before the cursor
:n edit next file in vi arguments file list CTRL+r redo last undo change
l or insert text after the cursor mA set mark A to current position. Marks can be
CTRL+G display current file name and position
the letter [a-z] and [A-Z]
k or insert text at the beginning of the current line
You can start vi with arguments, the -c option is particularly useful y'A yank (copy) from current line to mark A
to execute commands in a file from the command line. You can
j or insert text at the end of the current line separate multiple commands with a pipe “|”.
d'A delete from current line to mark A
G goto the end of file Example:
vi -c “%s/false/true/g|:wq” file.txt P put the buffer content before the cursor
nG or :n goto the line number n p put the buffer content after the cursor
Going Further with:
:map for mapping a key in command mode to a group of commands.
0 move to the beginning of the line Example: “:map de :1,$d^M” will delete all lines when using the :de x delete character at cursor position
command.
$ move to the end of the line dW delete first word after cursor position
:set to define or show your editor options.
Example: “:set number” will make vi display line numbers, and “:set all”
CTRL+f move one screen view forward will display all current vi options. d$ or D delete from cursor position to end of line
:ab to define a text abbreviation in insert mode. dd delete current line
CTRL+b move one screen view backward Example: “:ab VIM Vi Improved” will auto-complete “VIM” in insert mode
for the phrase “Vi Improved”.
J join current line with following line

https://www.shell-tips.com Essentials References for the Vi/Vim text editor


Last Updated: Mar 20, 2022

You might also like