Some useful tips about Vim

I edited this post with suggestions from another users of Vim, and to remove some typos. Thank you!

Vim is a command-line text editor that you can install on every unix-like system, and on Microsoft Windows too (with Cygwin or with its own executable). Also you have GUI version (gVim) if you want a desktop application. This powerful editor is my favourite one for coding, for editing config files and for everything that I need in command-line.

Vim is the evolution of the popular Vi (its name is Vi Improve), with some amazing features: tabs, syntax of popular programming languages, a lot of new keyboard shortcuts and some performing features.

Nowadays, on my new job, I use it every day for everything on a remote Solaris box. This post is about some tips that I use a lot, but this is not a replacement for the official documentation. It’s only an abstract for remind me that commands and, if they are useful for you, I will be pleased.

Some basics commands

There are four modes of work with Vim. It’s strange but don’t worry, you usually use three of them: command, movement and insert modes. You will be on command mode for default when you run Vim.

  • To move the cursor: use the arrows keys or use, only on command mode:
    • ‘h’ to move left.
    • ‘j’ to move down.
    • ‘k’ to move up.
    • ‘l’ to move right.
  • To change to insert mode: type ‘i’ on command mode. This command allows you to write into your document. Also you can use more commands to go into this mode:
    • To insert after the character marked by the cursor: ‘a’ on command mode.
    • To insert new line after the current line: ‘o’ on command mode.
  • To come back to command mode from insert mode: press ESC key.
  • To open a file:
    • On command-line: vim name_of_file
    • If you already run vim, type ‘:o name_of_file’ on command mode.
  • To save your document: type ‘:w’ on command mode. If you want to write in read-only file but you have permissions to do it (it occurs when you run two or more vim to edit the same file), use ‘:w!’ to save on command mode.
  • To quit: ‘:q’ on command mode. If you want to quit, when you have edited the file but don’t want to save the changes: type ‘:q!’.
  • To copy (yank) lines:
    • Same line: ‘yy’ on command mode.
    • Actual line and X-1 next lines: ‘yXy’ on command mode.
  • To paste lines, after the actual line: ‘p’ on command mode.
  • To cut/delete actual line: ‘dd’ on command mode.
    • Cut/Delete actual and X-1 next lines: ‘dXd’ on command mode.
  • To delete character: ‘x’ on command mode.
  • To replace one character ‘rA’ meaning A the character that will replace the other marked by the cursor, on command mode.
  • To undo the last command: ‘u’ on command mode.
  • Search something ‘/word_to_search’ or ‘?word_to_search’ on command mode. The ‘?’ do a backwards search.

Do you want to learn more about the basics of Vim? Use this documentation or search on Internet for more detailed. For small sheets, you can use this.

Some useful commands

