VIM Part I – Get a basic VIM up and running

Recently, I “buzzed” that how much I love VIM. Then one friends thought of trying VIM. And by evening he had VIM un-installed from his system, terming it as an old tool in this age of bleeding edge GUI editors. This made me think that how many people dump VIM before even trying the features this editor has to give. So, here is my two cents to this editor.

I will try to write on how to setup this editor and then on we can go digging deeper. I use gvim on openSUSE 11.2 at home and on Windows XP at office. Barring a few different file names and file paths there is not much difference in the way gvim works in Linux and Windows. I will be specifying where ever and when ever required.

One thing, VIM is not an IDE, it is a simple (yet powerful) editor. It is not a replacement for Adobe Photoshop or anynthing like that. VIM is for people who like to “write down” their code. people who do not want to to take their hands off their keyboard. It is surely a blessing for LINUX/UNIX sys-admins. VIM is a VI like editor, VIM is VI Improved. And in addition to console editor, VIM also has a graphical front end GVIM, which brings the powe of VIM to the GUI. In all modern LINUX distros VIM is installed by default and you call only VIM when typing “vi” on the console, vi is aliased to vim. On windows you can download the EXE file from here. Download gvim72.exe. Direct link for gvim72.exe here. When installing on Windows, it will ask you for the kind of install, select “Full Install” there.

After installation the Windows user will get a batch file “gvim.bat” in C:\windows, it is this file which needs to be executed and not the gvim.exe found in C:\Program Files\Vim, that needs to be executed when we have to open gvim. But Windows users do not need to worry as whenever you type gvim in run command interface, it will always open gvim.bat. For Linux users, they can open gvim by typing in “gvim &” from the console (& will put the gvim in backgrouond in that console window), or type in gvim in Linux run command interface (Which can be opened by ALT+F2 in KDE). You will get a basic gvim running there.

This is it, installation is always the easiest way, it is the working with gvim which we have to learn. gvim is highly customizable editor, which you are going to know very soon. Gvim stores its settings in a file named “vimrc”. In windows the filename is “_vimrc” and it is located at C:\Program Files\vim. And, in Linux the filename is “.vimrc” and it is located at $HOME/.vim directory. If there is no vimrc file at these locations create them. If there is no “.vim” directory in $HOME, create it and then create a empty file “.vimrc” there.

For windows users there will be two directories in C:\Program Files\vim : vim72 and vimfiles. Whatever syntax changes, colorscheme changes any plugins that you add, they all need to be done in “vimfiles”i directory. And for “Linux” users it is the $HOME/.vim directory. This is to keep your customization safe when you are going to upgrade to future versions of VIM. e.g. VIM is going to release VIM 7.3, when you install that your vim72 directory will get irrelevant but you will not loose your customizations as the new vim installation will takje it from vimfiles folder, and in Linux it will be “.vim” directory. Other reason is that you can mess as much as you can with your vim, and if you screw it totally, you just need to empty vimfiles/.vim folder and everything will be fine again, then start again 🙂

So let’s make some basic changes in vimrc file and start editing… For winodws the changes need to be done in “_vimrc” and for Linux the changes need to be done in “.vimrc”.

Suppose you need to set you font to Lucida Console, 10 pt, Bold. To set your font
In Linux:


And in Windows:



Want to set a default colorscheme. Add this line to your vim:
Check the Vim color schemes sample here. Download your chosen colorscheme and put it in $HOME/.vim/colors (Unix) or C:\Program Files\Vim\vimfiles\colors (Windows) folder.

I am putitng my vimrc file here just for refrence. The comments in it explain what the commands do.

