The vi editor is the default editor of the UNIX operating system in which you can create a new file or edit the existing one. Also, you can read files through this editor. It is a classic text editor and available across all the Linux Distributions. It included by default on most of the Linux distributions out there and is very easy to use.
The latest and advanced version of the vi editor is VIM (Vi Improved). It has a lot of features and offers to edit the file in diverse possibilities.
It has three operation modes to work on it.
- Command Mode
- Insert Mode
- Last Line Mode (Escape Mode)
Command Mode is the primary mode of vi editor, and on startup, it opens in this mode. It understands only commands, and every typed character is counts as a command. With the commands, you can copy, paste, cut, and move the cursor. Also, you can save changes in file through this mode. Be careful to use letter case because commands are case sensitive.
By default, you are in Command Mode but if you want to enter to this mode from other modes, press the [Esc] key. The vi will beep or flash if it is command mode.
Insert Mode allows you to insert text into the file. Every character typed this mode reads as input and saves in the file. By default, vi is in command mode; to enter in insert mode, you should press “i” on the keyboard. To go back to command mode and save changes, press the Esc key.
Last Line Mode (Escape Mode)
Last Line Mode helps you jump on the last line of the screen, execute the command, and save changes. To enter this mode from command mode, press colon [:].
vi Editor Commands
In this article, we will show you some useful vi editor commands. Vi editor provides numerous options for different purposes.
1. File Opening
If you want to open a file or have to create a new file in your Linux server, you can use the following command:
$ vi output.log
This command will open the output.log file, or if it does not exist, it will create a new file in the working directory.
When you are working on a production environment, and most probably you want to open a config file in read-only mode:
$ vi -R app.config
You can save changes even if a file is open in a read-only mode using “:wq!”.
2. File Navigation
In a file, you can move up, down, left, right, and do other actions using commands. These commands are one-character commands. With the following commands, you can do action differently within a file without affecting file text.
- k: Move cursor one line upward.
- j: Move cursor one line downward.
- h: Move cursor one character left.
- l: Move cursor one character right.
- 0 or |: move the cursor at the start of the line.
- $: move the cursor at the end of the line.
- W: move the cursor to the next word.
- B: move the cursor to the previous word.
- (: set cursor to the start of a sentence.
- ): set cursor to the start of the next sentence.
- H: Moves to the start of the screen
- nH: Moves to the nth line of the screen
- M: Move to the middle of the screen
- L: Move end of the screen
- nL: Move to nth line of the screen from bottom
3. File Scrolling
Some useful commands used with the control key are below;
- CTRL+d: Half screen move forward
- CTRL+f: One full screen move forward
- CTRL+u: Half screen move backward
- CTRL+b: One full screen move backward
- CTRL+e: Move up screen one line
- CTRL+y: Move down screen one line
- CTRL+l: Redraws Screen
4. File Editing And Inserting
In insert mode, you can edit and replace the text in a file using below commands:
- i (Lowercase) – Insert text before the current location of the cursor.
- I (Uppercase) – Insert text at the start of the current line.
- a (Lowercase) – Insert text after the current location of the cursor.
- A (Uppercase) – Insert text at the start of the current line.
- o (Lowercase) – Below the cursor location, create a new line for text entry.
- O (Uppercase) – Above the cursor location, create a new line for text entry.
- r (Lowercase) – Under the cursor location, replace a single character with the next character typed.
- R (Uppercase) – Text replaces from the cursor to the right.
- s (Lowercase) – Under the cursor, replace a single character with any number of characters.
- S (Uppercase) – Entire line replace.
5. Deleting Lines And Characters
To delete lines and characters from an opened file, you can use the following commands:
- X (Uppercase) – Before cursor location, deletes the character
- x (Lowercase) – At the cursor location, deletes the character
- Dw – Deletes from the current cursor location to next word
- d^ – Deletes from the cursor current position to the start of the line.
- d$ – Deletes from the cursor current position to the end of the line.
- dd – Deletes the line on the cursor’s current position.
6. Copy And Paste
To copy and paste text, you can use the following commands:
- Yy – Current line will be copied.
- 9yy – Copy current line and nine lines below
- p (Lowercase) – Paste the copied text after the cursor.
- P (Uppercase) – Paste the copied text before the cursor.
7. Searching Patterns
Like UNIX grep and find commands, you can also search in VI editor within the file. You can search a particular word or ID with top to bottom and bottom to the top order. If you want to find the first occurrence, use the top to bottom search, and for the last occurrence, use bottom to top search.
- /Error – This command will search the “Error” word from top to bottom and stop at the first occurrence. To see the next match enter “n” and use “Shift + n” for the previous match.
- ?Error – This command will search the “Error” word from bottom to top and stop at the first occurrence. To see the next match enter “n” and use “Shift + n” for the previous match.
8. Running Shell Command Inside VI Editor
In the editing process, sometimes you want some more information on why you need to run some shell commands. For this purpose, you usually close the VI editor, and then you run a shell command. This is not a quick way to do it.
You can execute a shell command directly from VI editor. Use the “!” sign before the command in command mode. For example, you need to run the “ls” command, and you can type;
If you want to go shell without quitting VI editor, use the “!sh” command and use the “exit” command to go back to VI editor.
9. Find and Replace Text in File
vi editor provides its own find and search command ‘:s’. This command will look for a particular pattern and will replace it with the given substitute.
The above command will replace the first occurrence of the word ‘Funny’ with ‘Hilarious’ in the file. To replace all occurrences, we have to use the g option.
Here g stands for all occurrences, but you can use a number 1,2,…N as well, which stands for nth occurrence on each line.
You can also specify a range of lines for which find and replace will work.
The above command will replace all occurrences of the word ‘Funny’ with ‘Hilarious’ between lines 5 to 20 only.
To ask for confirmation before replace, we can use a command as shown.
10. Other Useful Options
VI editor has some other useful options as well, which we will discuss one by one.
:set nu – This option will enable line number in front of each line, which is quite helpful if you want line by line information. You can disable it using “set nonu”.
:set hlsearch – This option will enable highlighting the matching word whenever we will do a search in vi editor, which is quite useful for better visibility. You can disable it using “set nohlsearch”.
:set wrap – This option will wrap text in the file, which is quite useful when you have some long lines, and if you want to turn it off, you can use “set nowrap”.
:syntax on – This option will turn on color syntax, which is quite useful if you have open HTML, XML, PHP, or other programming language files. You can turn it off “:syntax off“.
:set ignorecase: This option will let you do case insensitive search. When this option is set, the search will ignore the case.
:set smartcase: This option will allow you to do a case-sensitive search. When this option is set, the search will consider the case as well.
11. Save And Exit
To save and exit a file in vi editor, press the Esc key with following Colon (:) to come back into escape mode and then use the following commands:
- q – Exit the file
- q! – Discard changes and exit the file.
- wq – Save and exit the file.
- w fileName – Save the file with a new name provided.
- w! fileName – Forced save the file (it will overwrite changes).
In the end, You can see the vi editor provides you plenty of options. To learn more about these commands usage, you can use the help option in the terminal while in the particular command.