#Quick #editor #hacks #DevOps #Engineers

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,

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.

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
  1. To set line numbers
  • Press Esc to go to command mode.
  • Enter :set nu
vi editor - set line numbering
vi editor – set line numbering

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 )
Insert space beginning of the line
Insert space beginning of the line

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 )
Inserted text beginning of specific lines - vi editor
Inserted text beginning of specific lines – vi editor

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.
search text in vi editor
search text in vi editor
  • 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
Search and replace on current line - vi editor
Search and replace on current line – vi editor

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.
Search and replace on file - vi editor
Search and replace on file – vi editor

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.
Search and replace on range of lines - vi editor
Search and replace on a range of lines – vi editor

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.
Select the text in the vi editor
Select the text in the vi editor

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
Convert the text to UPPER case - vi editor
Convert the text to UPPER case – vi editor

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.

Share:

administrator

ahmedaljanahy Creative Designer @al.janahy Founder of @inkhost I hope to stay passionate in what I doing

Leave a Reply

Your email address will not be published. Required fields are marked *