Maybe you know typical commands of Vim. Did you try to use more? I have learnt a lot, only seeing another people using them and trying that commands after. For example:

  • Using tabs:
    • Create tab: ‘:tabnew name_of_file’ on command mode.
    • Change to next tab: on command mode, type ‘gt’. To change to another tab, ‘Xgt’ meaning X the number of the Xth tab that you have.
    • Close tab: use the common quit command ‘:q’ on command mode.
    • Save all tabs: ‘:wall’ on command mode.
    • Save all tabs and exit: ‘:wqall’ on command mode.
  • Navegate in files:
    • Moving to the end of line: ‘$’ on command mode.
    • Moving to the beginning of line: ‘0’ on command mode.
    • Moving Forward pages: ‘Ctrl + f’ on command mode.
    • Moving backward pages: ‘Ctrl + b’ on command mode.
    • Move to the end of the file: ‘G’ on command mode.
    • Move to the beginning of the file: ‘gg’ on command mode.
    • Move to Xth line: ‘:X’ on command mode.
  • Using man, without opening another terminal: put the cursor above the function you want to know and press ‘Ctrl+K’ on command mode  (realize that the K is in uppercase).
  • Auto-complete words that you have written before in the same file: type ‘Ctrl+n’ on insert mode.
  • Run command-line programs without opening another terminal: type ‘:! name_of_command’ on command mode.
  • Replacing words in all the file: type ‘:0,$ s/word_to_change/new_word/g’ on command mode. If you want to change in only few lines, use ‘:X,Y’ instead of ‘:0,$’, meaning, of course, the Xth is the first line and the Yth the last line between you want to replace that word. The ‘g’ means that you want to replace all the appearing of the word in the same line.
  • When you are coding, it’s useful know where a ‘(‘, ‘{‘ have its closing equivalent ‘)’, ‘}’ or viceversa. To know that, put the cursor on it and press ‘%’ on command mode.
  • Insert the content of another file inside the current one: ‘:r file_to_get_data’ on command mode. Vim will paste its content in a new line.
  • To extract some lines from the current file and paste them in nother file: ‘:.,.+Xw!file_to_paste’ on command mode. Meaning the X how many lines you want to copy after the current one.
  • Some settings:
    • If you type ‘:set incsearch’ on command mode, Vim will show the first match for the pattern, while you are still typing it.
    • If you type ‘:set number’ on command mode, Vim will show the number of each line of the document in left side of screen.
    • If you type ‘:set syntax=c’ on command mode, Vim will remark the syntax of the C language on your document.

Do you want to learn more?

Use the official documentation, it’s perfect for you.

Do you want to help me discovering new useful commands?

Write a comment on my blog! I’m willing to learn more!

13 thoughts on “Some useful tips about Vim

  1. Please note that there is also a native Windows version of vim (or rather gvim), no Cygwin needed. It even includes OLE support, so it can integrate with Visual Studio, too. (I still have to look into integrating Eclipse with vim..)

  2. @Jakub: I added this information to my post. I didn’t know that an executable for Windows exists, although it’s shown on the official website! 🙂

    I’m going to install it on my work’s computer.


  3. « Replacing words in all the file: type ‘:0,$ s/word_to_change/new_word/g’ on command mode. »

    There’s a ‘ ‘ between the ‘$’ and the ‘s’, is that a typo ?

    Also, I’m not sure about this ‘0’, I use ‘1’ as it is the.. well, 1st line 🙂

    Also, instead of ‘1,$’, you can use ‘%’.

    « :%s/word_to_change/new_word/g »

    Saves you 2 key strokes 😛

  4. @bochecha: Yes, there is a space between 0,$ and s/, it isn’t a typo because it works too. Also the ‘0’ works to represent the beginning of the file (in this case), but you can use ‘1’ instead, as you do.

    I didn’t know the use of ‘%’ to replace ‘1,$’, thanks!

  5. hi,

    You can use tabedit to open a new tab too.

    The split comes handy too sometimes.

    you can switch between the splits using “ctrl+w” and then w.

    for tabs.. “wall” saves all the tabs, “wqall” will save all tabs and then quit.



  6. You might add search functions (“/”, “/”, “?”) They are very usefull when navegating in files, and with “less” (and “more”) too.
    When I switch to command mode with ESC, I usually type “CTR-G” to know where I am…
    If I want to extract some lines, I use “:.,.+5w!where-I-want-to-put-the-lines”; the insertion from a file “truc” comes with “:r truc”.

  7. Sometimes, one wants to paste text from other documents -which are not opened under vi), and gets upset (too many white spaces)…
    “:paste on”
    seems to fix it according to
    “” which gives also ways to code fold (codefolding seems very useful with modern tiny screns… or huge code) with _new versions_ of vim…

  8. + Браво, мне кажется это отличная мысль
    Присоединяюсь. Так бывает. Можем пообщаться на эту тему. Здесь или в PM.
    аот лажа
    В этом что-то есть. Благодарю за информацию. Я не знал этого.
    Извините, я подумал и удалил вопрос

Comments are closed.