Note: ” in vimrc is for putting comment.
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/vimrc_example.vim
"I am a Cobol Developer :)
autocmd FileType cobol set sw=4 sts=4 et sta tw=7
set nocompatible
"Behave like Microsoft.. CTRL+P, CTRL+P stuff
source $VIMRUNTIME/mswin.vim
behave mswin
set nu "Display line numbers
set ignorecase
set smartcase
set nobackup
set nowrap
"Set file formats to Unix, the other options
"are ff=dos or ff=mac. If you open a file from 
"Windows while your gvim ff=unix, you will see 
"^M characters. These ^M characters are Windows 
"End of Line characters
set fileformats=unix
set guioptions=agimrLt;
"The following commands will indent your code the right amount, 
"using spaces rather than tabs and automatically indent after you 
"start.>>> 
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
"<<< Complete indentation code
set autochdir
"Whenever I open or creat a file with extension pco, cpy or
"link, set its filetype to cobol, which will evoke proper
"syntax highlighting
au BufNewFile,BufRead *.pco set filetype=cobol
au BufNewFile,BufRead *.cpy set filetype=cobol
au BufNewFile,BufRead *.link set filetype=cobol
set diffexpr=MyDiff()
let cobol_legacy_code=1
let $p_h='/home/vivek/public_html'
set gfn=Consolas\ Bold\ 10
colorscheme ps_color
set cursorline
" Press Space to turn off highlighting and clear any message already displayed.
nnoremap   :nohlsearch:echo

"Tab navigation like firefox
nmap  :tabnext
nmap  :tabprevious
map  :tabprevious
map  :tabnext
imap  :tabpreviousi
imap  :tabnexti
nmap  :tabnew
imap  :tabnew
"Key map to copy the selected word and search it... Key Map: CTRL+F
map  /
imap  /
nmap  /
"Go Directly to Procedure Division in COBOL...
nmap  /PROCEDURE
imap  /PROCEDURE
"CTRL+Z --> Undo
imap  :u
nmap  :u
map  :u
"CTRL+O --> Open a new tab and also open a window to browse 
nmap  :tabnew\|browse e
imap  :tabnew\|browse e
"CTRL+W --> Selects word
nmap  bvw
imap  bvw
":%s#\s*\r\?$##
"ATL+M --> Trim trailing spaces
nnoremap  :%s#\s*\r\?$##
inoremap  :%s#\s*\r\?$##
noremap   :%s#\s*\r\?$##
"CTRL+Down Arrow --> Scroll page down
nnoremap  
inoremap  
noremap  
"CTRL+Up Arrow --> Scroll page up
nnoremap  
inoremap  
noremap  
"CTRL+F1 --> Toggle Shortcuts Bar 
nnoremap  :if &go=~#'T'set go-=Telseset go+=Tendif


This is just for getting a basic Vim running on your machine. Next I will write down how to do real work in Vim.
Let me know if I missing something in this post.

  1. Scott left a comment on June 22, 2010 at 7:02 PM

    Your friend probably never got to the point of setting up a configuration file. He most likely tried to type some text into the editor and immediately found he could not type anything, being totally unaware that Vim is a modal editor.

    The first thing I would do before explaining basic configuration is to explain normal mode and insert mode and stress the advantages of having all keys available as command keys and of keeping your hands on the keyboard. Offer a list of the various editing commands available at the press of one or two keys, and point out that his editor probably offers few of them and only as menu entries. Then I would point him immediately to the built-in Vim tutorial. Encourage him to persist by stressing that he will soon be as productive in Vim as he is in his current editor, but that soon after that his productivity will rise well beyond what is possible in his current editor. Then point him to some of the excellent on-line tutorials available and to the many plug-ins and tips available at Vim’s website.

    To give him an idea of the productivity gains that can be achieved, perform a simple demonstration by building a header box comment using Vim commands and have him duplicate it using his editor. Or, demonstrate Vim’s global search and replace capabilities with its sed-like substitute command. Stress the simplicity of navigating through a file with the /, ?, n, and N commands. Seeing is believing. If he can see how much easier Vim can be, he may want to keep trying to learn it.

  2. Yes Scott, giving an overview of insert, command, etc modes in VI would have been nice. But, my thought was at least get a basic editor up and running which may not look like an outdated one :), so that people will not just say – Oh! it looks like pre-historic, leave there are better looking editors there.

    In my next blogs, I will be writing about how to really use VI.
    btw, thanks for the comment and giving me some kind of road map on how to shape up this series of blogs 🙂

  3. it was very interesting to read.
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

Leave a Comment

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.