File editing with the VI editor in Linux
We will start learning the VI editor which is the standard text editor on Unix-based operating systems
You have to learn VI because is some cases, you will not find a graphical text editor, so the only solution to edit files is to use the VI editor
Let us first of all see how to open a file with VI
To open a file with the vi editor, open a terminal or a command prompt
and then type VI followed by the name of the file and then press enter
for example if you want to open my_data.txt file, type vi my_data.txt and then press enter
Vi will open the file and you will be in the command mode which is the default mode for vi
From here you can navigate either to the insert mode or to the last line mode.
Let us see how to navigate from the command mode to the insert mode to write some text or making some change and how to navigate to the last line mode to quit, save the changes and so on.
First off all, let's open the file by typing vi my_data.txt and then pressing enter
now we are in the command mode!
1. Insert Mode
you can switch from the command mode to the insert mode by pressing a o i either in capital or in lower case
pressing any of this keys will take you from the command mode to the insert mode which will enable you to start adding text, deleting text, doing whatever you want to do with this file.
After doing what you want to do, you can go back to the COMMAND MODE by pressing the escape key.
If you have finished your work with this file and you want to save or discard the changes you have to switch from the command mode to the last line mode
You can do that by pressing the COLON key
An important point to note here. you cannot switch from the insert mode to the LAST LINE mode directly. To switch to the last line mode, you have first of all to go back to the command mode and switch to the LAST LINE mode
here you can discard the changes and quit the vi editor, save the changes and quit and so on.
to go back to the command mode just press the escape key and you are now in the command mode.
2. Command mode
Let's see what we can do we are in the command mode with the vi editor
Just a reminder, when you open a file with VI, you will be in the command mode which is the default mode for vi
2.1 Navigation
let us explore in this lecture, some navigation options that we have in the VI editor
To move the cursor left, down, up or right, you can use the h, j, k, l keys respectively, and You can use the arrow keys for navigation as well.
To move the cursor to end of the current line, press the $ sign key, now the cursor is at the end of the current line
Let's say that you want to move the cursor to the end the file? what should you do here? you can achieve that by pressing the G key in CAPITAL
To move the cursor to a specific line, press the colon key (:) to switch to last line mode and then enter the number of the line that you want
For example to move the cursor the 3rd line, press : colon followed by 3 and then press enter after that you should find the cursor at the third line
2.2 Editing
We have also some editing options in VI which can speed up our work
For example
- To delete the character under the cursor you can do that by pressing the x key
- To delete the entire current line, press the d key twice
- If you want to copy the current line, you can do thay by pressing the y key twice
- To paste the line that you have copied, first of all, move the cursor to the location that you want and then press p to paste it
- Let's say that you are unhappy with the last change that you do, so you want to undo it, how you can achieve that? you can undo the last change by pressing the u key
- and to redo the last undone action just press ctrl-r
2.3 Searching
Let us see how to search for words or a sequence of characters in vi
- To search for a word, first press the forward slash key and then enter the word that you want to find. By pressing enter, you will be in first occurrence of this word
- To navigate to the next occurrence, press the the n key in lowercase
- To go back to the previous occurrence, press the n key in uppercase
3. Last line mode
we will see now how to save or discard the changes and how to quit the vi editor?
- To save the changes that you have made without quitting the VI editor, you can do that by pressing : followed by w
- and then if you wan to quit, just press : followed by the q key and press enter
- To save the changes and quit the vi editor at the same time, press : followed by w to save the changes followed by q to quit and finally press enter
- To discard the changes and quit the vi editor, press : followed by q followed by ! an exclamation mark and the press enter
Comments
Post a Comment