• ARTICLES
SEARCH

How-To Geek

A Beginner’s Guide to Editing Text Files With Vi

Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you’ll have to edit a text file on a system that doesn’t include a friendlier text editor, so knowing Vi is essential.

Unlike Nano, an easy-to-use terminal text editor, Vi doesn’t hold your hand and provide a list of keyboard shortcuts on the screen. It’s a modal text editor, and it has both an insert and command mode.

Getting Started

Vi is a terminal application, so you’ll have to start it from a terminal window. Use the vi /path/to/file command to open an existing file with Vi. The vi /path/to/file command also works if the file doesn’t exist yet; Vi will create a new file and write it to the specified location when you save.

Remember to use sudo if you want to edit a system file. So, for example, you’d type sudo vi /etc/fstab if you wanted to edit your fstab file. Use the su command instead if you’re using a non-Ubuntu version of Linux that doesn’t use sudo.

Command Mode

This is what you’ll see when you open a file in vi. It looks like you can just start typing, but you can’t. Vi is a modal text editor, and it opens in command mode. Trying to type at this screen will result in unexpected behavior.

While in command mode, you can move the cursor around with the arrow keys. Press the x key to delete the character under the cursor. There are a variety of other delete commands — for example, typing dd (press the d key twice) deletes an entire line of text.

You can select, copy, cut and paste text in command mode. Position the cursor at the left or right side of the text you want to copy and press the v key. Move your cursor to select text, and then press y to copy the selected text or x to cut it. Position your cursor at the desired location and press the p key to paste the text you copied or cut.

Insert Mode

Aside from command mode, the other mode you need to know about is insert mode, which allows you to insert text in Vi. Entering insert mode is easy once you know it exists — just press the i key once after you’ve positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands.

Once you’re done in insert mode, press the escape key to return to command mode.

Saving and Quitting

You can save and quit vi from command mode. First, ensure you’re in command mode by pressing the escape key (pressing the escape key again does nothing if you’re already in command mode.)

Type :wq and press enter to write the file to disk and quit vi. You can also split this command up — for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.

Vi won’t let you quit if you’ve modified the file since you last saved, but you can type :q! and press enter to ignore this warning.


Check out Nano if you’re looking for an easier-to-use terminal text editor. Most Linux distributions come with Nano installed, but embedded systems and other stripped-down environments often only include Vi.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Google+.

  • Published 01/11/12

