If you’re planning on becoming a linux ninja, then you need to learn how to use the text editor called vi.
There are so many text editors in the linux world but the Linux Professional Institute 1 (LPIC-1) certification requires you to know the basics of vi.
vi is very powerful and knowing the basics can help you save so much time when editing text.
However, vi is not particularly easy to learn at the beginning and it does require some memorisation. Keep using vi and it becomes second nature, you will be moving through it in no time.
If you’re new to linux then I’d recommend that you understand the basics of working around a linux command line before attempting this post, although it is NOT a prerequisite. My post ‘How To Work On The Linux Command Line‘ will hopefully help you through this if you need it.
As always, the best way to learn is by doing. I encourage you to follow along with me and perform the commands yourself if you truly want to learn and understand whats happening here.
I apologise in advance for the sheer lack of images in this post! What can I say, there really isn’t much I can show you!
echo “This is line 1” > file.txt
echo “This is line 2” >> file.txt
echo “This is line 3” >> file.txt
Execute the above commands in order from top to bottom in a linux terminal.
The first ‘echo’ command will create a brand new file called ‘file.txt’ and write a line of text on the first line. CAUTION: If a file with the same name exists in the directory then it will be over written.
The reason it creates a new file is because we are using ONE right-angle bracket (>).
The second ‘echo’ command will APPEND a line of text to this ‘file.txt’ file. It will create a string of text onto the next available, blank line in the file which happens to be line 2. The reason this command APPENDS and does NOT overwrite the original file, is because we are using the double right-angle brackets (>>) to append our string of text.
The third command will work exactly the same way as the last ‘echo’ command, except that it will append the string of text to the third line in our file.
Finally, the ‘vi’ command is used to open up the text file for editing.
Without the angle-brackets in any of these commands, ‘echo’ would normally display the string of text to the console for us to see. By using the angle brackets we are REDIRECTING the output to a file. If you want to know more about ‘redirecting’ then check out my post about ‘Streams, Pipes & Redirects‘.
COMMAND Mode & INSERT Mode
vi has two modes. The Command mode and the Insert mode.
Insert mode is the easiest to explain so I will cover that first. In fact, you probably have already edited a text file once or twice before, and so that is all insert mode is, just regular text editing.
Command mode is where the power of vi is. In command mode we can search for text in our text file and we can move text around with ease. Note that we CANNOT insert any text while we are in Command mode.
We cannot be in both modes at the same time so we need to know how to switch between modes.
When we are in insert mode and we want to switch to command mode, we simply press the escape (Esc) key. We need to be in command mode to save the file and exit.
When we are in command mode and we want to switch to insert mode, we can press one of many keys to enter us into insert mode, that will insert us in a particular way. These different insert modes are covered in the next section.
Different INSERT Modes
A, I & O (a, i, o)
When we are in Command mode and we want to switch to Insert mode, we can press one of many keys to switch over.
We can simply press the ‘i’ key. Once we are in Insert mode, we can now enter text into our text file.
Unfortunately there’s no real big sign given to us as to which mode we are in! If I’m not sure which mode I’m in, I usually press the Escape key a couple of times. This way I’m quite certain that I’m in Command mode.
Other than entering into Insert mode with the ‘i’ key, we can also enter Insert mode with the ‘a’ key. There’s a very slight difference here. Entering into Insert mode with the ‘a’ key, the cursor will place you at the next character.
If we want to enter Insert mode BUT place our cursor at the next line from where the cursor is, we can use the ‘o’ key.
To exit out from Insert mode and bring us back to Command mode, we simply press the ‘Esc’ (Escape) key.
Navigating In COMMAND Mode
UP, DOWN, LEFT, RIGHT
When vi opens for the first time then we are usually in Command mode. Sometimes we can use the arrow keys on the keyboard to move our cursor around the text but we need to know that the HJKL keys are for moving UP, DOWN, LEFT and RIGHT.
The H key is on the left side of these set of keys and this is for moving LEFT. The L key is on the right side of this key set and is used to move right. Simple enough so far right?
Well the J key is for moving down, and the K key is for moving the cursor UP.
A good way to remember this is that K is for kite and kites fly up in the air. Therefore K is for moving UP.
"Copy", "Cut" & "Pasting" Text Strings In COMMAND Mode
We’ve all heard of copy, cut and paste right? In vi we have similar features. But to make things a little more challenging we have given these features different names. We can’t make it too easy, right?
Delete A Line Of Text (Cutting)
So once we have pressed the Escape key to enter command mode, we can now try to delete a line of text from our text file. To do this we simply press the ‘d’ key twice (dd). Think of it as ‘deleting’ the line, or ‘cutting’. The deleted line is now placed into what we call the register (The deleted line of text is placed into memory). Think of the register as a ‘clipboard’.
If we don’t want to delete a whole line of text with the ‘dd’ command, we can use ‘dw’ (delete word) to delete a word instead.
Note that characters such as an exclamation mark (!) is considered it’s own word.
Delete A Letter
We can also delete just one letter (character) while in command mode too. Using dl (lowercase L)
To copy a line of text from our file, we first make sure we are in Command mode (by pressing Escape). Then by pressing the ‘y’ key twice, we copy the whole line that the cursor is at. In vi, we call this ‘yanking’.
We are copying this line of text into the register, and we don’t see anything happening on the screen when we do this.
Next we need to ‘put’ (paste) this text back into our file (See ‘Putting’ below).
We can also yank a single letter by using the command ‘yl’ or yank a single work with the command ‘yw’.
Putting (or Pasting)
If we use the navigation keys to move the cursor around the screen (HJKL), we can then press the ‘p’ key anywhere we like, and the text that is held in the register will be placed down.
Think of ‘p’ as ‘pasting’, but in vi we call this ‘put’.
We can use ‘put’ right after copying with yank, or by deleting a whole line with ‘dd’ for example.
Try these command yourself to get familiar with them (dd, dw, dl, p, yy)
NOTE About The New Line Character
I don’t want to go into much detail here, but in linux we have an invisible character at the end of a line of text.
This character tells the system that there’s a line break here, and the next set of characters are on a new line.
When we use ‘yy’ to copy a line of text, we are also copying this invisible character. So when it comes to placing down this line of text, this invisible character is also placed down, and so, the line appears on a new line from where your cursor happens to be.
Searching In COMMAND Mode
While we are in command mode (tap the Escape key to confirm we are in command mode if not already) we can search the text file for a particular string. If you’re unfamiliar with what a string is, it’s simply a line of characters. This can be a word or a partial word that can include any character on the keyboard, including spaces.
Searching with ‘/’
To start the search function in vi, we press the forward slash (/).
Now enter a string of characters and press the enter/return key.
If vi matches your chosen pattern then it will jump to the first match in the text (relative to your cursor position). We can “scroll” through each matching string by pressing the ‘n’ character. If we want to “scroll” through each matching string in reverse then we would use the uppercase n (N) character.
Searching with ‘?’
We can also use the question mark (?) instead of the forward slash to search through our text.
But in the case of the question mark, the lowercase ‘n’ is NOW used to “scroll” from bottom to top, and the uppercase ‘N’ is used to “scroll” from top to bottom. The roles have been reversed!
I want to search for the string “This is line” and I want to use the lowercase ‘n’ to “scroll” DOWN through the text in the file. First of all I need to be in Command mode. I do this by pressing the Escape key.
Next I will press the forward slash (/) character, followed by the string “This is line”. The screenshot below shows what this looks like:
Replacing(substituting) A Word(string) or Character
While we are in Command mode, we can change a character or a word by using the commands: cl (for ‘change letter’) or cw (for ‘change word’).
All we would do here is place our cursor over a word that we want to replace, and press cw (making sure we are in Command mode). Then we can type in our replacement word.
Changing a character works exactly the same way, except that we use ‘cl’ to change a letter (or character). Notice how our cursor will need to be hovering over the exact character we want to replace.
Save file In COMMAND Mode
What use is all this text editing if we can’t save our progress right?
Because vi is a command line based editor, there’s no drop-down menu list or floppy disk icon for us to click on to save our text file.
To save our file we must first be in command mode (By hitting the Escape key again).
Then we need to enter the colon (:) character (holding the ‘shift’ key and press the semi-colon (;) character on the keyboard).
What follows the colon character will depend on what you want to do. We can revert a save, force a revert to save, write to file, write to a NEW file (which works like ‘save as’ in a graphical environment) we can also quit, force a quit and write to file and quit.
Of course all of these options will take some time to master and the only real way to learn this is by doing.
The following list shows different write/save/quit commands:
: =execute. We start with the colon character
:e =revert to save. This will remove ALL changes since last save.
:e! =revert to save and force it, as ‘vi’ protects us from loosing our work.
:w =writes to original file
:w new_file.txt =writes to a new file called ‘new_file.txt’. (Works like ‘save as’)
:q =quit. If changes have been made but not saved, vi will tell us!
:q! =quit, but override’s vi’s cautionary message.
:wq =write, then quit. Notice how we can use TWO commands together. We can also use a shortcut for this command, which is: ZZ
ZZ is used in command mode and does NOT require the colon (:) character prepended.
In this post I covered the basics of vi which the LPIC-1 certification requires you to know. We have covered the two modes (Command mode and Insert mode), the different insert modes which can be used to enter us in to insert mode, Navigating around our text file while in command mode, “copy, cut and paste” our text, replacing text, searching through our text file and finally.. ‘Saving’ our file.
My advice to you when working with linux text files is to always use vi. nano is a very common text editor too in linux but in my opinion it would be a bad habit to always use it.
In the beginning, I always used to open a text file for editing with vi, but then found myself having to learn vi on the fly (I’m a poet and didn’t know it) when all I really wanted to do is edit a text file! Just stick with it as you only need to learn it once.
Once you get familiar with vi then why not try vim (vi improved)? Although, you are NOT tested on this command line tool on the LPIC-1 exam at the time of writing this post but well worth checking out as it contains some cool extra features.
Please note that ’emacs’ is also a popular text editor in linux and I hope to cover it sometime in the future.
I would make sure I knew all of the following before sitting the LPIC1 cert:
- / Search forward
- ? Search backward
- n Next search
- N Next search
- HJKL Navigation
- i, a, o Insert modes
- Esc Enter ‘Command Mode’
- cl, cw Change letter, or change word
- dd Delete a line (place into register)
- dl, dw Delete letter, or delete word
- yy Copy (yank) a line (place into register)
- yl, yw Copy (yank) letter, or yank word
- p Put (paste) from register
- :e! Revert to original file
- :wq or ZZ Writes and quits
- :w! Force file write (Even writes to a ‘read-only’ file!)
- :q! Force quit