Home > English, Linux, Programming > Vim for the win :)

Vim for the win :)

I have been using Vim a lot in the last few weeks, to edit CSS and HTML files. It might seem a bit awkward at first but once you get used to it, it becomes great!
What helps a lot too is to configure Vim properly to make it perfect for use.
Vim and Vi configurations are done by setting variables using :set or by editing the file .vimrc in your home folder.
I will share here my Vim configurations and some of the commands I use more often. It also works as a Vim cheat sheet for my own future reference.

So, my ~/.vimirc looks like this:

set tabstop=2 expandtab autoindent smartindent shiftwidth=2 number

 
Here’s what each of them does

Configuration Effect
tabstop=N Sets the tab size to N spaces. The default is 4.
expandtab Tabs are expanded, when you press tab, N characters will be inserted instead of a tab character (where N is the amount of spaces set for tabstop).
autoindent Will keep the same indentation when you press enter in the end of a line.
smartindent Vim will try to automatically detect indentations. For instance, if you go to a new line and enter a ‘}’, Vim will go back one indentation level.
shiftwidth=N The number of spaces used for one indentation level.
number Shows lines numbers.

And here are some of the commands I use more often:

Command What it does..
:N goes to line N
/foo searches for ‘foo’
:%s/foo/bar/g replaces all occurrences of ‘foo’ with ‘bar’ in the current file
:s/foo/bar/g Same as above, but replaces only on the current line
:s/foo/bar/gc Same as above, but prompts for confirmation before replacing each occurrence of ‘foo’. Can also be used with ‘%’ to replace all occurrences in the file
Shif + G goes to the end of the file
v enter select mode
<< moves the current line one indentation level backwards;
>> moves the current line one indentation level forward;
yy copies current line to the buffer
dd removes the current line out and place it on the buffer
p pastes the text from the buffer on the next line

Also remember that many of these commands may be used with a different number of lines.
For instance, pressing 1, 0, d, d will delete he next 10 lines.

Any other cool commands for Vim? Let me know on the comments!

Categories: English, Linux, Programming
  1. May 16, 2010 at 15:52

    Hey there uncle lost.
    I’m also using vim, since I’ve grown so tired of learning some new editor every single time I need to switch OS.

    I find myself also using these all the time:

    ggVG (selects everything)
    “+y (‘yanks’/copy to clipboard instead of register)
    “+p (paste from clipboard instead of register)
    shift + control + V then BI (prepend to lines)
    di” or ci” (delete/replace inside “, also applies for ‘([ and maybe others)
    gg=G (auto indent the file)
    % (go to matching bracket, parenthesis, begin-end)
    :set cursorline (highlights current line)

    plus, custom key bindings for NERDTree and FuzzyFind plugins

  2. May 16, 2010 at 16:00

    now for a complete useless comment:

    I thought you were using linux, so why “vim for the WIN” ?

    • May 16, 2010 at 16:15

      hahaha Yes, I use Linux of course, Vim FTW! though 🙂
      Cool commands! I didn’t know about the =G, very useful!!!
      What does shift + ctrl + V do?

      • May 17, 2010 at 02:27

        visual block mode

  1. No trackbacks yet.

Leave a comment