Vi Editor Tutorial For Beginners

Vi Editor Tutorial For Beginners

Welcome to this Vi Editor Tutorial for beginners. Vi is a robust and powerful way to work with files on Linux. Now, we say wonderful with a hint of sarcasm as some people do in fact love vi, while others despise it. It might be due to the nature of vi in that it is entirely keyboard based, meaning you must learn specific key combinations and sequence of key presses to get anything done. You won’t be moving the cursor around a file with the mouse like you might be used to. Fear not however, since once you start getting good with vi, your geek status will elevate to the next level, and you just might like working with vi on a daily basis. Let’s check it out in this vi editor tutorial for beginners.


Create A File With vi

The first thing we’ll do is to simply create a file with vi. Here we’ll create the file, add some text, and save it.

vagrant@homestead:~$ vi file.txt
vagrant@homestead:~$ cat file.txt
This is a new text file created with the vi text editor.

Above this line, is a blank line.

Lets check out some of the other things we can do with the vi editor.
vagrant@homestead:~$

Command Mode

One thing to be aware of, is that vi starts in command mode. If you’ve ever opened vi before and found that nothing works, or that you couldn’t even type any text, you were likely stuck in command mode without realizing it. It is things like this that push newcomers to vi away, it seems like the thing doesn’t even work! Command mode however is a way to allow quick access to all the important commands you can apply to files such as write, quit, and so on.

Insert Mode

Insert mode on the other hand is needed so that you can begin to actually type some text into the file. We can enter into insert mode easily by hitting the i key. This is what we did in the example above in order to type out the simple example text. To leave insert mode, you simply hit the esc key which brings you back to command mode. This is a unique aspect of vi and as you use the software more, switching between insert and command mode will be second nature.

Navigating The File With vi

Remember, with vi you won’t have the convenience of using a mouse to move the cursor around the file. You do however have very quick ways to navigate using the keyboard. In command mode, you can navigate the file with the keys on the keyboard of h j k l. h takes the cursor left, j moves the cursor down. k moves the cursor up. l moves the cursor right (only in technology would you find this!). The same effect can be used by pressing the left, down, up, and right arrow keys as well. While in insert mode, you can navigate using the arrow keys however the h j k and l will operate normally to insert those characters into the text.


Edit Files With vi

Ok, now that we have some basic concepts down, we can actually edit and work with a file to learn some of the commands to do so. So far, we just need to remember the difference between command mode and insert mode and what each is for. You are likely familiar with the concepts of cut, copy, paste, and the clipboard. Sure, these are the things you use everyday to move text around with ease using the mouse. In vi, you can do all of these things as well, they simply go by different names.

register

In Linux and vi, the idea of a clipboard is called the register. There really isn’t much more to know beyond that. Just understand that if you copy or cut information, it goes to the register in vi.

yank

The yank concept is the equivalent of the copy command in the windows world. You can yank in several ways such as to yank the current line with yy, yank a word with yw, yank a single character with yl, or yank multiple lines with something like 4yy. This would yank 4 lines. So let’s test a few of these out.

vagrant@homestead:~$ cat file.txt
This is a new text file created with the vi text editor.
This is a new text file created with the vi text editor.

Above this line, is a blank line.
Above

Lets check out some of the other things we can do with the vi editor.

This is some text
which is broken across
multiple lines so
we can test the multi
line yank with something
like 6yy
This is some text
which is broken across
multiple lines so
we can test the multi
line yank with something
like 6yy
vagrant@homestead:~$

This is a simple example of using yy, yw, 6yy, and p to yank various amounts of text to the register, and then put the resulting characters back into the file. We did not mention p yet, but what it does is to put any contents in the register into the file.

delete with vi

You’re in luck my friend. If you’ve become familiar with the y command and the various combinations of how to use it, you already know how to use the d command for delete. In other words, all of the combinations that we used for y such as yanking a whole line with yy, or yanking a single character with yl, yanking a word with yw, or yanking multiple lines with nyy, can be directly applied to d delete. All you do is swap out any instance of y for d and now you are not simply yanking, but deleting the text being worked on. You can use this in two ways. First, you can use it to simply eliminate any characters you would like to from the file, or second, you can use this to move letters, words, or lines, to different areas in the file. This is because when you use d, it places the deleted contents in the register, so you can easily use the p command to put those contents where ever you’d like.


Searching With vi

Let’s say you’re examining a large configuration file, and not just some basic text like you have here. Well in a case like this, it is going to be helpful to be able to quickly search within the file. We can do this with vi by using special characters in command mode. The forward slash / allows you to search forward. So just as a really basic example, let’s say you need to search for the letter a. You would type /a in command mode and hit enter. It stands to reason that the letter a may occur many times in a text file. If you would like to find the next occurrence of the search just hit the n key. If you’d like to find the previous occurrence simply type capital N. It’s a really quick way to move through the file. To begin with searching backwards you would use ? instead of / as in ?a for this example.

Execute Mode

Some would refer to this as a whole separate mode, but really it’s just a subsection of the command mode. You can enter this state by typing the colon : while in command mode. This will create a prompt at the bottom of the editor beginning with :. It is from this mode where you can save or write changes to a file. You can also quit without saving and so on. Once you’re at the : prompt, you’ll be typing things like w, wq, q! and so on.

vi Summary

Now that we have run through the very basics of using vi with Linux, let’s assemble our own summary of commonly used commands. It helps to write these out as they’ll stick in your memory easier.

/

while in command mode, entering / will start a forward search

?

if you would rather begin a backwards search, you can do so with ?

n

once you are searching, n will take you to the very next occurrence

N

to find the prior occurrence, or search backwards, you can use N

h, j, k, l,

h takes you left, j moves down, k goes up, and l is for moving right

i

move to insert mode at the current character

a

enter insert mode after the current character

o

move to insert mode on the next line

<esc>

return to command mode from insert mode

cl, cw change a letter or word
dl, dw delete a letter or word
yl, yw yank a letter or word
cc change an entire line
dd delete an entire line
yy yank an entire line
p put the contents of the register into the file
:e! revert back to the original file
:wq or ZZ write or save your changes, and quit the editor
:w! write to a file even if it is read only – careful with this
:q! quit without saving

Vi Editor Tutorial Conclusion

Learning vi is a bit of a rite of passage into the world of Linux. This vi editor tutorial for beginners covers only the tip of the iceberg with regard to what is possible with vi. Once you have the basics down, you can move on to macros, text formatting, source code formatting, and much more.