VIM Editor Tutorial

Discussion in 'General Programming Chat' started by LongBanana, Feb 7, 2012.

  1. LongBanana

    LongBanana Regular Member

    Oct 23, 2009
    Likes Received:
    Chicago, IL
    VIM is a very powerful editor that allows you to navigate, replace, and manipulate code through keyboard commands and shortcuts.

    Now why should you use VIM as an editor?
    1) Speed. You can edit files MUCH faster than any other editor. It allows you to change multiple things in a file within just a few minutes.
    2) You don't have to take your hands off. Most editors make you have to use your mouse to get to a certain point since arrow keys are too slow. With VIM, you won't have to touch your mouse ever while programming! It will keep you efficient and keep you focused while programming.
    3) It has support for almost every language syntax
    4) Fast support for indentation. You can indent the whole file within a few seconds and keep your file indented the way you want it to be very easily. It can also auto indent based on where you are in the file.
    5) Allows you to execute shells commands easily without moving out of the editor
    6) REGEX support for replace & search / replace which allows you to change a whole file in a few seconds.

    If you a developer and you THINK you have a great editor. Give this a try, it takes probably 30 minutes to an hour to get used to it and it will be well worth your time.

    Developers should learn to use this and *nix-based machines.

    First off, you need to download VIM itself.

    === NOTE: VIM on Windows is not that great. I highly recommend a *nix machine for development ====

    VIM is already built into MAC / OSX, but you do not get a configuration file (the .vimrc file).
    But, if you want a GUI VIM for MAC, go here:
    *nix-based operating systems include VI (not exactly vim, but close). So, you have to install VIM yourself.

    Open up a Terminal and enter in the following code:

    Ubuntu / Debian Users:
    sudo apt-get install vim
    Gentoo Users:
    sudo emerge -av vim
    Arch Linux Users:
    sudo pacman -S vim
    After installing VIM, you need a simple setup so that you can edit files quickly and easily.

    So, in the Terminal you have open, enter in:
    vim ~/.vimrc
    That means open up the file located in my home directory (~) named .vimrc with VIM. It will most likely not be there and VIM should create it automatically.

    Now copy the following code:
    " map <C-J> gqap
    " imap <C-J> <ESC>gqapi
    syntax enable
    set title
    " Make trailing whitespace visible
    " set list listchars=eol:\ ,tab:>-,trail:.,extends:>,nbsp:_
    syntax spell toplevel
    filetype indent on
    filetype plugin on
    set grepprg=grep\ -nH\ $*
    " Auto number... 
    set nu
    set modeline
    " Fast buffer switching
    map <C-N> <Esc>:bn<CR>
    map <C-P> <Esc>:bp<CR>
    " autoindent is evil
    set noai
    set tabstop=3
    set shiftwidth=3
    set expandtab
    set showmatch
    set tw=78
    set nocompatible
    " Makes buffers not need to be saved when hidden.  Use with care.
    set hidden
    " set visualbell
    " set digraph
    " Spelling
    highlight SpellErrors ctermfg=Red guifg=Red cterm=underline gui=underline term=reverse
    " Test the autocommand
    " autocmd BufReadPost,FileReadPost    *.java read ~/c.txt
    highlight Comment ctermfg=LightGray
    filetype plugin indent on
    syntax on
    " stop the search from being obnoxious and hilighting everything
    set nohlsearch
    " change comment color
    highlight Comment ctermfg=darkcyan
    set cursorline
    After copying it, go to your opened .vimrc in the terminal and press "a" or "i" to enable the insert mode.
    You should see "---INSERT---" at the bottom of your terminal screen.
    Now, press "CTRL + SHIFT + V" and it should paste what you just copied into the file.

    The reason you had to use SHIFT key as well is because CTRL + C is a command which exits what you are currently doing in the Terminal.

    We won't go into much detail as to how the .vimrc works. There is a lot of documentation on that and there are many configurations you can put, but this is fairly basic.

    You need to save the file now, so type this in:
    And press "ENTER" and you should get a message saying it was "written" meaning "saved"

    Congratulations. You now have VIM and a manageable .vimrc configuration on your system.

    VIM has a built-in tutorial which can really help you get started with the basic commands.

    If you are still in the file with VIM, type this in:
    That will QUIT the current file and get you back into the terminal you were in.

    Now, type in:
    and press ENTER. You should be taken to a VIM tutorial within the terminal and it will show you how to use basic commands in VIM and should get you started fairly well.

    Here are some commands that I found very useful (besides basics):

    In the terminal type:
    vim -p file1.html file2.html file3.html
    That will open up those 3 files and have tabs separating them.
    To navigate between the tabs type this:
    gt tells the tab to move one forward. When you type a number before "gt", it will go to that numbered tab.

    For those working with Java files, you can compile with a simple command once you have the JDK installed on your *nix machine.
    When you are editing the file in VIM just type:
    :! sends a command to bash and you type in what you would like to send.

    Going to a specific line number:
    where "#" is the number you put in and press ENTER and it should take you to specified line number.

    Searching for something within the file:
    and press ENTER and it should find the first instance. But, if you want to see where the next instance is type:
    and it should go to the next available instance of that search

    Indenting the WHOLE file:
    V (shift + v)
    G (shift + g)
    gg goes to the TOP of the file and then V highlights the line and G tells it to highlight all the lines to the bottom of the file. Then, when you press "=" it should indent the whole thing.

    If you want to open up a new TAB within your current VIM that is opened up type:
    :tabnew filename
    That will open up a tab with the file you requested.

    Go to the end of the line:
    $ (Shift + 4)
    There are A LOT more commands that can really help you manipulate and easily navigate through files.

    Here are two cheat sheets which I find very useful:
    If you have something to add about VIM, feel free to. :) I'm always looking to learn how to make it faster and easier for me to edit files and develop programs.
    • Thanks Thanks x 3
    Last edited: Feb 8, 2012
  2. Nick1

    Nick1 Junior Member

    Oct 16, 2009
    Likes Received:
    Really helpful and concice.:)
  3. hiderightnow

    hiderightnow Junior Member

    Jul 19, 2010
    Likes Received:
    The best thing about VIM:
    That makes my day anyday :D