Comments (14)

  1. TheFu

    Ok, I’m a Linux/UNIX lover. I use vi daily. It is my preferred quick-n-dirty editor regardless of platform, MS-Windows included. But it wasn’t always this way. I used to hate vi too.

    What changed my mind?

    I didn’t have any other choice. A brand new workstation arrived on my desk around 1993, it was a DEC Alpha with a pretty small HDD – 2GB. Nano didn’t edit. I didn’t have root, so installing something else wasn’t an option. All my peers used vi – I was working inside a government lab where we had at least 1 of everything. This “Alpha” was the first 64-bit machine in the building. None of the other systems programs were binary compatible, so if I wanted any program, I had to convince the system administrator to install it. Our admin was like GOD – he was too busy to answer emails. I begged for emacs. BEGGED. Nope.

    So, after 6 months I gave up. I’d been editing files in emacs on different systems and moving the files via NFS back to the Alpha. By that time, my vi skills had become workable, not great, but I could do the job. vi is available on almost every single computer made and it works the same way on all of them. It is the most powerful editor that is universally available. Notepad/nano are pretty worthless in comparison, IMHO.

    For longer, programming sessions, I’ll use geany or whatever Eclipse calls their editor, but vi is where I spend most of my time editing.

    Ok, jump forward to this morning as I edit this comment. I’m so used to editing with vi, that I find myself going back and removing jjj/kkk and other cursor moving keys from everything I edit when I’m not allowed to use VI. To move around quicker, I’ll use a ‘search’ to get me exactly to the place I want with 2 or 3 characters – faster than looking for the mouse – much faster.

    Advice to anyone needing or wanting to learn VI?

    * Do yourself a favor – sudo apt-get remove nano
    * Get over all other editors, put your fingers on the home row and begin computing.
    * Use vi alone, no other editor for at least a week.
    * Being a touch typist helps too.
    * Oh and stop wasting your time using the mouse. That just slows you down.

  2. gyffes

    orrrrr

    sudo apt-get install nano

    wooo!

  3. tpro

    @TheFu I don’t agree with the last line…

  4. clakes

    @TheFu – can you expand more on the ‘search’ you use? sometimes i have to edit dns records in vi and i have to scroll through the whole file for the record i need.

  5. TheFu

    RTFM – I haven’t used that in years. ;) man pages are a fantastic aid.

    man vi
    ….
    +/{pat} For the first file the cursor will be positioned on the
    first occurrence of {pat}. See “:help search-pattern” for
    the available search patterns.

    regex is a magical thing. The great thing is that once you learn to use vi on 1 system, it works exactly the same on every other system, including embedded ones.

    @gyffes:
    # nano
    -sh: nano: not found

    That machine doesn’t have nano, no package manager, and is deployed all over the world. To change things on it, you **must** know basic vi.

    There are lots of other, more advanced things in the build-in help.

    Editing Effectively ~
    |usr_20.txt| Typing command-line commands quickly
    |usr_21.txt| Go away and come back
    |usr_22.txt| Finding the file to edit
    |usr_23.txt| Editing other files
    |usr_24.txt| Inserting quickly
    |usr_25.txt| Editing formatted text
    |usr_26.txt| Repeating
    |usr_27.txt| Search commands and patterns
    |usr_28.txt| Folding
    |usr_29.txt| Moving through programs
    |usr_30.txt| Editing programs
    |usr_31.txt| Exploiting the GUI
    |usr_32.txt| The undo tree

    Teh help is all already on your Linux system. Use it.

  6. arukaen

    or sudo apt-get install vim; yum install vim

    VIM/VI are best compared to nano

  7. Jim

    I have a long history of using ‘vi’ in unix Actually, my history goes back to ‘ex’, the predecessor to ‘vi’, when I was at Berkeley, and ‘vi’ was an incredible upgrade because it was WYSIWYG – taken for granted in any editor nowdays… ‘vi’ is an excellent application, IMO, largely because it is pretty easy to learn but is very powerful when you decide to learn more of its capabilities.
    I am replying here, not to reminisce about the good old days at Cal, but to let ‘vi’ users know that this great text editor is also available on Windows platforms! When I was forced to use a PC for the non-engineering aspects of my job (mainly sharing a calendar across the lab), I immediately looked for a text editor that was as capable as ‘vi’ (which, by then, I had highly customized above and beyond its already rich features). Happily, I found ‘gvim’ (at http://www.vim.org) which is an open source project. The ‘vim’ editor is considerably customized so that responds to mouse and to most of the keyboard actions that ‘word’ (or ‘notepad’ or other Windows text editors) respond to, while still retaining all the ‘vi’ behaviors as well.
    Of course, to a unix user (I use ‘cygwin’ [also open source] when on my PC), the fact that the file you produce with ‘vi’ is “pure text” and can be processed and filtered with the rich set of unix applications is a huge advantage over a ‘word’ document with its proprietary file format.
    So, if you like ‘vi’ but work on Windows, give ‘vim’ a try!

  8. Fe

    1st, @TheFu, I also don’t agree with the last line.

    2nd; Thanks a lot Chris for the article. I’ve started learning Perl and needed to know how to use vi via the SSH-Linux command interface. I’ve been having problems with saving the file once I have opened it and edited with vi. I know this’ll help a lot.

    3rd; My SSH-Linux doesn’t understand normal keyboard functions like UP, DOWN, RIGHT, LEFT navigation keys and instead displays ^[[A^[[B^[[C^[[D^C. I managed to set the backspace function in the keyboard option under settings. Is it my SSH installation or is there a way I can set these functions???

    REGARDS!

  9. Mark

    vi is a toy editor. emacs is the real deal.

  10. Chris Hoffman

    @Mark

    I knew we’d have an emacs vs vi comment at some point!

  11. Robert Hinson

    There is gvim for the graphical version.

  12. vimmaniac

    This is the first thing you should have put in the .vimrc file:

    set nocompatible

    Use vim not vi.

  13. dtigue

    Emacs is for children, vi is for babies! Joe is the best out there. or nano, or ……

    BOO! No one cares which you think is better. Your comment doesn’t do anything for the people that read the article, other than make them think you are a moron. If you think Emacs is better than great. Emacs has nothing to do with this article. The article is about vi. If you think nano is better, so what. Nano has nothing to do with vi. A better comment would be something like, ” In vi do _____ to be able to do ____ effectively.” Now that helps the article. It gives extra tips to the readers who OBVIOUSLY wanted to read up on vi and maybe learn something about it. It also helps the author, maybe he left something out, or thought some feature might not be relevant to new vi users. Come on, flame wars over text editors…..still? Really? I would think trolling around the internet and posting ridiculous, unhelpful comments about whether emacs or vi is better would be below someone who used emacs or vi anyway. I bet you use neither. I bet you still use Windows and a notepad editor.

  14. Dave Lane

    Been using Emacs (no X) daily for the last 15 years… over the holidays decided to see how the other half live, so I committed to learning vim… Modal editing still bends my brain (it actually does make my head hurt sometimes), but – if you haven’t bothered to learn lisp – it is an impressive, lightweight, powerful editing platform… thinking I might stick with it for a while. Would love to have vim flow from my fingers as unconsciously as emacs does now.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!