SEARCH

How-To Geek

Become a Vi Master by Learning These 30+ Key Bindings

image

Vi is a powerful text editor included on most Linux systems. Many people swear by vi and find it faster than any other editor once they’ve learned its key bindings. You can even use vi key bindings in Bash.

We’ve already covered getting started with vi for beginners. If you haven’t used vi in a while, you might want to give that post a look to get a refresher on the basics.

Mode Switching

As a short recap, vi is a modal editor – there’s an insert mode and a standard command mode. In insert mode, vi functions similar to a normal text editor. In command mode, you take advantage of these key bindings.

  • i – Enter insert mode.
  • Escape – Leave insert mode. If you’re already in command mode, Escape does nothing, so you can press Escape to ensure you’re in command mode.

image

Moving the Cursor

Vi uses the hjkl keys to move the cursor in command mode. Early computer systems didn’t always have arrow keys, so these keys were used instead. One advantage of these keyboard shortcuts is that you don’t have to move your fingers from the home row to use them.

  • h – Move cursor left.
  • j – Move cursor down.
  • k – Move cursor up.
  • l – Move cursor right.

You can also use search commands to quickly move the cursor.

  • / – Type a / followed by some text you want to find and press Enter to quickly move your cursor to the location of the text in the file. For example, if you have the word iguana in your file, type /iguana and press Enter to quickly move the cursor there.
  • ? – Like /, but searches backwards.
  • f – Type an f followed by any character to quickly move the cursor to the next occurrence of the character on the current line. For example, if you have the line “Hello world” on a line and your cursor is at the beginning of the line, type fo to move to the o in Hello. Type fo again to move to the o in world.
  • F – Like f, but searches backwards.
  • % – Jump between the nearest (), [], or {} characters on the line.

Use these commands to quickly move to locations in the file:

  • H – Move cursor to highest (top) line in file.
  • M – Move cursor to middle line in file.
  • L – Move cursor to lowest (bottom) line in file.
  • #G – Type a number and then type G to go to that line in the file. For example, type 4G and press Enter to move to the fourth line in the file.

Moving between words:

  • w – Move forward a word.
  • #w – Move forward a number of words. For example, 2w moves forward two words.
  • b – Move back a word.
  • #b – Move back a number of words. For example, 3b moves back three words.
  • e – Move to end of the current word.

image

Copying & Pasting

Vi refers to the act of copying as “yanking.”

  • v – Press v and move the cursor to select a section of text.
  • y – Copy (yank) the selected text.
  • p – Paste at cursor.
  • x – Cuts the selected text. Cuts the character under the cursor if no text is selected
  • r – Type r and then type another character to replace the character under the cursor.

Combining Commands

Some commands – including the y and v commands above and the d (delete) command accept cursor motion commands.

For example, when you press d to delete some text, nothing will happen until you enter a cursor motion command. For example:

  • dw – Deletes the next word.
  • db – Deletes the previous word
  • de – Deletes to the end of the current word.
  • dL – Deletes all text below the cursor in the file.
  • d/unicorn – After pressing Enter, deletes all text between the cursor and the word “unicorn” in the current file.
  • dd – Deletes an entire line.

As you can see, the combination of combining a command with a cursor movement command is very powerful.

image

Repeat & Undo

Vi’s repeat command is very powerful, as it can repeat complex, combined commands.

  • u – Undo.
  • . – The . repeats the last full command.  The insert command also functions as a command here. For example, type iunicorn and press Escape. You can then use the . key to insert the word unicorn at the cursor.

Bonus: Using Vi Key Bindings in Bash

Once you’ve mastered the vi key bindings, you may want to use them elsewhere on your system. No problem – you can set the Bash shell to use vi-style key bindings.

Try this out in the current session by running the following command in a Bash terminal:

set -o vi

Bash will start in insert mode – press Escape to enter command mode and use these key bindings.

If you like this, you can add the command to your ~/.bashrc file and it will be automatically run each time you log in. Use the vi .bashrc command to open and edit the file in vi.

image


This isn’t a complete list of key bindings for vi, but it should help you flex your vi wings and learn to fly. This list of key bindings at Harvard’s website is more complete and has more information, although it’s less organized and harder to digest all at once.

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 05/28/12

