Essential VIM commands

Recently on digg.com there was a list of UNIX/Linux commands every person should know. A few people were a bit miffed that the list included nano rather than VIM. VIM is easily my favorite text editor so I wanted to share some of the most useful commands that VIM provides. No doubt there are people out there that have their own set of commands they use most so I encourage them to comment about it.

Read on to learn more about VIM.

For those who do not know, VIM is a command line text editor commonly found on UNIX and Linux systems. VIM is an improvement upon the original VI, hence the name, and includes things like color coding and better default keymapping than VI. VIM is also extensible through different configuration options and with enough tweaking you can get VIM to do code completion, code folding and other tricks you can’t find in other editors.

You start VIM by typing vim at the command prompt of most any UNIX/Linux system and then entering the name of the file you wish to edit. At first glance VIM is very minimalistic, there are no menus and no hints as to what to do. While this can be a bit odd at first you’ll soon grow to appreciate the fact that the editor doesn’t get in the way of your work. Like everything else on the command line, you’re expected to know the commands without selecting them from a menu.

VIM will load the file you specified (or will create it if it’s new) and present you with it’s contents. At this point you can see the cursor at the upper left part of the screen and you can move the cursor around with the arrows. VIM works in modes and by default the first mode you are in is called Normal. There are a number of modes but I’m only going to mention Insert as it’s what I use the most. In fact, I usually enter the other modes purely by mistake but that doesn’t mean they’re any less useful, I just haven’t had a need to explore them yet.

While in normal mode you can do a number of things and it’s in normal mode where the real power of VIM is revealed. So without further ado, here is a list of the most common commands that I use daily.

  • i – puts you into insert mode. While in insert mode you can enter text
  • ESC – gets you out of insert mode (or any mode really) and back to normal mode
  • x – deletes the letter under the cursor
  • dd – deletes the current line under the cursor
  • d # down arrow – deletes current line plus # number of lines where # is a positive number. You can press any arrow direction instead of down arrow if you wish
  • dw – deletes the current word or deletes all characters until a change in character type is encountered. For example, if you wanted to delete the word super-soaker and the cursor was on the s in super, pressing dw would delete the word super and stop at the -. However, if you wanted to delete all of the spaces at the beginning of a line, dw would remove all spaces and stop at the first character encountered.
  • yy – copies the line currently under the cursor
  • y # down arrow – copies the line currently under the cursor plus # lines under it. Again, you can substitute the down arrow for any directional arrow.
  • p – pastes any deleted or copied lines below the current cursor position. Pressing shift will paste contents above the current position.
  • >> – shift the current line to the right, like dd, you can modify this by inserting a number in between.

The above commands are the bulk of what I do in VIM, along with some search and replace, but the above commands are very useful. When you are finished with your file, you simply get back to normal mode (esc) and type :wq to write the file out and then quit.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.