Handy VI Editor Tips for the Beginner

Share this article

When I was first introduced to the command line on Unix in the mid-1990’s, I have to admit I was intimidated. I had already been using DOS for some time, but only in a operative sense day to day.

Suddenly I was faced with learning to administer an entire system from a shell prompt. In particular, as I began to learn the ins and outs of using the VI editor, things became quite a bit easier to handle.

VI is launched from the command line and can be used by privileged and non-privileged users alike. In the web development arena, using a screen-based editor like VI can be handy for editing httpd.conf files, modifying firewall rules, quick edits to configuration files and of course work with html or scripting pages such as Perl, PHP and more.

Some have found VI to be more effective for their work then visual editors. I tend to use both, however, VI is fully functional and can be used for the complete editing process, from creating new files to later edits. For advanced files that will need extensive editing, I still fall back on my BBEdit text editor on OS X. However, having the Terminal a click away for local or remote editing is handy!

VI has two modes, command and insert. Command mode alows for command input to move around a file and search text. Insert mode allows complete editing control and inputs keyboard entry into the file. I have included a few of my most used commands when editing files.

Let’s look at httpd.conf for an example. Since this is a restricted file, if you open httpd.conf as a non-privileged user, it will be in read-only mode. So for edits, you must be privileged as root (or whatever user you have set for this purpose).

To open the file, issuing ‘vi /etc/httpd/conf/httpd.conf’ will do the trick. Once in the file, there are a few tools to use.

In command mode, typing enters commands. For example, using / tells VI to expect to search for something. So to find all entries in the file related to SSL dynamic loadable modules, I could enter ‘/HAVE_SSL’ and press enter.

VI goes to the first hit for this search term. To move to the next entry and on through the file, simply press the N key (for next).

To move to a specific line number, I can use the colon character and enter a number, such as :900 — which goes to line 900 of the file. This is very useful when chasing down errors that reference the line number in a file.

There are some handy commands for deleting text if you need to remove strings (words), lines or paragraphs. Here are some common deletion keystrokes:

  • delete current word –> dw
  • delete previous word –> db
  • delete entire line –> dd
  • delete paragraph –> d}

One of the advantages to deletion, is you can use it to cut and paste text. For instance, if you had deleted a paragraph which you wanted move from line 10 to line 1500 of a file, you could do the following:

Position your cursor in front of the paragraph, and type d}. The text disappears. Then type in :1500, which moves you to line 1500 in the file. Simply typing “p” will paste this deleted text in the new location.

If changes are necessary, such as adding a new virtual server entry in the httpd.conf file, one needs to move to insert mode. This is accomplished by either pressing the Insert key or the I key on the keyboard.

Now any keyboard entry is added to the file, so be careful. You can move around the file with the arrow keys, type input into the file and use the delete or backspace keys as in a visual text editor. To exit insert mode after your changes are made, press the escape key (esc) and you are back in command mode.

If you want to save your changes and exit, type :wq and press enter. To exit without saving changes (perhaps a mistake was made), press :q!, which overrides edits made in insert mode. Finally, to simply quit having not made any changes, use :q.

Blane WarreneBlane Warrene
View Author
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week