With the Vi editor, you can create a new file, edit an existing file, move around the file, etc. In this guide, we will learn everything about the Vi editor, from creating a new file to editing the file using various modes.
Getting Started with the Vi Editor
The Vi editor lets users create new files or work with existing files using various commands.
1. vi new-file: The commands create a new file if it doesn’t exist. However, if the specified file exists, it will open it.
Here’s how to create a new file named linuxhint.
With the new file created, you can add text or exit using various commands that we will discuss later. Each new line has the tilde (~), symbolizing an unused line.
2. vi -R filename: The command opens the named file in read-only mode. That way, you can’t edit anything in the existing file.
You will note that the file is opened in the Vi editor but is in read-only mode.
3. view filename: The command works similarly to the one above in that it’s an alternative way of opening a file in read-only mode using the Vi editor.
Vi Operational Modes
Depending on your goal, you can use the Vi editor’s two edit modes: command and insert modes. Furthermore, the Vi has an Escape mode for executing various commands starting with a colon followed by the command.
1. Command Mode
The command mode is the default mode that loads when you open a file using Vi. In this mode, you type keyboard keys to navigate the file, copy, paste, move the cursor, etc. Ideally, the command mode lets you type various commands to manipulate the opened file.
To switch to the command mode, press the Esc keyboard key, and you will note a beep on the screen, signaling that you’ve entered the command mode.
2. The Insert Mode
After opening a file and needing to add text to it, you must switch to insert mode. For that, press the i keyboard key. Once you’ve entered the insert mode, you can start typing, and the new text will get written in the cursor’s current position.
Once your text is written, you can switch to command mode by pressing the Esc key.
3. Escape Mode
You must use the escape mode when performing other tasks, such as saving your file, changing its appearance, etc. You can quickly switch to escape mode by pressing the colon (:) on your keyboard.
Once in escape mode, type the command you want to execute and press the enter key. For instance, you can save and exit the file by pressing the :wq command.
Working with the Vi Editor
The Vi editor offers various ways of working with a file. There are commands to navigate the file, scroll through it, edit the file, search within a file, save, exit, etc. Let’s discuss the commands in detail and their descriptions.
1. Navigating a File
When you want to navigate within a file, you must switch to command mode to avoid affecting the text. Once in command mode, use the below commands to navigate your file.
- j: Pressing the j keyboard key will move the cursor down one line.
- k: It moves the cursor position up one line.
- h: It moves the cursor one character to the left.
- l: It moves the cursor one character to the right.
- $: Pressing it moves the cursor to the last character of the current line.
- 0 or I: Pressing it moves the cursor to the first position of the current line. Note that the I is uppercase because the Vi editor is case-sensitive.
- B: It moves the cursor to the first character of the previous word in the same line.
- W: It moves the cursor to the first character of the next word in the same line.
- H: It moves the cursor to the top of the window.
- nH: It moves the cursor to the nth position from the top of the window. For instance, 2H would position the cursor on the second line from the top.
- L: It moves the cursor to the bottom of the window.
- nL: It moves the cursor to the nth line from the bottom. Pressing 3L would move the cursor to the third line from the bottom.
Another convenient way of moving the cursor to a specific line is by pressing the colon followed by the line number. For instance, we could access the 4th line on the file by pressing :4 and pressing the enter keyboard key, as shown.
Most of the above commands help navigate a file one character at a time. However, you can scroll through the entire file using the commands in the section below, especially when you have a large file.
2. Scrolling Through the File
To quickly scroll through your file using the Vi editor, switch to command mode, then use the below commands. Note that you must use the control (ctrl) and other keys to execute a command.
- CTRL + d: This command moves the cursor to half the screen size.
- CTRL + u: The command moves the cursor position back to half the screen size.
- CTRL + f: It moves the cursor forward to one full screen.
- CTRL + b: It moves the cursor backward one full screen.
- CTRL + e: It moves the screen one line up.
- CTRL + y: It moves the screen one line down.
3. Deleting in Vi Editor
The command mode in the Vi editor lets you delete characters or lines in a given position as follows.
- X: It deletes the character on the left of the cursor position.
- x: It deletes the character at the current cursor position.
- d^: It deletes all the characters starting from the current position to the beginning of the particular line.
- d$: It deletes all characters in the line to the end, starting from the cursor’s position.
- dd: When pressed, it deletes the current line where the cursor is placed. Add the number of lines to delete more than one line from the current position. For instance, pressing 2dd deletes the next two lines, starting with the current line.
- dG: It deletes all the lines in the file, starting with the current line.
- D: It deletes all words from the current position up to the end of the line.
- Dw: It deletes all characters from the current position up to the next word.
- ndw: It deletes all characters from the current position to the specified words. For instance, 2dw deletes all characters for the next two words in the same line.
- u: It undoes the last change made to a given file.
- U: You can undo the changes made to the line by pressing U.
The above are the common ways of deleting characters, words, and lines using the Vi editor.
4. Editing the File
Editing the file, including inserting new text, using the Vi editor, requires you to enter the insert mode. Depending on which key you press to enter insert mode, you can edit the file differently.
- i: It is the most common way to insert mode, and pressing it will insert any new text you type before the cursor’s current location.
- I: Pressing the uppercase i will insert the text you type at the beginning of the cursor’s current line. Once you press it, the cursor will move to the start of the line.
- A: The uppercase a moves the cursor to the end of the current line. Any text you type will get inserted at the last position of the line.
- a: When you enter the insert mode by pressing a, the new text you enter will get inserted after the cursor’s current position.
- – It is used when you want to insert a new line for the text below the cursor’s position. The new text you enter will appear on a new line below the cursor’s current position.
- – Pressing the uppercase o creates a new line above the cursor position for the new text you enter.
- r: Enter insert mode but only replace the character under the cursor position with one character by pressing the r key.
- s: It replaces the current character under the cursor with any number of characters you enter instead of only one.
- R: Pressing the R key enters insert mode, which replaces all text from the cursor position to the right with the text you enter. Instead of moving the text to make room for the new text, it will replace the existing text to the right.
- S: It replaces the entire line with the new text you enter.
So, when you want to switch to the insert mode on Vi, use any of the options above to enter the insert mode, depending on what you want to achieve.
5. Yanking Commands
Copying on Vi is referred to as yanking. There are four options for copying text using the Vi editor.
- yy: The yy keyboard keys, when pressed, will copy the current line, and you can paste it elsewhere.
- p: It pastes the copied text after the current cursor position.
- P: It pastes the copied text before the cursor position.
- yw: It copies the text in a word starting from the current cursor position to the end of the word.
6. Saving a File in Vi
Vi allows users to save a file and quit or save and continue editing it. Most commands require you to be in command mode and then switch to escape mode by pressing the colon. Let’s check them out.
- Shift + zz: Pressing the shift keyboard key and the z key twice will save the current file and close it.
- :w: You can use the command to save the changes to your file while keeping it open. Ensure you are in command mode, then press :w and press the enter key. Once you do, a line displaying the file’s name, number of lines, and size in bytes will appear at the bottom, confirming that your text has been saved and you can continue editing it.
- :q!: The command will quit Vi and close the file without saving the changes.
- :wq: The command saves your file and closes the editor, taking you to your terminal window.
7. Searching on Vi Editor
With the Vi editor, you can quickly search and replace a given string or character. You can find one instance of the string or all the instances.
- /string: The syntax is used when you want to search for a specific string in the text.
In the image below, we are searching for the string “linuxhint” and pressing the enter key. The cursor will move to where the string is in the file. You can move to the next instance of the string by pressing the n or N keyboard keys while in command mode.
- :s/string: The syntax is used when you want to find a given string in the current line. The found string is removed unless you specify what string to replace it with.
In the below example, the cursor is in line 10, and we want to find the “linux” string.
In the output below, on line 10, we note that in the string “linuxhint,” the “linux” string is removed and not replaced.
Use the syntax below to find a string and replace it with another in the current line.
- :s/string/replace
Let’s find the string “linux” and replace it with “UNIX” on line 14, as follows.
Our output will replace the first instance of the target string on the line. Note that replacing the string only works for the current line where the cursor is positioned.
- :%s/pattern/replace – Use this syntax to find all instances of a given string and replace with another.
In the example below, we want to find every instance of “test1” and replace it with “linuxhint.” Therefore, our command would be :%s/test1/linuxhint, as shown below.
Note that in the output below, we managed to replace all instances of our target string with the new string.
8. Line and File Formatting
You can format how various lines in a text are organized using the Vi editor options.
- J: The command joins the current line where the cursor is to the next line, creating a joint line.
- ~: The tilde option switches the case of a given character under the cursor.
- <<: It shifts the current line by one shift width to the left.
- >>: It shifts the current line by one shift width to the right. It acts like creating a tab space.
- :! command: It is used when you want to execute a command while on the Vi screen. For instance, you can list all the contents in the current directory.
Here’s the output. To return to the Vi screen, press the enter key.
- CTRL + G: It displays the current name of the opened file and its status.
In the above, we note the filename is “test1”, and its other details are displayed.
- :f new-filename – It replaces the current filename with the new filename.
Here’s an example of changing the filename to “file123.”
You can verify the changed filename by checking its status using the previous command.
- :cd directory-name – The command changes the current directory to the specified one.
Let’s change the directory to ~/Desktop.
Here’s the output.
- :e filename: The command opens another file inside the Vi editor. Add the name of the file you want to open; if the file doesn’t exist, it will be opened as a new file.
- :e #: It toggles between the opened files.
- :r filename: It reads the specified file and pastes its content after the open file.
The example below reads the contents of “linuxhint” into the opened file.
Here’s the output.
9. Modifying the Vi Screen
There are various set commands to use to change the appearance of the Vi screen.
- : set ai: It sets auto-indent for the lines on the file.
- :set noai: It unsets the auto-indent on the file.
- :set nu: It displays each line in the text with line numbers.
- :set ro: When entered, it changes the open file to read-only mode.
- :set term: It displays the terminal type for your editor. In the case below, we can see the terminal type on the last line.
You can utilize the above commands when working with the Vi editor to edit your file.
Conclusion
The Vi editor is among the loved editors for the great features that it offers users. There is so much more that you can achieve using Vi than using other editors. The key is knowing which command to use to achieve a given thing. This guide has presented everything you should know about using the Vi editor in all its editing modes. *