
Linux Command Line
The VI text editor is a powerful and versatile tool for editing text files in Linux. Whether you’re a beginner or an experienced user, this guide will help you understand the basics of using VI and take your skills to the next level. In this article, we’ll cover when to use VI, how to use it, and provide a cheat sheet of common VI commands.
When to Use VI:
VI is a command-line based text editor, which means it’s ideal for editing files on a Linux server or in a terminal window. It’s also useful for editing files in a terminal emulator on a desktop or laptop. VI is particularly useful for editing system configuration files, scripts, and programming code.
Getting Started:
To open a file in VI, simply type “vi” followed by the file name at the command prompt. For example, to edit a file called “example.txt”, you would type “vi example.txt”. To create a new file and open it in VI, type “vi” followed by the new file name.
VI Modes:
VI has two main modes: command mode and insert mode. In command mode, you can navigate the file and perform various editing commands. In insert mode, you can add or change text in the file. To switch between modes, press the “Esc” key to go to command mode and the “i” key to go to insert mode.
Cheat Sheet of Common VI Commands:
- “i” – enter insert mode
- “Esc” – return to command mode
- “:w” – save the file
- “:q” – quit VI
- “:wq” – save and quit VI
- “:x” – save and quit VI (same as “:wq”)
- “dd” – delete the current line
- “u” – undo the last change
- “Ctrl + R” – redo the last change
- “/text” – search for “text” in the file
- “n” – move to the next occurrence of the search term
- “N” – move to the previous occurrence of the search term
The “r” command is used to replace a single character at the current cursor position. For example, if you are in command mode and the cursor is on the letter “a” and you type “r” followed by the letter “b”, the letter “a” will be replaced by the letter “b”.
The “R” command is similar to the “r” command, but it allows you to replace multiple characters at the current cursor position. You can use it to overwrite text by entering insert mode by pressing the “R” key and then typing the new text. It is similar to pressing “r” repeatedly but allows you to insert multiple characters at once.
The “y” command is used to yank (copy) text. It is used in combination with other commands to copy text to a register. For example, you can use “yy” to copy the current line, “yw” to copy the current word, or “y$” to copy from the cursor position to the end of the line. The yanked text can be pasted later using the “p” command, which paste the text after the cursor or “P” command to paste the text before the cursor.
It is also worth noting that you can also use the “yy” command to copy multiple lines by specifying a number before it, such as “3yy” to copy 3 lines.
Navigating and Editing the File:
- “h” – move cursor left
- “j” – move cursor down
- “k” – move cursor up
- “l” – move cursor right
- You can also use your arrow keys
- “w” – move cursor to the beginning of the next word
- “e” – move cursor to the end of the current word
- “b” – move cursor to the beginning of the current word
- “0” – move cursor to the beginning of the current line
- “$” – move cursor to the end of the current line
- “gg” – move cursor to the beginning of the file
- “G” – move cursor to the end of the file
- “:num” – move cursor to line number “num”
VI is a powerful and versatile text editor that is ideal for editing files on a Linux server or in a terminal window. By understanding the basics of VI and the common commands, you’ll be able to navigate, edit and save your files more efficiently. Practice makes perfect, so take some time to familiarize yourself with these commands and soon you’ll be a VI pro.
See our list of 75 Linux commands you should know about.