On Unix operating systems, there are numerous ways to edit files. One of the most common methods is to edit files using the screen-oriented text editor vi (visual editor). You can use the vi editor to edit an existing file or to create a new file from scratch. This editor also allows you to read text files. This article, along with a cheat sheet, will provide you with some vi commands to use the vi editor effectively.
Download Vi Cheat Sheet
What is Vi in Linux?
Vi is a text editor that lets you create, edit, and manipulate text files. It has a number of editing features, including the ability to navigate through a file, make changes, copy and paste text, search and replace, and more.
The Vi text editor has the following two modes:
- Command Mode: When you launch Vi, this is the default mode. When in command mode, you can move around the text file, copy or delete text, look for patterns, and issue a number of commands. In this mode, you are unable to edit the text directly.
- Insert Mode: Direct text editing is possible while in insert mode. You can type and insert new text, modify existing text, and edit with all of the standard keyboard keys. To enter insert mode, first exit command mode by pressing the “I” key.
Vi Commands List
In this section, I aim to provide a comprehensive list of essential Vi commands, whether you’re a beginner just getting started with Vi or an experienced user looking to expand your knowledge.
By becoming acquainted with these commands, you will be able to perform a variety of editing tasks, including moving within a file, making changes, searching for patterns, and more. From basic navigation to advanced editing techniques, the Vi commands listed below will provide you with the tools you need to increase your productivity and effectiveness when working with text files.
Launching Vi Editor
Here, you will get to know how to open a file in the vi text editor. The text editor will open in command mode, and to edit, you have to enable insert mode.
Commands
Description
vi
Opens the editor in default mode
vi <filename.txt>
Opens the specified file or creates a new file in the vi editor
Switch to Insert Mode
In this section, I have included commands that will help you switch from Command mode to Insert mode and start editing texts.
Commands
Description
i
Changes to insert mode
I
Inserts text at the start of the current line
a
Appends after the cursor’s current position
A
Appends to the end of the line
o
Adds a new line beneath the current one
O
Opens a new line above the current line
Jump Lines Commands
Here you will find commands to jump from one line to another.
Commands
Description
G
This indicates you to the file’s final line
I
Takes you to your last position in the file
Delete Commands
In this section, I have listed all the commands which you can use to delete a character, line, or word.
Commands
Description
x
Removes one character
X
Removes the character preceding the cursor
r
Replaces character
C
Deletes the contents of the line after the cursor and replaces it with new text. To stop insertion, press the ESC key
dd
Removes an entire line
Xdd
Deletes X number of lines
D
Deletes the contents of the line following the cursor
dw
Deletes word
Xdw
Deletes X number of words
dG
Deletes from the current line to the end of the file
d^
Deletes from the current cursor position to the line’s beginning
d$
Deletes from the current cursor position to the end of the line
Undo and Repeat Commands
Here, I have listed all those commands that will help you to Undo or repeat a change in your text file.
Commands
Description
u
Undo the most recent change
U
Reverts all changes made to the entire line
.
Repeats the last command
Copy and Paste Commands
Using these listed commands, you can copy and paste any text in your file.
Commands
Description
yy
Copies (yanks) a line of text
yw
Copies the current word from the character with the lowercase w cursor until the word is finished
p
Paste a line of yanked text after the current line
P
Paste before the current line
Joining Lines Commands
Here you will find commands that you will use to join lines, repeat line, and swap lines.
Commands
Description
J
Joins two lines
yyp
Repeats the current line
ddp
Swaps two lines
Navigating Within a File Commands
In this section you will find commands that will help you navigate with in a file without using a mouse.
Commands
Description
k
Moves up one line
j
Moves down one line
h
Moves one character to the left
l
Moves right one character
G
Navigates to the last line of a file
XG
Navigates to line X in a file
gg
Navigates to the first line of a file
Saving and Closing File Commands
You can follow this list to know how to save and close files using commands in vi editor.
Commands
Description
wq or ZZ
Saves your work and exits vi editor
w
Continues editing after saving
w!
Saves (and writes to a non-writable file)
q!
Quits vi without saving the changes
Searching String Commands
Suppose you want to search a string. Here are some commands to help you.
Commands
Description
/string
Forwards lookup for a given string
?string
Backwards lookup for a given string
/^string
Forwards search string at the start of a line
/string$
Forwards search string at line’s end
n
Proceeds to the next occurrence of the searched string
/\<he\>
Looks for the word he (rather than there, here, and so on)
/pl[abc]ce
Looks up the terms place, plbce, and plcce
Text Buffers Commands
In this section, I have listed some commands that will help you to efficiently work with multiple sections of text, copy and paste content, and organize your editing workflow effectively
Commands
Description
“add
Deletes the current line and insert text into buffer a
“ap
Paste the line from buffer a
Change Commands
To substitute or remove any character or line from a text, you can use the commands listed below.
Commands
Description
cc
Removes the line’s contents, returning you to insert mode
cw
Changes the current word with new text, starting with the character under cursor
R
Overwrites characters beginning with the cursor
s
Substitutes one character under the cursor and continue to insert
S
Substitutes the entire line and begin to insert at the beginning of the line
Set Commands
You can alter the appearance of your Vi screen by using the following list.
Commands
Description
set ic
Sets case insensitivity while searching
set ai
Sets auto indention
set noai
Unsets auto indention
set nu
Shows lines with line numbers on the left side of the screen
set sw
Sets the width of a software tabstop
set ws
If wrapscan is enabled and the word is not found at the end of the file, it will look for it at the beginning
set wm
If this option is set to a value greater than zero, the editor will “word wrap” automatically
set ro
Sets the file type to “read only”
set term
Prints the terminal type
set bf
Discards control characters from input
Scrolling Commands
You can also scroll your screen using commands or keyboard keys. In this section, I have demonstrated the keys that you can use.
Commands
Description
CTRL+D
Moves the screen down by half a page
CTRL+F
Scrolls the screen down by a full page
CTRL+U
Scrolls the screen up by half a page
CTRL+B
Scrolls the screen up by a full page
CTRL+E
Scrolls the screen up by one line
CTRL+Y
Scrolls the screen down by one line
CTRL+I
Redraws the screen
Other Vi Shortcuts
Here I have listed more commonly used shortcuts for vi that will enable you to edit files and learn vi in a short amount of time.
Commands
Description
b
Returns to the word’s beginning
e
Goes to the end of the word
Xyy
Yanks X number of lines
num
Shows the line number of the current line
Esc
Switches to Command mode /Terminate insert mode
xp
Switches two characters
cw
Changes word
~
Changes the case of an individual character
Conclusion
In this article, I presented a comprehensive Vi cheat sheet that can be used as a handy reference guide for both novice and experienced Vi users. You can navigate, edit, and manipulate text files more efficiently and precisely if you are familiar with the commands and shortcuts provided in this cheat sheet. The best way to learn Vi is to create a new file and test it. Please feel free to use the list of common keyboard shortcuts provided in this article and the vi cheat sheet to assist you in learning Vi’s extensive vocabulary.
FUNDAMENTALS A Complete Guide for Beginners