How to use vi to edit files in Linux

Here’s a brief reminder on how you can edit files with vi directly from the command line. Text is all you get, no other visual clues or menus are included, and best of all you need to know each keyboard shortcut once you’ve entered it.

vi is an old editor going back all the way to 1976. It’s not the most comfortable one, but the beauty is that it’s available on pretty much every distribution. There are several other common editors out there, but it’s not always an option to install those just for a quick file edit.

In this article I’ll show you the most commonly used commands and shortcuts, help you delete old versions of a file and show you a couple of links to more in-depth sources.

I assume here that you are comfortable using the command line interface (CLI).

Getting Started

Invoking vi is pretty straightforward. All you do is type

vi yourfile

That’ll bring up the interface ready for you to use. If the file does not exist, vi will create it for you if you add content to it and save it later.

You can also prefix your file name with a path and an extension of course:

vi /yourdirectory/yourfile.txt

Here' I'm editing the license.txt file that comes with WordPress

Editing

Once your displaying content, you can navigate around the document just with your cursor keys or some other handy shortcuts outlined in this handy cheat sheet.

You can invoke editing mode by pressing the a button – notice the word INSERT being displayed at the bottom of your window. Now you can add and delete text to your heart’s content.

Notice that there’s no option to “mark” or “highlight” a text passage. It’s not that advanced. It’ll get you by if all you need is to change a config file though.

Once you’re done editing, hit the ESCAPE key and you’ll go back to navigation mode.

vi in editing mode. As you can see, the interface never really changes.

Exiting vi and saving your file

Now that you’re out of EDITION mode, press SHIFT-Z twice (ZZ) to save your file. If no changes have been made your file won’t be saved. However if you’ve made changes then they WILL be saved – even though you may not want that.

If you don’t want your changes to be saved, the only way to leave vi is by typing :q! (I know… don’t ask)

Troubleshooting

Sometimes – by accident or deliberation – it can happen that your SSH connection gets closed before vi can write to an amended file properly. Or, another user may be editing the same file as you.

To make sure that in such cases no file corruption occurs, vi actually writes all changes to a temp file before overwriting the existing file. You may encounter a message like this:

vi Attention Seeker Message

This means that a temp file has been found and vi needs you to tell him what to do. You have several options here:

  • hit q or a to exit the session
  • hit o to just look at  the file but not make any changes to it
  • hit e to ignore this message and start editing the file (in which case previous edits to the temp file will be overwritten)
  • hit r to recover previous edits
  • hit d to delete the temp file (doesn’t always work)

If you persistently get this message, look out for a hidden file called ending with .swp

You can check if there are any in your directory by using the ls -a command.

Further Reading

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or drawing on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.