VimHowto

Revision 4 as of 2006-01-18 09:25:50

Clear message

Vim is an advanced text editor that seeks to provide the power of the de-facto Unix editor 'Vi', with a more complete feature set. Vim is often called a "programmer's editor," and so useful for programming that many consider it an entire IDE. It's not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.

This article aims to get help you install vim, and give you a basic introduction to vim.

Installing vim

The console version of vim comes preinstalled with ubuntu, hence there is no need to install it. However, if you prefer to work with a GUI based vim, install the package vim-gtk from synaptic or type in a terminal

sudo apt-get install vim-gtk

A Quick Introduction

vim has a large learning curve. However, if you are comfortable with vim, you will become very efficient at manipulating text using the great features of vim.

You can start vim in console mode by typing vim at the terminal or vim in graphical mode by typing gvim. Doing so should bring up a blank screen, with details about vim. However, any attempts to type text will fail! Which brings us to the most confusing feature for beginners - modes.

Modes

One of the most confusing things about vim is that it has three modes.

  • Insert: To type text

  • Command:To issue commands. Also called as Normal mode.

  • Ex: To issue colon commands

The Insert mode is not default, you must press i to move into insert mode. Type some text in the screen. Press the <Esc> button to get out of insert mode into Command mode. The command mode is used to move about, and to manipulate text, sometimes in interesting ways. The Ex mode is used to issue colon commands, which is used for operations like saving, search & replace and configuring vim. Save the text you just typed in by going to the Ex mode by pressing : from the normal mode and typing :w filename<Enter>. Quit vim by executing the colon command :q. To summarize,

vim (to start vim)
i (to insert text)
<type text> 
<Esc> (to come to normal mode)
:w filename (to save the text to the file 'filename')
:q (to quit the file)
vim filename (to open the file you just saved directly in vim)

However, it is best to learn vim by using it. You can quickly learn the basics of vim by using the inbuilt vim tutorial, by typing vim-tutor in the terminal.

Configuration

vim is a highly configurable editor, and it is best to configure vim to your liking as vim by default has all the nice features turned off. A list of files and their locations are given below.

  • ~/.vimrc is the vim configuration file which vim reads on startup
  • ~/.gvimrc is the gvim configuration file which gvim reads on startup. It's best to keep only gui specific settings here, as it will take preference over the settings in your .vimrc file.
  • ~/.vim/ is the directory in which the user can add utility plugins, syntax highlighting plugins, and indent plugins.

Enable Syntax Highlighting

Turning syntax highlighting on is quite simple.

If you want to just enable syntax highlighting for a session, you can simply issue a colon command

:syn on

Syntax highlighting can be turned off by issuing another 'colon' command

:syn off

To make this permanent everytime you open a file, just add the following line to your vimrc.

syntax on

Enable Autoindenting

To enable Auto-Indenting of code, just type the following colon command.

:set ai

The code you type will indent automatically. If it does not indent correctly, you might need to obtain a indenting plugin for the language you are writing in from [http://www.vim.org vim] site.

TO make this permanent, add the following lines to your vimrc.

filetype indent on
set autoindent

Sample .vimrc file

Below is a basic .vimrc file with basic configuration. Please note that lines beginning with the character " are comments.

" Turn on line numbering. Turn it off with "set nonu" 
set nu 

" Set syntax on
syntax on

" Indent automatically depending on filetype
filetype indent on
set autoindent

" Case insensitive search
set ic

" Higlhight search
set hls

" Wrap text instead of being on one line
set lbr

" Change colorscheme from default to delek
colorscheme delek

You can also learn more by looking at a more elaborate vimrc file at /usr/share/vim/vim63/vimrc_example.vim. You can find this example explained in detail at the [http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.2 Vim documentation].

You can also find several .vimrc files online on the [http://www.dotfiles.com/index.php3?app_id=9 dotfiles] website.

Editing docbook documents with vim

To contribute to the Ubuntu Documentation, you will need to use the docbook format. If so, you might be interested in the VIM filetype plugin [http://www.vim.org/scripts/script.php?script_id=301 xmledit].

Add the following to your ~/.vimrc

map! ,e <emphasis>
map! ,p <para>
map <F3> v/>^Mx

If you are at the beginning of an opening XML tag you can just press F3 and the tag gets cut to the buffer. Go the end of the section and press 'p' (=paste) and it will be appended after the current char.

This is useful to add tags after the text is already written. A typical usecase is when it is necessary to add formatting to current documents which have been copy/pasted from a web site.

Editing the Ubuntu Wiki

You can use VIM to edit the articles in the Ubuntu wiki. Since we use the MoinMoin engine, we can use the Vim syntax plugin [http://moinmoin.wikiwikiweb.de/VimHighlighting moin] to get syntax highlighhting for the wiki text in vim. Enable the plugin by using the instructions in the previous link.

To use, just click on the More Actions: drop down list on the page you want to edit. Then select Show Raw Text. Copy the source of the wiki page you are editing, and paste it in vim. If you are using the console version of vim, it might be a good idea to turn off autoindenting as vim autoformats the text as you paste it. This is not a issue in the gui version of vim.

Note: This wiki article was edited in vim Smile :)

Online Sources

You can find valuable information about vim at the following pages