Open Source Software and Linux

Sep 15 2008   8:28PM GMT

VIM Shortcuts

John Little Profile: Xjlittle


I like the VIM text editor. Yes, VIM took some getting used to. Yes, VIM requires that you use a keyboard although there is now a graphical VIM for Windows and Linux which is pretty cool as well.

I am writing this so that maybe you will begin to like the VIM editor. Once you get used to the keyboard it becomes quick and easy to edit your text files. You may even get to avoid Carpel Tunnel Syndrome.

Ok so let’s get to it shall we?

Saving a file
To close a file without saving type ctrl +q. To save a file type ctrl + w. To save and close a file type ctrl + wq or ZZ (yes those are caps).

Ok, all of that was straight forward and anyone who has used VIM knows these things.

Let’s try a few more.

To open a file at a given line number type vim + <line number> . To open a file at the first instance of a word type vim +/<word to find> .

Ok let’s do some work inside of a file.

To undo text that you have entered type u and to undo all changes to a line type U. Typing :e! will revert back to the last saved copy of a file if you really mess up. To create a new line below the cursor type o and to create a line above the cursor type O. For searching a string use /<string to find> to search to the right of the cursor and ?<string to find> to the left of the cursor.

Deleting text
To delete a line place the cursor on the line and type dd. To delete several lines type <number of lines to delete>dd. For instance 3dd would delete the line where the cursor is and the next two lines. To delete all text from the cursor to the end of the line type D. For deleting all the text from the beginning of the line to the cursor type d. To delete all of the lines from the cursor to the end of the screen type dL and to delete all text from the cursor to the end of the file type dG. To delete all of the text from the cursor to the beginning of the file use d1G. If you want to delete all text from the cursor position to a given line number type d#$ where # is the line number of the specified line.

Changing text
If you want to change a word move the cursor to the beginning of the word and type cw. Note that the cursor will only remove up to any special character, punctuation or space. If you want to change the current sentence type cs. To change the current line type c$ or C. Note that preceding the command here with a number, as in most VIM commands, will change that number of words, sentences or lines.

Copying and pasting
To copy a line or lines type yy on the line with the cursor. To copy several lines from the location of the cursor down type <number of lines to copy>yy. For instance 5yy would copy the line where the cursor is and the next 4 lines. To paste those lines below the cursor type p and P to paste them above the cursor.

Ok now let’s have some fun!
How about finding and replacing a word or group of words in a file? Easy and fast. Let’s say we want to replace the word module with the words kernel module. At the VIM prompt type:
:%s/module/kernel module/
That would replace the first instance of module with kernel module. But what if you want to replace all instances? Simply add a g at the end of the line:
:%s/module/kernel module/g
If you want confirmation each time before the replace add a c to the end of the above:
:%s/module/kernel module/gc

How about opening another file from which you want to copy something into your current file? This is quite a simple operation and with VIM’s tab completion, which works much the same as bash tab completion, it is really a snap. Ok let’s say we have two files, a sample file in /home/jslittl/sample.txt and a config file in /etc/myapp/app.conf.

Let’s open the application file:
vim /etc/myapp/app.conf
Now open the sample file. Remember we have tab completion so you can use that. In the file that you just opened type:
:split /home/jsl<tab>/samp<tab>
Now the sample file is open and your cursor is in the sample file. Move the cursor to the line that you want to copy and type:
and then:
ctrl w
to move into the app.conf file. Move the cursor to the line above where you want to paste and type;
Easy stuff huh?

The real beauty of an application like VIM starts to shine when you telnet or ssh into a remote machine and have to edit a configuration or ini file.

I hope that you picked up a pointer or two from this that will help make your work quicker and easier. I know I became more efficient when I learned some of these shortcuts.


 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: