Basics of Linux Vi Editor

Linux Vi editor is a powerful and versatile text editor. It does have something of a learning curve that you might find initially disconcerting, but in time you will find that it gets under your skin, so to speak, and you find yourself using the working methods it gives you elsewhere. Vi will definitely repay your time and attention. It’s a very powerful tool with many features, and we can’t hope to cover everything that it can do here, so here’s an overview.

Vi, Command Line Editor

Vi is a command line text editor. The command line behaves differently to your GUI. It’s a single window that only has text input and output. Vi was created to work within these constraints, and many would say that that’s actually been instrumental in making it so powerful. It’s a plain text editor that resembles Notepad on Windows, or Textedit on Mac. That means that it doesn’t have quite the same amount of word processing horsepower as programs like Word or Pages. It is more capable than Notepad or Textedit though.

You can send your mouse on holiday because everything in Vi is done using the keyboard.

Insert (or Input) mode and Edit mode are Vi’s two modes. Input mode lets you enter content into a file. Edit lets you delete, copy, search and replace. Users often mistakenly start typing commands without first going back into edit mode, or they start typing in text without going into insert mode. It’s fairly easy to put these mistakes right though.

Here’s what it looks like when you want to open file:

vi <file>

If you forget to name the file you want to open then the easiest thing to do is just close vi down and try again. When you specify the file, remember it can be with either an absolute or relative path. Okay, time to start typing! Let’s go into the directory where your files are kept and edit our first file.

vi myfile

This command opens up the file. If the file doesn’t exist yet, then it will create it for you and open it up. (Once you enter vi it will look something like this (though it can look a bit different according to what system you’re running it on).

Edit mode is always the default mode to start with, so change to insert mode by pressing i. Any time you want to know what mode you’re in currently, just look in the bottom left corner. Now add a few lines of text and press Esc which will return you to edit mode.

Vi Editor – Save and Exit

There are a few ways to do this which order the same thing, so choose the one that suits you best (and don’t forget to ensure that you’re in edit mode first.)

If you are unsure if you are in edit mode or not you can look at the bottom left corner. It will always tell you. If it doesn’t say INSERT then you are good to go. Or you can just press Esc to be certain. If you are already in edit mode, pressing Esc doesn’t do anything so you won’t be doing any harm.

  • ZZ (Note: capitals) – Save and exit
  • :q! – Get rid of all changes since the last save, and then exit
  • :w – save file but don’t exit
  • :wq – once more, save and exit

Most commands in vi are performed as soon as you hit a sequence of keys. If a command starts with a colon ( : ) then you need to tap <enter> to complete it. Save and exit the file you have open right now

Other ways to look at files

Vi lets us edit files, but we can view them as well. The first of two convenient commands that help us do that are  cat (short for concatenate)—which joins files together but can simply be used to just view files.

cat <file>

If you use cat, giving it a single command line argument which is the file we just created, you will see the contents of that file shown on screen, and then the prompt.

If you mistakenly run cat without giving it a command line argument, then you’ll see that the cursor advances to the next line and nothing happens. As we didn’t specify a file, cat reads from something called STDIN instead which defaults to the keyboard. If you type something, followed by <enter> you will see cat mirror your input to the screen. To get out of this you can press <Ctrl> + c which is the universal combination for Cancel in Linux.

In fact, whenever you get in trouble you can generally press <Ctrl> + c to get yourself out of trouble.

This this works fine with viewing small files, but with big ones it will be hard to see all the content as it zips across the screen. The only viewable part will be the last bit. For these files we use a command called less.

less <file>

It’s great because it lets you use the arrow keys to move up and down within a file, the SpaceBar to jump forward a page, and b to jump back a page. Press q to quit when you’re finished.

Have a look at the file you just created now using both these commands.

Navigating a file in Vi Editor

Now we can go back into the file that we just created and add some additional content. Enter insert mode and use the arrow keys to move the cursor around. Add a couple or more paragraphs of content then tap Esc to return to edit mode.

Here’s a list of some of the many commands you can enter to move around the file. It’s worth spending some time trying them out to see how they work.

  • Arrow keys – move the cursor around
  • j, k, h, l – move the cursor down, up, left and right (like the arrow keys)
  • ^ (caret) – move cursor to start of current line
  • $ – move cursor to end of the current line
  • nG – move to the nth line (eg 5G moves to 5th line)
  • G – move to the last line
  • w – move to the beginning of the next word
  • nw – move forward n word (eg 3w moves three wrds forwards)
  • b – move to the start of the last word
  • nb – move back n word
  • { – move back one paragraph
  • } – move forward one paragraph

If you type :set nu in edit mode in vi it will enable line numbers. You might find that doing this makes it much easier to work with files.

Deleting content in Vi Editor

Deleting is not too different from movement, and there’s a few delete commands which will let us use a movement command to define what is going to be deleted.

Here are a few of the ways that you can delete content in vi. You can test a few out now. (If you want to undo anything you’ve done then check out the delete section further down.)

  • x – delete one character
  • nx – delete n characters (eg 4x deletes for characters)
  • dd – delete the present line
  • dn – d followed by a movement command. Delete to where the movement command would have taken you. (eg d6w means delete 6 words)

Undoing the changes

Undoing the things you change in vi is relatively simple.

  • u – Undo the last action (keep hitting u to carry on undoing)
  • U (Note: capital) – Undo all changes to the current line

Conclusion

We can now insert content into a file, move the file around, remove content and undo it then save and exit. You can now do basic editing in vi. This is only just start what vi can do though. You can find a lot more by searching for vi online. There are lots of vi cheat sheets available so you can take your time in learning all of the various commands and concepts. Enjoy your explorations within the interesting world of Linux vi editor. There’s no doubt that it will be a difficult journey to start with, but as you get used to it, you’ll wonder what you ever did without it.

No comment yet, add your voice below!

Add a Comment

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

GET LATEST NEWS AND TIPS

  • Yes, please, I agree to receiving my personal Plesk Newsletter! WebPros International GmbH and other WebPros group companies may store and process the data I provide for the purpose of delivering the newsletter according to the WebPros Privacy Policy. In order to tailor its offerings to me, Plesk may further use additional information like usage and behavior data (Profiling). I can unsubscribe from the newsletter at any time by sending an email to [email protected] or use the unsubscribe link in any of the newsletters.

  • Hidden
  • Hidden
  • Hidden
  • Hidden
  • Hidden
  • Hidden

Related Posts

Knowledge Base