VimHowto

Differences between revisions 2 and 3
Revision 2 as of 2006-01-16 20:37:02
Size: 2898
Editor: dhcp0534
Comment: update path to vimrc_example.vim
Revision 3 as of 2006-01-18 09:19:38
Size: 7570
Editor: ppp-70-251-243-233
Comment: Cleanup to bring out of CategoryCleanup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
'''This page is still a draft. Please don't link to it. Work will be done on this on this document to try and make it a comprehensive document for editing using vim on Ubuntu. Data from DocbookVim and VimSyntaxHighlighting will be merged here.''' '''This page is still a draft. Please don't link to it. Work will be done on this on this document to try and make it a comprehensive document for editing using vim on Ubuntu. Data from DocbookVim and VimSyntaxHighlighting has been merged here.'''
||<tablestyle="float:right; font-size: 0.9em;
width:40%; background:#F1F1ED; margin: 0 0 1em 1em;" style="padding:0.5em;">'''Contents'''[[BR]][[TableOfContents(3)]]||
Line 3: Line 4:
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. More details about Vim can be found on the Vim [http://www.vim.org homepage]. 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. 
Line 6: Line 9:
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 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
Line 11: Line 14:
== First Steps ==
If this is the first time you are using vim, please follow the tutorial for vim by typing '''vim-tutor''' in the console, or choose '''tutorial''' from the help menu.
= A Quick Introduction =
Line 14: Line 16:
= How to enable syntax highlighting in vim =
(cut paste from VimSyntaxHighlighting)
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.
Line 17: Line 18:
Turning syntax highlighting on is quite simple. Create a new file named ".vimrc" in your home folder, and insert the following text: 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.
Line 19: Line 20:
== 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,
Line 20: Line 28:
syntax on 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)
Line 23: Line 37:
Save the file and you're done. Any new instance of vim will automatically have syntax highlighting turned on. 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.
Line 35: Line 60:
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.
Line 37: Line 109:
attachment:IconsPage/PicDocs.png
(cut paste from DocbookVim)
Line 40: Line 110:
For more information about vim see [http://www.vim.org/about.php the vim about page]

'''VIM file type plugin "xmledit"'''
 Good for vim-lovers. See [http://www.vim.org/scripts/script.php?script_id=301 xmledit-plugin]

== .vimrc ==
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].
Line 50: Line 115:
    map! ,e <emphasis>
    map! ,p <para>
    map <F3> v/>^Mx
map! ,e <emphasis>
map! ,p <para>
map <F3> v/>^Mx
Line 57: Line 122:
I use that to add tags after the text is already written. We'll need that a lot to add formatting to current documents. I tend to just copy/paste them from the web site. 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.
Line 59: Line 124:
= Editing the Ubuntu Wiki =
Line 60: Line 126:
== More information == 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.
Line 62: Line 128:
A more elaborate example of a .vimrc file can be found at: 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.
Line 64: Line 130:
{{{
/usr/share/vim/vim63/vimrc_example.vim
}}}
Note: This wiki article was edited in vim :)
Line 68: Line 132:
You can find this example explained in detail at the [http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.2 Vim documentation].
= Online Sources =
You can find valuable information about vim at the following pages
 * The vim [http://www.vim.org homepage] is the place to go for scripts and plugins. The tips section has a RSS feed which gives many useful tips about vim.
 * The vim [http://vimdoc.sourceforge.net/ documentation] is the central location for documentation regarding vim. You can download the Vim User Manual, and the Vim Book there.
 * Vim [http://www.ukuug.org/events/linux2004/programme/paper-SMyers/Linux_2004_slides/vim_tips/ Power tips] give tips about effectively using vim.
 * The author of Vim, Bram Moolenaar has written an article called [http://www.moolenaar.net/habits.html Seven habits of effective text editing].
----

This page is still a draft. Please don't link to it. Work will be done on this on this document to try and make it a comprehensive document for editing using vim on Ubuntu. Data from DocbookVim and VimSyntaxHighlighting has been merged here.

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


CategoryCleanup

VimHowto (last edited 2008-08-06 16:32:11 by localhost)