LPI 010-150 – Text Files Editing
Basics of VI or Vim Editor the VI editor is installed on almost very Unix distribution, and Vim is by default installed in the most Linux distributions. VI and Vim both are similar. Every system administered reader should know VI or Vim because it’s an easy tool to solve many problems. VI or Vim has three modes command Mode the VI editor starts in Command mode. In Command mode, you can type comments. Some comments will bring you to another mode called Insert Mode, and in the Command mode, you can move the cursor, cut or paste text, or change to another mode. The second mode is the Insert mode. In this mode you can modify or add the tickets inside the file. The third mode is Ex mode. In this mode we can do many things such as save and quit from the file. If we press Escape key in the keyboard, we will exit the current mode. If we press Escape twice, it will bring us to the commands mode. To use them, you must at least be able to open the file, modify the file in the Insert mode, and save the file in the Ex mode. So to start using Vem, we will type Vem or Vim followed by the file name. For example, test file. If the file exists, the file is opened and the contents are displayed. If the file doesn’t exist, VI will create it when the edits are saved for the first time. So let’s say press enter.
Now we are in the Command mode. So by default, when we open any file using VI or Vim, the default mode is the Command mode. To start inserting in the file, we should go to the Insert mode. To go to the Insert mode, we will press I now notice here the declaration of the Insert mode. So now we can insert any text such as this is the first file. Now to save the file, we should go to the command mode again using Escape key in the keyboard. So we will press Escape key. Now we are in the Command mode. Then we will press coron. Notice here the colon. After that, to save the file and exit, we will press X, then Enter. Now to validate the contents of the file, we will use Cat test File. Here the contents of the file. This is the first file. To open the file again using VM, we will use Vim followed by the file name. Now we are in the Command mode again by default, and to go to the Insert mode, we can use a capital to insert in the end of the line.
Now the cursor has been moved to the end of the line, so let’s type anything. For example, this is the end of the line, for example and to save the file without exit, we will press Escape Key in the keyboard, then press Coron. Notice here the Coron is coming. After that we will press W. Then press Enter. Now the file has been written and we still inside the file.
To insert a new line below the current line, we will press O Small. Now the cursor is moved. To the second line so we can type anything. For example, this is the second line. Then we can press Enter to add the third line. This is the third line. Now, to save the file, we will press Escape. To go to the command mode. After that we will press Coron to enter the save command. We can use X to Save and Exit, or we can use WQ exclamation mark to Save and Exit to validate our file. Let’s get the content of test file we will see the content is there now let’s open the file again using givem test file now the cursor at the last line this is the third line to insert a new line. Above this line we will press O Capital here. The cursor has been moved to the line number three. So we can edit anything. For example, this is New Lion. Then to save, we will go to the command. Mode using escape key. Then type colon and x to quit and exit. Now let’s open the file again.
If we do any changes in the file and we need to exit without save for example, this is error and we need to exit without save this line. So we will go to the command mode using SKF key, then type colon, then Q exclamation mark, then press enter. If we open the file again and notice here the cursor. At the end of the file. To go to the first line, we will press double g small. Now, the cursor has been moved to the first line to copy this line by default. As we mentioned, we are in. The command mode. So to copy this, we will press YY. And to paste this as the end of the file, we will press P. Now, the first line has been copied and to search in the file for a specific word. In the command mode we will press Forward Slash. Notice here the forward slash is coming. Then type the word which we need to search for. For example, this t capital Hi s. As we mentioned before, everything in Linux is case sensitive. Then we will press Enter. Here we will see the result by default the search will be start from top to bottom direction. So if we need to search for the next result, we will press N small.
Notice here the cursor is moved to the second result. If we press N small again it will go to the second line. If we press N again it will go to the third line. If we need to search to the opposite direction we will press any capital. So in the capital it will get the result from the second line. And if we press any capital again. It will get the result from the first line. Now to search and replace specific word we have to go to the command mode by press escape twice then type colon then percentage sign s this it means it will search for this string and substitute it with that string to search for all the file, I mean globally we will put slash GI. Now we will notice that this has been replaced with that. So and to save the file we will go to the commands mode using the escape key then press colon and to save and exit if we open the file again and to delete the current line which is the last line, to delete this line we will press double D.
Now the line has been deleted. To save the file we will go to the command mode using escape then type colon then x then enter. Let’s open the file again. Now let’s cut and paste the specific line. For example we will cut the third line using GG and to paste this line at the end we will press p for Pin. Now the line has been moved to the end of the file. To save the file we will go to the command mode using escape key in the keyboard then type colon and x then enter.
Let’s open the file again. If we made any changes in the file and we need to undo the changes we will use U letter. So let’s for example add here new test for example new test and we need to undo the most recent change. So we will go to the command mode using escape command. After that we will press U. Now the new text has been moved, if we press control or we will redo last undo change. So the new text has been come again. So to save the file we will go to the command mode using escape key then press colon x then enter.
Popular posts
Recent Posts