Comments (18)

  1. spaceyjase

    The H, M and L movement bindings move on screen rather than the file. G (on its own) will move to the end of the file so ‘dG’ will delete from the cursor until the end of the file rather than dL which will delete until the lowest line on screen (how I remember it, H – high, M – middle, L – lowest).

  2. TheFu

    h, j, k, l are the movement keys. Any ‘vi’ tutor has to say that – forget the arrows, not all keyboards have arrow keys and not all ‘vi’ implementations support arrow keys.
    * h – move left 1 character
    * l move right 1 character
    * j move down 1 line
    * k move up 1 line

    A number, X, before almost any command will perform that command X times. Those numbers can be 1 or 10000000000000. I routinely delete 11 lines at a time when I’m not sure how many lines need to be removed.

    5dd – deletes 5 lines.
    5dw – deletes 5 words.
    5p – will paste whatever is in the buffer, 5 times, below.
    5P will paste whatever is in the buffer 5 times, above.

    If an action going forward uses a lowercase letter, then going in the opposite direction (or before) is usually shift-{same key}. That’s why searching forward is / and backwards is ?.

    Global search and replace is pretty powerful to …. but you need to know regex for the real power to be available.

    df{character} – “delete find” – “dfP” deletes all characters from the cursor to the first instance of “P”.

    No love for folding / unfolding blocks/subroutines/methods? I guess that is an expert need – for coders only.

    There are thousands of these tricks. OSX people should love this. ;) They love all those hidden features in Apple products that are well thought out after all.

    vi and vim are unbelievable editors – probably the most efficient editors available, but I don’t know anyone who is an expert under age 50 that didn’t think they sucked when they first started using either.

  3. Bob Jones

    You have an error, “k – Move cursor left.” whereas it should be “k – Move cursor up.”

  4. KashifSMalik

    vi sucks. I have used it for almost fourteen years. Never fell in love with it. Learning vi is a necessary evil of working on UNIX platform. People have to realise that systems have to evolve, it’s not 71 anymore and Ritchie is dead. I am not a UNIX-hater. For me, Ritchie’s death was more tragic than Job’s; but vi…. Give me a break.

  5. pbug56

    There have been far better and more usable text editors for systems and programming for decades. The second version of the DEC standard editor was reasonably intuitive and powerful, and far easier to use then VI. I consider VI one of the many old, band jokes laid by the quacks who decided how commands should work and be named in the varying types of EUNUCHS.

  6. Kevin James Lausen

    also don’t forget about the vimtutor command that launches the vim tutorial….a pro can complete it i n 5mt. or less…

  7. marc

    ZZ – will save and quit

  8. djf

    vi is like playing the piano. If you need to think about each key you will be hopeless frustrated. Learn to play combinations of keys (like songs) and there is not any software out there YET which can replace vi.

    I play the piano and have a hard time telling someone how to play a song by note, or equally find combinations of words in a file using vi by the key strokes ;-)

  9. Geoff

    The v command is specific to vim. It won’t work in other versions of vi.

  10. Random

    http://vim-adventures.com/ is there for a while now

  11. Abdel Said

    After you’ve done here, let’s play golf at vimgolf.com

  12. anon

    ugh – just tried to :q this article ;)

  13. Jorge

    d$ will delete from the point to the end of the line

  14. deeb

    What, no mention of cut & paste buffers?
    5ya – yank the next 5 lines into buffer a [b, c, d...]
    “ap. – paste buffer a

    One great benefit of vim vs vi – multiple undo levels.

    Another trap for young players – deleting a line OVERWRITES YOUR CUT BUFFER. Geez, how many times have I been caught out by that one……

    And at the risk of trollbaiting – vi is a bit like notepad in windows in that it ain’t the best editor, but like sed, awk, grep and Bourne shell it’s a part of even the most minimal install of pretty much any *nix you can think of. So if you know how to drive vi properly you can edit files whatever system you’re on. That might not mean much if you’re in a pure Linux environment, but in a mixed environment it’s pretty damn handy.

  15. Ash

    Very useful vi commands, thank you for sharing

  16. Chris Hoffman

    @TheFu

    I thought about including folding, but it seemed a bit expert.

    @Bob Jones

    Sorry — obvious mistake. Fixed!

    Thanks everyone else for chiming in. I’m nowhere near as much of a vi expert as many of you seem to be, but I’m glad I could help less experienced users get started with vi.

  17. JimW

    It’s good to see ‘vi’ get some “press”…
    Actually ‘vim’ is yet another major leap forward (free download), especially because it runs on Windows as well, and its cusomtization for Windows enables the arrow keys, etc – virtually all the keys that Word or Notepad use to be halfway useful.
    The search capability and flexibililty far surpasses windows editors and only minimal “regex” knowledge provides most of the usability.
    It is also quite “programmable” (though well short of emacs); I have a wealth of macro actions I have written for more efficiency.
    Certainly, the ‘vimdiff’ program that comes with the ‘vim’ download for comparing two versions of a file should be mentioned.
    There are SO many things I can do in ‘vim’ that I cannot do efficiently in a wiindows editor, but if I had to point to one that is simple and always missed when I am not editing in ‘vim’ it is the “repeat” action using the ‘.’ (period) key.
    This is much harder to describe than it is to use… but I’ll take a cut at it…
    Suppose I want to look at each time a phrase, say, “all of us” occurs in my file, and suppose I want to change this text to “some of us” selectively, depending on the surrounding context. I set up a search for /all of us/ and am taken to the first match; now, the first time I find a case to change, the entire phrase is highlighted with the cursor on the first character, so I use ’3s’ (substitute for 3 characters), type in “some” (the ESCAPE terminates the input…this is basic vi); now, I can walk through each of the detected cases one by one (using the ‘n’ key), each time seeing the matched phrase highlighted, only now, for those cases where I do want to replace “all” with “some”, I just hit the dot ‘.’ key (period), and so on!
    BTW, ‘vim’ has excellent help; plus everytime I read a section, I find new features I can use!=)
    Still, for me, the critical requirement that ‘vim’ supports is that its format is simple TEXT – one of the few data representations that are universally understood! And since I still use unix (actually ‘cygwin’ now) for a lot of my computing, the power of unix file filtering and manipulation is enabled by text files.

  18. Scott

    You mention in passing that even bash has vi key bindings available. Well, using as many programs as possible that offer vi key bindings is a great way to reinforce what you learn in Vim. Bash, Firefox (via Pentadactyl or Vimperator), Chrome or Chromium (via extensions), and even Okular or any other PDF reader that allows custom key bindings will give you additional muscle memory practice.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!