Skip to main content

Basic useful vim commands for everyone

Sanjeev Kumar Jaiswal's picture

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands.
This post has only most commonly used vim commands which we use in our day today development activities. This post will be very helpful for those who wish to learn vim editor from the scratch and it can be useful for all other vim users too.

So, first open the file by using vim filename (single file at one time). I will be posting very soon about manipulation on multiple files using vim editor.

  1. Text Entry Commands(Used to start text entry)
    • a Appends text following current cursor position
    • A Appends text to the end of current line
    • i Inserts text before the current cursor position
    • I Inserts text at the beginning of the cursor line
    • o  Opens a new line above  the current line and add text there
    • O Opens up a new line below  the current line and add text there
  2. Cursor Movement Commands
    • hMoves the cursor one character to the left
    • lMoves the cursor one character to the right
    • kMoves the cursor up one line
    • jMoves the cursor down one line
    • nG or :nCursor goes to the specified (n) line   (ex. 10G goes to line 10)
    • ^F (CTRl F)Forward screen
    • ^BBackward screen
    • ^fOne page forward
    • ^bOne page backward
    • ^UUp half screen
    • ^DDown half screen
    • $Move cursor to the end of current line
    • 0(zero) Move cursor to the beginning of current line
    • w Forward one word
    • bBackward one word
  3. Copy
    • The command ‘Y’ or ‘yy’ copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively, we can use following commands:
      Y, 2Y, 10Y, yG
    • It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:
      yw, y$
    • The same commands paste the text within a line. Lower case p pastes after the cursor position and upper case P pastes before.
      Paste will also work with deleted text, either lines or parts of lines.
      Be careful not to execute any other commands prior to pasting as this will empty the buffer.
  4. Copy using visual mode
    • Press esc to confirm that you are in normal mode.
    • Press shift+v (Visual Line) to start with visual mode. v will just enable visual mode, ctrl+v will enable visual block and shift+v will enable Visual Line. (to copy lines visual line mode would be handy)
    • Select lines using arrow key then press yy to yank. y to yank and p to paste (edited. Thanks to Srikanth Doss for correcting it)
  5. Paste
    • To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:
      P, p
  6. cut/copy/delete/paste lines without knowing the actual number of lines (Very useful one)
    If you ever need to cut/copy/delete/paste lines without knowing the actual number of lines, here is what you should do.
    • In normal mode, go to the beginning of the section that you want to yank.
    • Type mkto mark this spot as k.
    • Go to the end of the section you want to yank using whatever movement commands you like.
    • Type: y'k(y for yanking, single quote for go to mark option and  k is like giving a cross sign to the spot for our recognition ) :To yank from the mark to the current location.
    • You can paste those lines wherever you want with p or P
      Similarly, d'kwill cut/delete the lines from the current location to the mark.
  7. Searching something
    • :/search string in present file forward (press esc in opened file and then type)
    • :?search string in present file backward
    • use n to find the next match, or N for previous match.  (When done with above two commands no  get used of it )
  8. Replacing word, character or line
    • r Replace one character at the cursor position
    • R is used overstrike or replace mode. (use ESC key to exit from this mode)
    • cw Changes current word to a new word (It will clear that word and you can write new word)
    • Change from cursor to end of line C
    • Change entire line cc  (It  will clear out the entire line seems like dd but dd deletes/cuts it does not so you can’t apply paste on this action)
  9. Text Deletion Commands (use only after pressing esc button)
    • x Delete character
    • dw Delete word from cursor on
    • db Delete word backward
    • dd Delete line
    • d$ Delete to end of line
    • d^ (d caret, not CTRL d) Delete to beginning of line
    • dL -> deletes till the visible screen
    • dG -> deletes till the end of a file
  10. Brace Matching
    If the cursor is on an opening parenthesis { [ (, the command %will move the cursor to the matching closing } ] )in the normal mode, and vice versa.
     
  11. Keyword Matching

    If you are as lazy as me, you will find this function wonderful :)

    In insert mode, you can type a few characters of a word, e.g., if there is a variable called jassi_intro_users, you may just type jass then Ctrl+p. Vim will find out the last word in the file started with characters jass, if it is not you want, you can re-type Ctrl+p to match the further previous ones.

    Similarly, Ctrl+n can do that for finding the next matches. Therefore you do not worry about the mistyping of the long variables, or uncommon-used words.
     
  12. Saving File
    • :wq writes files and exit
    • ZZ :x    write file if updated and exit otherwise similar to :wq
    • ZQ   :q! don't write and exit
    • :q   quite the current window
    • :w   just write/save file, don't exit
  13. Recovering files When system goes mad
    When you edit an important file and suddenly there is a power outage. All system is shutdown before you can save your source (UPS didn’t work?).  Don't be sad because Vim provides a recovering mechanism. There will be  a file called .pentium.v.swp if you were editing a file called pentium.v

    Recover it with Vim command, open .swp file, check if everything is there or not.

    vim -r .pentium.v.swp and see if it contains the source That you wrote . If everything seems fine to you, then save it and  then remove the swap file .filename.swp.

  14. Jumping Between Multiple Windows
    • To split the window you can use Ctrl+w s, or start Vim initially
    • vim -o *.c
    • or inside vim write :split filename (horizontal split) and :vsplit filename for vertical split
    • Jumping between the sub-windows, use Ctrl+w plus arrow key to switch the cursor.
    • Left and right arrow key to move cursors between vertically split files.
    • Up and down arrow key to move cursors between Horizontally split files
  15. Other Useful Commands (All in Escape mode only)
  • Type Ctrl+g and Vim will report the filename, the status (modified or readonly), total number of lines and the cursor position.
  • esc+i or shift+c will put insert mode
  • u Undo last change
  • U Restores line or al changes
  • J Joins next line down to the end of the current line
  • . Repeats last command
  • Use “:set nu” or “:set number” to set number in vim (after pressing esc button)
