Using vi/vim

Discussion in 'Intermediate Tutorials' started by Rob, Jul 9, 2013.

  1. Rob

    Rob Administrator Staff Member

    Oct 27, 2011
    Likes Received:
    VI is probably the most popular text editor for Linux. Even if you don't like it, you may end up using it quite often. If you need to make a quick change to a file, you can't beat 'vi'. This is not meant to be an exhaustive guide to vi. This is just meant to show you how to use the most common (and useful) commands.

    Sometimes you need vi

    I had an unpleasant surprise once. A friend of mine who had installed Linux had somehow changed the default editor from vi to joe. He called to tell me that his crontab entries didn't do anything. One more reason to get to know vi. Crontab is designed for vi and may not work if you use certain alternative editors.

    vi basics

    Open file with vi

    vi /etc/hosts.allow

    Of course, just opening the file gets you nowhere, unless you only want to read it. That can be just as easily done with less, so we'd better look at what to do with 'vi' beyond just opening the file.

    Adding text

    To add text, you would type the following:

    ESC + i

    (i for insert)

    And there you have it. Now you can type away. Of course, it doesn't really do us too much good if you're not able to save what you've typed. So let's save it, shall we?

    Saving the file

    ESC + w

    (w for write)

    Closing the file

    Now, to finish you would type the following:

    ESC + q

    (q for quit)

    Of course, there is a lot more that you may need to do. Let's have a look.

    Vi for budding power users

    Again, my aim is not to do into a treatise on vi, but here are a few more commands that you might need to do a little more heavy lifting.

    Removing Lines

    You may find that you need to remove an entire line from a file. Just place the cursor at the beginning of the line and type:

    ESC + d

    (d for delete)

    Changing your mind

    Sometimes you wish you hadn't done something. With vi, you can undo what you just did.

    ESC + u

    (u for undo)

    Changing your mind (again)

    Again, you have second thoughts. With vi, there are no regrets.

    ESC + q!

    (q! for quit and I *really* mean it!)

    The exclamation point (!) is used in vi to override default settings. If you make changes to a file, vi is going to ask you if you want to save them. 'q!' means that you want to quit without saving.

    Where did I put that word?

    Did you misplace a word in your file? You can find it easily with vi

    ESC + /word

    If you're looking for the word nonplussed in your text (as in: 'vi is so easy I am nonplussed') you would type:

    ESC /nonplussed

    and it will find every instance of the word nonplussed.

    Can I change that word?

    Maybe you don't want to use the word nonplussed. Perhaps it's better to use a more well-known word. You can use vi to change the word

    First you could use the


    to look for that word. When you find it, you would then type

    ESC : s/nonplussed/amazed/

    to replace the word on that line.

    If you were sure that you wanted to replace all instances of that word in the whole text, you could type this

    ESC :%s/nonplussed/amazed/g

    and nonplussed would be changed to amazed throughout the text.

    If you want to get some control over what you replace - that is you want to used both nonplussed and amazed, then you would add gc to the end:

    ESC :%s/nonplussed/amazed/g

    Vi will now ask you for confirmation.

    Vi configuration settings

    There are some basic vi configuration setting that you should be aware of if you want to use this text editor comfortably.

    Word WrappingIf you don't want your words running off the page into oblivion, you can set the word wrapping feature in vi

    ESC : set wm=30

    This is a good wrap for me personally. It sets the wrap to 30 spaces from the right so it makes it tight. This means that the bigger the number, the sooner you get a line break. If you send something via email, then this tight wrap ensures that it will get there without the lines being broken into stair-steps.

    Vi as an email editor

    What did you say? Vi and email? Yes! You can use vi as an email editor. This is most commonly done with the email client mutt.

    More Vi

    This just scratches the surface of what you can do with vi. Here I've tried to show you what might be the most useful features and setting of vi. It should be enough to get some basic work done with this ubiquitous editor for Linux.
    Vitor Augusto M. Pio, Bo$$ and MikeyD like this.
  2. MikeyD

    MikeyD Active Member

    Jul 23, 2013
    Likes Received:
    Great post Rob. I love Vi/Vim as once you learn the commands it becomes as (if not more) efficient than many major IDEs for writing and editing scripts, Linux files, C programs, etc.
    A lot of other Linux programs use Vi commands as well like 'less'/'more' and like you said, 'mutt'

    I would also recommend anyone interested in gaining more hands-on knowledge to check out vimtutor
    If you have vim installed (doesn't apply to vi sadly) just type 'vimtutor' at your command-line and it should come up. It usually takes 30 mins at most to go through and will give you a hands-on tutorial of many of the basic Vi/Vim functions
    ZHANG/ZHIKUN and Rob like this.
  3. Saptarshi Nag

    Saptarshi Nag Member

    Jan 26, 2014
    Likes Received:
    Rob,you have said to save the file one have to give Esc+w and to quit Esc+q but i think it is done by giving :+w and :+q,i.e.,in last line mode.So please clarify it.
    Vinay Gupta likes this.
  4. plotozoidz

    plotozoidz New Member

    Mar 21, 2014
    Likes Received:
    I find ESC + o handy to use as an alternative to using ESC + i

    ESC + i will enter Insert mode on a line at the cursor position.
    ESC + o will enter Insert mode, but at start of a new line opened below cursor.
    ESC + O will enter Insert mode, but at start of a new line opened above cursor.

    Struggling to get your head around Vi? If you web search 'vi in 10 minutes' you will find a two page guide that i found helped me!

    (i will edit this post, to provide link when i get full member link privileges)
    Last edited: Mar 23, 2014
  5. Eric Z. Ma

    Eric Z. Ma New Member

    Apr 23, 2014
    Likes Received:
    ESC is too far away from my fingers. I almost always use Ctrl+[ instead. It need my 2 hands to work together but is much easier and faster.
    ZHANG/ZHIKUN likes this.
  6. TheShellWave

    TheShellWave New Member

    Oct 2, 2013
    Likes Received:
    I've been using VI since I got involved in UNIX in 1990's. I still use it for both development and system administration.
  7. VitalkaDrug

    VitalkaDrug Member

    Oct 11, 2013
    Likes Received:
  8. Alvin Lazar.C

    Alvin Lazar.C New Member

    Jun 8, 2014
    Likes Received:
    thanks lot friends.... to know new thing☺☺

Share This Page