Vi – Linux file editor
- If you find that vi is hard to use, there is some good news: RHEL uses a user-friendly version of vi called vim, for “vi improved.”
- To start vim, just use the vi command. In this section, I will provide you with the bare essentials that are needed to work with vi.
- One of the hardest things to get used to when working with vi is that it uses two modes.
- After starting a vi editor session, you first have to enter insert mode (also referred to as input mode) before you can start entering text.
- Next there is the command mode, which is used to enter new commands.
- The nice thing about vi, however, is that it offers you a lot of choices. For example, you can choose between several methods to enter insert mode.
- Use i to insert text at the current cursor position.
- Use a to append text after the current position of the cursor.
- Use o to open a new line under the current position of the cursor.
- Use O to open a new line above the current position of the cursor.
After entering insert mode, you can enter text, and vi will work just like any other editor. To save your work, go back to command mode and use the appropriate commands. The magic key to go back to the command mode from insert mode is Esc.
When starting vi, always use the file you want to create or the name of an existing file you want to modify as an argument. If you don’t do that, vi will display the relevant help text screen, which you will have to exit (unless you really need help).
Saving and Quitting After activating command mode, you use the appropriate command to save your work. The most common command is :wq! With this command, you’ll actually do two different things.
- First the command begins with a : (colon).
- Then w saves the text you have typed thus far. If no filename is specified after the w, the text will be saved under the same filename that was used when the file was opened. If you want to save it under a new filename, just enter the new name after the w command.
- Next the q will ensure that the editor is quit as well.
- Finally, the exclamation mark is used to tell vi not to issue any warnings and just do its work. Using an ! at the end of a command is potentially dangerous; if a previous file with the same name already exists, vi will overwrite it without any further warning. As you have just learned, you can use :wq! to write and to quit vi. You can also use just parts of this command.
For example, use :w if you just want to write the changes you made while working on a file without quitting it, or you can use :q! to quit the file without writing the changes.
The latter is a nice panic option if you’ve done something that you absolutely don’t want to store on your system. This is useful because vi will sometimes do mysterious things to the contents of your file when you have hit the wrong keys by accident. There is, however, a good alternative; use the u command to undo the last changes you made to the file.
Cut, Copy, and Paste
You do not need a graphical interface to use the cut, copy, and paste features. To cut and copy the contents of a file in a simple way, you can use the v command, which enters visual mode. In visual mode, you can select a block of text using the arrow keys. After selecting the block, you can cut, copy, and paste it.
Use d to cut the selection. This will remove the selection and place it in a buffer in memory.
Use y to copy the selection to the designated area reserved for that purpose in your server’s memory.
Use p to paste the selection underneath the current line, or use P if you want to paste it above the current line. This will copy the selection you have just placed in the reserved area of your server’s memory back into your document. For this purpose, it will always use your cursor’s current position.
Deleting Text Another action you will often do when working with vi is deleting text. There are many methods that can be used to delete text with vi.
The easiest is from insert mode: just use the Delete and Backspace keys to get rid of any text you like. This works just like a word processor. Some options are available from vi command mode as well.
Use x to delete a single character. This has the same effect as using the Delete key while in insert mode.
Use dw to delete the rest of the word. That is, dw will delete anything from the current position of the cursor to the end of the word.
Use D to delete from the current cursor position up to the end of the line. Use dd to delete a complete line.
When working with ASCII text confi guration files, you’ll often need to replace parts of some text. Even if it’s just one character you want to change, you’ll appreciate the r command. This allows you to change a single character from command mode without entering input mode.
A more powerful method of replacing text is by using the :%s/oldtext/newtext/g command, which replaces oldtext with newtext in the current fi le.
This is very convenient if you want to change a sample configuration file in which the sample server name needs to be changed to your own server name.
Replacing Text w ith vi
1. Open a terminal, and make sure you’re in your home directory. Use the cd command without any arguments to go to your home directory.
2. Type vi example, which starts vi in a newly created file with the name example.
Press i to open insert mode, and enter the following text: Linda Thomsen sales San Francisco Michelle Escalante marketing Salt Lake City Lori Smith sales Honolulu Zeina Klink marketing San Francisco Anja de Vries sales Eindhoven Susan Menyrop marketing Eindhoven
3. Press c to enter command mode, and use :w to write the document.
4. In the name Menyrop, you’ve made an error. Using the r command, it is easy to replace that one character. Without entering insert mode, put the cursor on the letter y and press r. Next type a t as a replacement for the letter y. You have just changed one single character.
5. As the Eindhoven department is closing down, all staff that works there will be relocated to Amsterdam. So, all occurrences of Eindhoven in the fi le need to be replaced with Amsterdam . To do this, use :%s/Eindhoven/Amsterdam/g from vi command mode.
6. Verify that all of the intended changes have been applied, and close this vi session by using :wq! from command mode.