DevOps engineers have to work on a wide range of toolsets to meet the organization’s requirements. In the DevOps role, one tool is going to stay forever to make an engineer’s life simple and more powerful. As the article heading says, it is “vi” editor. The “vi” editor is created by Bill Joy in 1976 for Unix operating systems. Though it has been created 46 years ago, still “vi” editor is one of the most widely used text editors in the world. “vi” editor is mostly found in *nix operating systems and doesn’t feature on the windows operating system. In the DevOps world, engineers more often need to play around with Linux instances, and “vi” editors can’t be avoided.
Once Dennis Ritchie said,
But this quote is complementing the “vi” editor as well. This editor looks simple but more powerful than you think. In this article, I will share 10 “vi” hacks to make the job easier. These “vi” tips and tricks will increase productivity for sure. This article is intended for Linux/Unix beginners.
To open a file using the vi editor, (vi file_name)
[lingesh@okd4 ~]$ vi redis_ep.yaml
- To set line numbers
- Press Esc to go to command mode.
- Enter :set nu
2. To insert a specific character at the beginning of the line.
- Press Esc to go to command mode.
- Enter :1,19s/^/ / (To add a space character from line 1 to line 19 at the beginning of the lines )
3. To insert a specific text at the beginning of specific lines.
- Press Esc to go to command mode.
- Enter :10,19s/^/UnixArena/ (To add text “UnixArena” from line 10 to line 19 at the beginning of the lines )
4. To search specific text in the “vi” editor.
- Press Esc to go to command mode.
- Enter “/” key and type the text which you want to search.
- Repeat the previous search using the n command.
- Repeat the previous search in the opposite direction by pressing the N command.
5. To perform search and replace operation on the current line.
- Press Esc to go to command mode.
- Navigate to the specific line where would you like to perform the search and replace operation.
- Enter :s/OLDtext/NEWtext. In my example, I have replaced “redis” with “redis-new“
- If you want to replace multiple occurrences on the current line, just /g
- Ex: :s/OLDtext/NEWtext/g
6. To perform search and replace operation on the complete file.
- Press Esc to go to command mode.
- Enter :s/OLDtext/NEWtext/g. In my example, I have replaced “redis” with “redis-new” on the whole file.
7. To perform a search and replace a range of lines in the file,
- Press Esc to go to command mode.
- Enter :#,#s/OLDtext/NEWtext/g. In my example, I have replaced “redis” with “redis-new” from line number 16 to 19.
8. How to select the text in the vi editor?
- You need to enter into visual mode by pressing the “v” command
- Use the arrow keys to select the texts.
9. To Convert to Upper and Lowercase or vice-versa
- Select the text by using the visual mode. (Refer above steps)
- To convert the text into UPPER case, press “gUU” in command mode
- To convert the text to lowercase, press “guu” in command mode
10. To delete the selected lines on the vi editor, simply enter “dd” (delete lines).
- You can also navigate to the line and press dd to delete the specific line.
- To delete 5 lines from the file, navigate to the line and press 5dd in command mode.
Hope this article is informative to you.
Leave a Reply