Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd please sign up again. Thanks!

  1. Get $5 off your first Lyft ride while helping us pay for hosting Linux.org! --> $5 Lyft
    Dismiss Notice

Using vi/vim

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

  1. Rob

    Rob Administrator
    Staff Member

    Joined:
    Oct 27, 2011
    Messages:
    176
    Likes Received:
    488
    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

    /nonplussed

    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.
     
  2. TCRatius

    TCRatius New Member

    Joined:
    Aug 30, 2017
    Messages:
    11
    Likes Received:
    16
    Thanks for that, I was using golang go and vi was recommended for editing the files and I had no clue. Certainly has help me out. I am curious though, when I press shift + esc + q = Entering Ex mode. Type "visual" to go to Normal mode. Then I can type q! Is that how I am meant to do it?
     
  3. JasKinasis

    JasKinasis Active Member

    Joined:
    Apr 25, 2017
    Messages:
    96
    Likes Received:
    144
    Robs article in this thread is quite old. I think this might refer to the original version of vi.

    The main version of vi which is pre-installed on most Linux distros nowadays is actually a minimal version of vim, which runs in vi compatibility mode.

    Now, I don't know about the original versions of vi, but in the vim based vi you need to include a colon : in the quit commands.
    e.g.
    [ESC]:q![ENTER]

    It might be an error in Robs post, or it could just be that the original version of vi didn't require the colon! IDK. :/ Either way, the vim-based version of vi that most distros have installed nowadays requires a colon : to prepend commands.

    Quitting vi/vim:
    You can switch to EX mode and then issue one of the many quit commands to exit vim. But you don't need to.

    To quit vi/vim without saving any changes to the current file/buffer you can simply use:
    [ESC]:q![ENTER]
    note: NOT [ESC]q! as per the article above

    There are several other ways to quit vi/vim:
    [ESC]:q[ENTER]
    - This will only allow you to quit vim if you have not made any edits to the current buffer/file. Otherwise, you should use :q! to discard changes, or use one of the following commands that allow you to save and quit:

    [ESC]:wq[ENTER]
    - This writes the current buffer to file and quits, but if the file-name has not been set you will have to use [ESC]:w {filename}[ENTER] and then :q[ENTER]

    Alternatively:
    [ESC]:x[ENTER]
    - This is a great one, because it will quit vi/vim and will automatically save the file if any changes were made to it. This command will only fail to exit vim if the current buffer/file does not have a file-name set. In which case you would need to use:
    [ESC]:x {filename}[ENTER]


    More on vi/vim's modes:
    Vim is what is called a modal editor (Something that Rob neglected to mention in his article). It has two main modes:
    Command mode - for entering commands and cursor movement
    and Edit mode - which is for actually editing text.
    There are also additional modes for text selection (visual, visual line, visual block) and EX mode, which is basically command mode on steroids.

    The bottom-left of vims status bar will tell you which mode vim is currently in. If it is empty, you are in command mode. Otherwise the mode will be listed there.

    Also, because vim is modal - if you are already in command mode - you don't need to worry about the leading [ESC] key-presses. [ESC] is used to switch back into command mode from any other mode. So if you can see that you are already in command mode, you only need to use the colon key : to prefix commands.

    EX mode turns vim into a line-based editor (like ed). It's like a permanent command mode, that can only be exited by entering the "visual" command. So pressing [ESC] will not exit EX mode. Entering the "visual" command will switch you back to the normal command mode.
    EX mode isn't usually used much. The only time you would really use EX mode is if you had a sequence of operations that you wanted to perform on a file (or even simultaneously on several files).

    e.g.
    You want to perform a sequence of search/replace operations on a file (or several) - You could switch to EX mode and then enter all of your commands one after the other, without having to switch modes or prepend commands with the colon key. Allowing you to quickly make large edits to one or more files....

    BTW: You can also switch to EX mode using gQ {NOTE: no colon is required for this one!}

    Also, rather than using vi (which nowadays is a minimal version of vim running in vi compatibility mode) - I recommend installing and using the full version of vim, which has a lot of improvements over the original vi editor!

    I hope this is of some help!

    [edit]
    Also - after installing vim, try opening up a terminal and run the command:
    vimtutor

    This will open a tutorial in vim which you can work through and will quickly get you up to speed with the basics of using vi/vim.
    [/edit]
     
    #3 JasKinasis, Nov 9, 2017
    Last edited: Nov 9, 2017
    wizardfromoz and atanere like this.

Share This Page