Sometimes, the intrinsic text needs a user-friendly and feature-ready tool for editing. To edit text on Linux, there’s a Text Editor called Vi(Visual Instrument). VI text editor helps accomplish work with exclusive keywords instead of menus. VI Text editor works in either insert mode, command mode, or command-line mode. Insert mode is where the text that you type in becomes part of the document, and the command mode is where each keystroke is considered a command by the system. You get the command line mode by inserting the colon : key. If you press the key ‘i during the command mode, it turns the editor into insert mode. We have created this post to let you know the basics of VI Text Editor so that you feel easy to work on text files on the command line interface.
Vi is a modal editor; thus, the above two functions are possible without having to go to the home button to assign individual functions to specific keys, as several purposes, but the primary one was to make visual text editing User-friendly for users and make the text attractive for them. Welcome to the introduction of VI Text Editor.
How to Use VI Text Editor?
Vi uses the basic format of editing and does not follow up with fancy projections like others. Here you will not retrieve a text which looks precisely as it is presented on your screen. The editor lets you change, delete and add text to your file but does not allow you to do additional formatting such as paragraph indenting and line centring.
#1. Opening the VI Text Editor
To launch the VI Text Editor, open the terminal and type the following.
vi where “filename” is the name of that file which is already existing in the memory of the Text Editor. You can also create a new file by entering the command . If you do not remember the name of the file, you have to enter the entire pathname of the file. If the file that you want to open does not exist, it will show a screen with only a cursor, and the tilde (-) sign will occur in the first column.
#2. Operating Modes of VI Text Editor
There are two modes of VI Text Editor-
- Command Mode
- Insert Mode
#1. Command Mode:
The default mode on the VI Text Editor is Command Mode. In Command mode, essential functions are performed by giving commands. These include moving around in the file, making changes, and exiting that particular file. These commands are executed not by using any specific key but by using the alphabetical keys present on the QWERTY Keyboard.
Commands given in the command mode are in the form of alphabetical mode but are case-sensitive. “I” is not the same as “i”. Commands given in the Command mode when processed, do not appear on the screen as you enter them but instead, they appear in the last line. When you are giving commands in the Command Mode of VI Text Editor, you have to hit colon to take the cursor to the last line of the text and then enter the command and hit .
For example, if you want to save a particular file, then first hit and the “W” and hit . Talking the cursor to the last line of the file is also called the last-line mode or command-line mode.
#2. Insert Mode
This is the mode in which the text is created. You can switch to this mode from the Command Mode by pressing”i” on the Command Mode. While creating text, you should enter “Return” and move to the text line or set up a “wrap margin” and do the same. You can enter Insert Mode by other means too, but you can exit it by only using the button.
If you want to know which mode you are in, then you can press the button and check it by pressing a key. If the key prints itself, then you are in the Insert Mode, or else you are in the Command Mode.
#3. Moving the Cursor Within a File in VI Text Editor
Since the command mode accepts commands when you hit an alphabetical key can use it to move within a file without using the usual cursor keys.
- “ķ” – For moving the cursor in the upward direction
- “j” – For moving the cursor in the downward direction
- “h” – For moving the cursor in the left direction
- “i” – For moving the cursor in the right direction
To use these keys, you have to be in command mode.
#4. Closing and Saving Files in the VI Text Editor
Closing and saving files in the Text Editor will happen from Command mode. For doing these, there are some codes for closing and saving a file:
- : wq! – For saving the file and quitting VI Text Editor.
- : w! – For saving the file but keeping it open.
- : q ! – For quitting the file without saving it, or the changes that you made to an existing file. This command can also be called control for forced closure.
#5. Deleting Text in VI Text Editor
In VI, you have to use specific commands to remove parts of the text. These commands will help you to eliminate the character, a word or a specific line. Since the editor has to be in the Command Mode while deleting parts of the text on the editor, you have to enter the Insert mode again to have subsequent text insertions into the text file.
#1. Deleting One Character From The Text
To remove an individual character from your text file, you have to place the cursor above the particular character to be deleted and type “x”. This command will delete the character and also the space occupied by that particular character. This command shall also help you to eliminate blank spaces wherever required.
This command, with “x”, deletes the characters or the spaces on the right-hand side of the cursor, where it is placed.
Alternatively, if you want to delete characters or aces to the left-hand side of the cursor, then place the cursor accordingly and hit “X”.
#2. Deleting A Word Or Line From The Text
Place the cursor at the beginning of the word you want to delete and enter the command <dw>. This will remove the word and the space that it had occupied. To eliminate part of a word, place the cursor on the right side of the part which you want to save. Enter the command <dw>, and this will delete the rest of the word, for removing a line. Place the cursor anywhere on the line, and hit the command <dd>. This will delete the line as well as the space that it had occupied.
#6. How to Append Text in VI Text Editor?
There are times while preparing a text file wherein you want to simply enter a text that you have missed out by mistake. Appending a text is an easy task wherein you have to move to the Command Mode of the text editor and simply enter the line number in which you want to add a text followed by “G”. If you wish to add something to the 6th line of the text, you simply move to the Command Mode and hit “6G”. This will take the cursor to the end of this line, and you can move to the Insert Mode by hitting “A”. To edit the last line of the text file, you have to simply hit “G” in the Command Mode of the text.
#7. Inserting a Line Between Two Lines
You can also enter lines in-between lines of the text. If you want to enter a line above the current line, take the cursor to the left-hand corner of that line and hit the command “O”. This will move the current line below its former occupied space, thus, creating a space above it. If you want to enter a line below the current line, take the cursor to the left-hand corner of that line and hit the
command “o”. This will move the current line above its former occupied space, thus, creating a space below it.
#8. How to Cut, Copy and Paste in VI Text Editor?
For copying a particular text, you have to hit the command”y”, followed by the direction keys, which will help you select the text you want to copy.
You have to hit the command “d”, followed by the direction keys. This shall help you to cut the necessary parts of the text.
To paste, move the cursor to the required position and hit”p” to paste the text after the cursor or”P” to put the text before the cursor.
#9. Searching Within a File on VI Text Editor
To search within a file, you need to be in Command Mode. Type “/” followed by the string which you want to search in that particular file. After this. You have to hit . This will lead you to the first occurrence of the word in the file. Then you can hit “n” to find the next occurrence or”N” to go back to the previous occurrence.
#10. Searching and Replacing in VI Text Editor
If you want to search a certain word and replace it with another one. then you need to activate this particular command in the Command Mode.
<;%s/(the word you want to search)/(the word you want to replace with)/
For example, if you want to replace “soft” with the word “hard”, then the command for this goes like this, <:%s/soft/hard/>
If you want to replace all occurrences of the word, then you need to follow it up with a “g”. <:% s/soft/hard/g>
#11. Editing Multiple Files and Switching Between Them
You have to use ex commands for carrying out this task. The command for this goes as follows. For invoking multiple files,
vi <name_ of filel> <name _of file2>
For example, vi power play
This command will open two files, “power” and “play”, in a particular order. You have to do the edits in the text file “power” and save it with <w> and move to the next file with <n and do the edits and save it with *. Just keep in mind that when you switch between two files, make sure that you go to the Command Mode to execute the command. Besides, if you want to insert another text file while editing the above two lines, you can also do that with the following command. First, save the files with <:w> and then open other files with
<:e (filename) >
For example, if you want to open another text file when editing the files “power” and “play”, you can insert another text file by opening it with
This will open the file “button,” and you can continue your edit by switching between the files.
We hope this post would help you in learning the basics of VI Text Editor. Thanks for reading this post. Visit our social media page on Facebook, LinkedIn, Twitter, Telegram, Tumblr, & Medium and subscribe to receive updates like this.