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.
Opens the editor in default mode
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.
Changes to insert mode
Inserts text at the start of the current line
Appends after the cursor’s current position
Appends to the end of the line
Adds a new line beneath the current one
Opens a new line above the current line
Jump Lines Commands
Here you will find commands to jump from one line to another.
This indicates you to the file’s final line
Takes you to your last position in the file
In this section, I have listed all the commands which you can use to delete a character, line, or word.
Removes one character
Removes the character preceding the cursor
Deletes the contents of the line after the cursor and replaces it with new text. To stop insertion, press the ESC key
Removes an entire line
Deletes X number of lines
Deletes the contents of the line following the cursor
Deletes X number of words
Deletes from the current line to the end of the file
Deletes from the current cursor position to the line’s beginning
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.
Undo the most recent change
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.
Copies (yanks) a line of text
Copies the current word from the character with the lowercase w cursor until the word is finished
Paste a line of yanked text after the current line
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.
Joins two lines
Repeats the current line
Swaps two lines
In this section you will find commands that will help you navigate with in a file without using a mouse.
Moves up one line
Moves down one line
Moves one character to the left
Moves right one character
Navigates to the last line of a file
Navigates to line X in a file
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.
wq or ZZ
Saves your work and exits vi editor
Continues editing after saving
Saves (and writes to a non-writable file)
Quits vi without saving the changes
Searching String Commands
Suppose you want to search a string. Here are some commands to help you.
Forwards lookup for a given string
Backwards lookup for a given string
Forwards search string at the start of a line
Forwards search string at line’s end
Proceeds to the next occurrence of the searched string
Looks for the word he (rather than there, here, and so on)
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
Deletes the current line and insert text into buffer a
Paste the line from buffer a
To substitute or remove any character or line from a text, you can use the commands listed below.
Removes the line’s contents, returning you to insert mode
Changes the current word with new text, starting with the character under cursor
Overwrites characters beginning with the cursor
Substitutes one character under the cursor and continue to insert
Substitutes the entire line and begin to insert at the beginning of the line
You can alter the appearance of your Vi screen by using the following list.
Sets case insensitivity while searching
Sets auto indention
Unsets auto indention
Shows lines with line numbers on the left side of the screen
Sets the width of a software tabstop
If wrapscan is enabled and the word is not found at the end of the file, it will look for it at the beginning
If this option is set to a value greater than zero, the editor will “word wrap” automatically
Sets the file type to “read only”
Prints the terminal type
Discards control characters from input
You can also scroll your screen using commands or keyboard keys. In this section, I have demonstrated the keys that you can use.
Moves the screen down by half a page
Scrolls the screen down by a full page
Scrolls the screen up by half a page
Scrolls the screen up by a full page
Scrolls the screen up by one line
Scrolls the screen down by one line
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.
Returns to the word’s beginning
Goes to the end of the word
Yanks X number of lines
Shows the line number of the current line
Switches to Command mode /Terminate insert mode
Switches two characters
Changes the case of an individual character
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.