VI Editor Made Easy: Tutorial for Efficient Editing
What is a VI editor?
Ah, the venerable Vi editor - the tool that strikes fear into the hearts of newbies and earns the respect of experienced command-line warriors. VI editor is a powerful command-line text editor used in Unix-based operating systems. It allows users to create, edit, and manipulate text files using a variety of commands and shortcuts.
Imagine Vi as a magic wand that lets you edit text like a boss. But unlike other text editors that are designed to coddle you with friendly buttons and menus, Vi is like the wild west - raw, powerful, and uncompromising. It's the kind of tool that separates the keyboard cowboys from the keyboard cowards.
Modes and Editing Commands
First things first, let's talk about the two modes of VI editor:
The first is command mode, which is where you'll spend most of your time. This mode allows you to navigate and manipulate the text file. The second is insert mode, which lets you insert text into the file. To switch to insert mode, press the "i" key. To get back to command mode, hit the "Esc" key. It's that simple!
Some basic navigation commands:
To move the cursor, use the "h", "j", "k", and "l" keys, which represent left, down, up, and right, respectively. You can also use the arrow keys, but trust me, the other keys are much more efficient.
Example:
This is some text.
In command mode, hit the "j" key to move the cursor down to the next line:
This is some text.
|
Note: In this example, the cursor is represented by the vertical line (|).
To delete a character, use the "x" key.
Example:
This is some text.
|
In command mode, hit the "x" key to delete the "T" in "This". Thus:
his is some text.
|
To delete an entire line, use the "dd" command.
Example:
This is some text.
This is another line.
|
In command mode, hit the "dd" keys to delete the second line. Thus:
This is some text.
|
Some basic editing commands:
To insert text, switch to insert mode by pressing the "i" key.
Example:
This is some text.|
In insert mode, type "new" to insert the word "new" after "This is some text.":
This is some text.new|
To replace a character, switch to command mode and move the cursor to the character you want to replace. Then hit the "r" key followed by the replacement character.
Example:
This is some text.n|ew
In command mode, move the cursor to the "n" in "new" and hit "r" followed by "N" to replace "n" with "N":
This is some text.N|ew
Advanced Editing Techniques
Copying and pasting text in VI editor:
To copy text, move the cursor to the beginning of the text you want to copy and hit the "y" key twice. This will copy the current line. To copy multiple lines, hit the "y" key followed by the number of lines you want to copy and then the "y" key again.
To paste the copied text, move the cursor to where you want to insert the text and hit the "p" key.
Example:
This is line one.
This is line two.
This is line three.
|
In command mode, hit "2y" to copy the second and third lines, then move the cursor to the end of the file and hit "p" to paste the copied lines:
This is line one.
This is line two.
This is line three.
This is line two.
This is line three.
|
Searching and replacing text in VI editor:
To search for a specific word or phrase, hit the "/" key followed by the word or phrase you want to search for.
Example:
This is some text.
This is some more text.
|
In command mode, hit "/more" to search for the word "more".
To replace a word or phrase, hit the ":%s/old/new/g" command, where "old" is the word or phrase you want to replace, and "new" is the replacement word or phrase.
Example:
Using the previous example as an instance. In command mode, hit ":%s/more/less/g" to replace all instances of "more" with "less":
This is some text.
This is some less text.
|
Working with Multiple Files
This is an important skill to have, as it allows you to switch between different files without having to close and reopen VI each time.
To open a new file in VI, you can use the following command:
:edit <filename>
For example, to open a file called "file2.txt", you would enter the following command:
:edit file2.txt
This will open the file in a new buffer within VI. You can switch between buffers by using the following command:
:buffer <number>
For example, to switch to buffer 2, you would enter the following command:
:buffer 2
Conclusion
Well, folks that bring us to the end of our journey through the wonderful world of VI editor. I hope that this brief series has given you a good foundation to build upon and that you're now well-equipped to navigate, edit, and customize your text files using the VI editor.
While VI editor may seem daunting at first, with a little bit of practice and patience, you'll soon find yourself editing text like a pro. So don't be afraid to dive in and experiment with different commands, mappings, and plugins - after all, the beauty of VI editor is its versatility and flexibility.
And remember, text editing doesn't have to be a dull and dreary task - with VI editor, you can inject a little bit of fun and personality into your work.
Subscribe to my newsletter
Read articles from Ugochukwu Charles Eugene directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ugochukwu Charles Eugene
Ugochukwu Charles Eugene
DevOps engineer, footballer, Nigerian, and technical writer with a passion for leveraging technology to drive innovation and sharing knowledge through engaging content