Handy VI Editor Tips for the Beginner

By Blane Warrene
We teamed up with SiteGround
To bring you the latest from the web and tried-and-true hosting, recommended for designers and developers. SitePoint Readers Get Up To 65% OFF Now

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.

We teamed up with SiteGround
To bring you the latest from the web and tried-and-true hosting, recommended for designers and developers. SitePoint Readers Get Up To 65% OFF Now
  • Another trick :
    When I started learning vi and did some mistake (eventually deleting important lines), it took me some time to learn this..
    use :q! to quit witout saving ;)

  • craig34

    I just don’t get VI, or people who use it? I can’t for the life of me find even one real advantage (besides working with a live file – which could be a disadvantage) for using VI over using Homesite.

  • That’s not an uncommon reaction, vi is pretty counter-intuitive. However for those of us who:

    – regularly have to administer remote servers
    – work in a Linux development environment
    – want our editor to be as powerful as the programming languages we edit
    – are a bit odd-in-the-head

    then vi is priceless.

    If your needs are basic enough to be satisfied with a GUI editor and only a GUI editor, then you’re lucky, quite frankly ;-)

  • cholmon

    like pointbeing said, the ability to run it from a remote server with nothing more than an ssh client is a huge advantage. also, it is much more lightweight than homesite, so it takes no time at all to load up. of course, it doesn’t have all the cool project and deployment features that homesite does. basically, they’re two very different tools that have a small amount of functional overlap.

  • Additionally, though some tasks are difficult where a simple mouse can be handly there are some tricks in vi that most editors can’t do or take to long to do for instance search and replace using regex.

    Split screens also tend to come in handy when comparing code, and the ability to fold paragraphs. Why see a block of code when you don’t have to.

    I see the positives of both. These are just a few more examples.

  • a personal favorite–change dos to unix line endings inside of vi:


  • alanr

    As a basic text editor vi will seem overly hard to use. But it is truly the most powerful editor I have ever come across. For those familiar with it, using any GUI alternative knocks back productivity.

    A couple of tips some might find useful:
    Use of the exclamation mark to run a shell within vi. For example mark the end of an area you want to sort (say mb

  • Anonymous

    Another important reason for any serious developer to learn vi is that it comes with every single installation of any flavor of Linix/Unix going back for decades (though specific functions will vary). This will save your hide at least once, so print out a vi cheat sheet and keep it with you. For those who have control over a system or will have frequent need of a text editor, you can also try pico or joe. They are much more intutive. Some other vi tricks:

    u = undo
    yy = ‘yank’ which is the equivalent of copy
    p = paste
    type a number before any command will do that command that number of times, so

    22yy will copy 22 lines, starting where your cursor is. This is useful for making copies of a config

    If you know what a regular expression is, vi has full native support for them. Here’s a very simple search and replace command. First, hit your escape key (you know a true vi user if they hit their escape key constantly):

    : 1,$ s/this phrase/that phrase/g

    this will start on the first line of the file and replace “this phrase” with “that phrase” every single time until the end of the file ($). If you need to use / or ” somewhere in there, put a backslash before it. Happy editing.

  • George

    For those who don’t see the point of vi, try editing a system file when booting from an Unix boot disk. Vi is very small and has some very useful comments.
    Vi is a wonderful editor. Its successor vim is also very nice. It has some additional functionality that makes coding easier because it highlights key words.

  • Anonymous

    How can we delete specific lines together randomly distributed in the text file starting with a particular word or character using vi editor?