I have posted this article in experts-exchange community too!
http://www.experts-exchange.com/Programming/Open_Source/A_9497-Basic-vim-commands-for-daily-use.html
Venkateswaran (not verified)
Super Like... :)
The above post is actually very useful to gain knowledge about the VIM Editor, personally, i dont know much about "VIM". So would request you to kindly guiding me about VIM... Thanks and Regards.
sanjeev's picture
Offline
Joined: 21 Feb 2011
Thanks for your Quick response Venkateswaran

Yes Venkateswaran, you are absolutely right. Many times even I forget  some of these commands. So thought to put day today commonly used vim commands at one place.
 

Ok will post about basic details of vim very soon and will notify you.

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

Saru Singhal (not verified)
Bookmarked it, very useful. I
Bookmarked it, very useful. I will use it, thanks for sharing!
sanjeev's picture
Offline
Joined: 21 Feb 2011
so nice of you Saru

We will try post many bookmark-able posts then :D

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

Saru Singhal (not verified)
Is this something new, I know
Is this something new, I know only clicking the star and done!
sanjeev's picture
Offline
Joined: 21 Feb 2011
yes bookmarking site is also a technique

Yes saru, there are various ways to bookmark a site or link.

Will post about it too. By the way i was saying that I will write more posts so that you can book mark more posts :D

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

sanjeev's picture
Offline
Joined: 21 Feb 2011
posted this article on experts-exchange too

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

Anonymous (not verified)
bookmark or buy the Mug of VI
bookmark or buy the Mug of VI :)

regards

M.
Fred (not verified)
Additions
Section 2 - ^ will also send you to the beginning of the line, but 0 is easier on my keyboard.
Section 9 - D will also delete to the end of the line.  You can use Dp like y$ if your keyboard makes $ hard to type.
Section 12 - :wn will save the file and start editing the next file. In the old vi you have to :w and then :n.
sanjeev's picture
Offline
Joined: 21 Feb 2011
Thank Fred for enhacing the knowledge base
Thank you so much Fred for your invaluable reply and I learned few more options today with an ease.
That's why we promote as "sharing means half done!"

Keep learning and don't forget to share ;)

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.