1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

VIM Editor Tutorial

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

  1. LongBanana

    LongBanana Regular Member

    Joined:
    Oct 23, 2009
    Messages:
    411
    Likes Received:
    247
    Location:
    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.

    Homepage:
    Code:
    http://www.vim.org/
    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:
    Code:
    http://code.google.com/p/macvim/
    
    *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:
    Code:
    sudo apt-get install vim
    
    Gentoo Users:
    Code:
    sudo emerge -av vim
    
    Arch Linux Users:
    Code:
    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:
    Code:
    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:
    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:
    Code:
    :w
    
    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:
    Code:
    :q
    
    That will QUIT the current file and get you back into the terminal you were in.

    Now, type in:
    Code:
    vimtutor
    
    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:
    Code:
    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:
    Code:
    gt
    1gt
    2gt
    3gt
    
    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:
    Code:
    :!javac filename.java
    
    :! sends a command to bash and you type in what you would like to send.

    Going to a specific line number:
    Code:
    :#
    
    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:
    Code:
    /insertwhatyouwannasearch
    
    and press ENTER and it should find the first instance. But, if you want to see where the next instance is type:
    Code:
    n
    
    and it should go to the next available instance of that search

    Indenting the WHOLE file:
    Code:
    gg
    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:
    Code:
    :tabnew filename
    
    That will open up a tab with the file you requested.

    Go to the end of the line:
    Code:
    $ (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:
    Code:
    http://bullium.com/support/vim.html
    
    Code:
    http://www.catswhocode.com/blog/100-vim-commands-every-programmer-should-know
    
    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

    Joined:
    Oct 16, 2009
    Messages:
    196
    Likes Received:
    45
    Really helpful and concice.:)
     
  3. hiderightnow

    hiderightnow Junior Member

    Joined:
    Jul 19, 2010
    Messages:
    104
    Likes Received:
    22
    The best thing about VIM:
    :%s/replace/replacewith/g
    That makes my day anyday :D