Become A Vi Master by Learning These 30+ Key Bindings

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Become a Vi Master by Learning These 30+ Key Bindings

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 theyve learned its key bindings. You can even use vi key bindings in Bash. Weve already covered getting started with vi for beginners. If you havent 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 theres 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 youre already in command mode, Escape does nothing, so you can press Escape to ensure youre in command mode.

Moving the Cursor


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

h Move cursor left. j Move cursor down. k Move cursor left. 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.

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.

Repeat & Undo


Vis 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 youve 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.

This isnt 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 Harvards website is more complete and has more information, although its less organized and harder to digest all at once.

By Chris Hoffman on 05/28/12

Ads by Google
Online Ladies
Buy Pen Drives, Music CD's, Mobiles Gifts, Apparels, Watches and more!
Shopping.Indiatimes.com

Shop for Women Tunics


Register for Member Discounts of Upto 80% on Stylish Women Tunics
www.fetise.com/Shop-Now

Download Google Chrome


The fast new browser by Google Fast and Free. Download Now!
www.Google.com/Chrome

Comments (11)
1. May 28, 2012 9:07 am

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. May 28, 2012 10:01 am

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 Im 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}. Thats 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 dont know anyone who is an expert under age 50 that didnt think they sucked when they first started using either. 3. May 28, 2012 2:09 pm

Bob Jones You have an error, k Move cursor left. whereas it should be k Move cursor up. 4. May 28, 2012 4:08 pm

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, its not 71 anymore and Ritchie is dead. I am not a UNIX-hater. For me, Ritchies death was more tragic than Jobs; but vi. Give me a break. 5. May 28, 2012 8:27 pm

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. May 28, 2012 11:00 pm

Kevin James Lausen also dont forget about the vimtutor command that launches the vim tutorial.a pro can complete it i n 5mt. or less 7. May 29, 2012 2:49 am

marc ZZ will save and quit 8. May 29, 2012 3:31 pm

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. May 30, 2012 3:07 am

Geoff The v command is specific to vim. It wont work in other versions of vi. 10. May 30, 2012 9:37 am

Abdel Said After youve done here, lets play golf at vimgolf.com 11. May 30, 2012 10:20 am

anon

ugh just tried to :q this article ;)

You might also like