Vim Editor HOWTO (Vi Improved with syntax color highlighting) <!-- chapt change Vim Editor HOWTO Vi Improved with syntax color highlighting --> <author>Al Dev (Alavoor Vasudevan) <htmlurl url="mailto:alavoor@yahoo.com" name="alavoor@yahoo.com"> <date>v1.0, 14 October 1999 <abstract> This document is a guide to very quickly setup Vim editor on Linux or Unix systems. The information here will improve the prodcutivity of programmer since Vim editor supports syntax color highlighting and bold fonts which improves the "readability" of program code. The information in this document applies to all operating sytems where Vim works that is - Windows 95/NT and all flavors of Unix like Linux, Solaris, HPUX, AIX, SCO, Sinix, BSD, SCO, etc.. </abstract> <!-- Table of contents --> <toc> <!-- Begin the document --> <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt>Introduction --> <sect>Introduction <p> Vim editor stands for 'Vi Improved'. Vi is the most popular and powerful editor in the Unix world. A good editor will improve the productivity of the programmer. Vim supports color syntax highlighting of program code and also different fonts like normal or bold. <p> To use Vim install the following rpm packages on linux - <code> rpm -i vim*.rpm OR do this - rpm -i vim-enhanced*.rpm rpm -i vim-X11*.rpm rpm -i vim-common*.rpm rpm -i vim-minimal*.rpm </code> You can see the list of files the vim rpm installs by - <code> rpm -qa | grep ^vim | awk '{print "rpm -ql " $1 }' | /bin/sh | less </code> and browse output using j,k, CTRL+f, CTRL+D, CTRL+B, CTRL+U or using arrow keys, page up/down keys. See 'man less'. For other flavors of unix download the source code file <code> zcat vim.tar.gz | tar -xvf - cd vim-5.5/src ./configure --enable-gui=motif make make install </code> For Windows 95/NT, download zip file and install clicking on setup. <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt> Setup vim init files --> <sect> Setup vim init files <p> To enable the syntax color highlighting you should copy the vimrc file to your home directory. This will also put the "Syntax" Menu with gvim command. You can click on Syntax Menu and select appropriate languages like C++, Perl, Java, SQL, ESQL etc.. <CODE> cd $HOME cp /usr/doc/vim-common-5.3/gvimrc_example ~/.gvimrc cp /usr/doc/vim-common-5.3/vimrc_example ~/.vimrc </CODE> Comment lines in .vimrc begin with double-quotes ("). You can customize vim by editing the file $HOME/.vimrc and put the following lines - <CODE> "set guifont=9x15bold set guifont=8x13bold "set guifont=7x14bold "set guifont=7x13bold </CODE> The tabstop is number spaces TAB will put while editing with gvim. The shiftwidth is the number spaces the lines will be shifted with ">>" or "<<" vi commands. Refer to Vi tutorials more details. To set the tabstop and shiftwidth - <CODE> set tabstop=4 set shiftwidth=4 set nowrapscan set ignorecase </CODE> <!-- ******************************************* ************ End of Section *************** ******************************************* --> <sect1> Xdefaults parameters <p> You can set some of the Vim properties in Xdefaults file. <bf> WARNING: </bf><it>Do not put <bf>Vim*geometry</bf> it will break the gvim menu, use <bf>Vim.geometry</bf> instead</it> Edit the $HOME/.Xdefaults and put the following lines: <CODE> ! GVim great Colors. Vim*useSchemes: all Vim*sgiMode: true Vim*useEnhancedFSB: true Vim.foreground: Black !Vim.background: lightyellow2 Vim*background: white ! Do NOT use Vim*geometry , this will break the menus instead ! use Vim.geometry. Asterik between Vim and geometry is not allowed. ! Vim.geometry: widthxheight Vim.geometry: 88x40 !Vim*font: -misc-fixed-medium-r-normal--20-200-75-75-c-100-iso8859-15-*5 Vim*menuBackground: yellow Vim*menuForeground: black </CODE> You must re-start the X manager to make changes take effect. You can also edit the ~/.gvimrc file to change the background colors <CODE> gvim $HOME/.gvimrc </CODE> <!-- ******************************************* ************ End of Section *************** ******************************************* --> <sect1> Sample vimrc file <p> The sample vimrc from /usr/doc/vim-common-5.3/vimrc_example is as follows: <tscreen><verb> " Vim " An example for a vimrc file. " " To use it, copy it to " for Unix and OS/2: ~/.vimrc " for Amiga: s:.vimrc " for MS-DOS and Win32: $VIM\_vimrc set nocompatible " Use Vim defaults (much better!) set bs=2 " allow backspacing over everything in insert mode set ai " always set autoindenting on set backup " keep a backup file set viminfo='20,\"50 " read/write a .viminfo file, don't store more " than 50 lines of registers " In text files, always limit the width of text to 78 characters autocmd BufRead *.txt set tw=78 " For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries " let & guioptions = substitute(& guioptions, "t", "", "g") " Don't use Ex mode, use Q for formatting map Q gq " Switch syntax highlighting on, when the terminal has colors " Also switch on highlighting the last used search pattern. if & t_Co > 2 || has("gui_running") syntax on set hlsearch endif augroup cprog " Remove all cprog autocommands au! " When starting to edit a file: " For *.c and *.h files set formatting of comments and set C-indenting on. " For other files switch it off. " Don't change the order, it's important that the line with * comes first. autocmd BufRead * set formatoptions=tcql nocindent comments& autocmd BufRead *.c,*.h set formatoptions=croql cindent comments=sr:/*,mb:*,el:*/,:// augroup END augroup gzip " Remove all gzip autocommands au! " Enable editing of gzipped files " read: set binary mode before reading the file " uncompress text in buffer after reading " write: compress file after writing " append: uncompress file, append, compress file autocmd BufReadPre,FileReadPre *.gz set bin autocmd BufReadPost,FileReadPost *.gz let ch_save = & ch|set ch=2 autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip autocmd BufReadPost,FileReadPost *.gz set nobin autocmd BufReadPost,FileReadPost *.gz let & ch = ch_save|unlet ch_save autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r") autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r autocmd FileAppendPre *.gz !gunzip <afile> autocmd FileAppendPre *.gz !mv <afile>:r <afile> autocmd FileAppendPost *.gz !mv <afile> <afile>:r autocmd FileAppendPost *.gz !gzip <afile>:r augroup END </verb></tscreen> <!-- ******************************************* ************ End of Section *************** ******************************************* --> <sect1> Sample gvimrc file <p> The sample gvimrc from /usr/doc/vim-common-5.3/gvimrc_example is as follows: <CODE> " Vim " An example for a gvimrc file. " The commands in this are executed when the GUI is started. " " To use it, copy it to " for Unix and OS/2: ~/.gvimrc " for Amiga: s:.gvimrc " for MS-DOS and Win32: $VIM\_gvimrc " Make external commands work through a pipe instead of a pseudo-tty "set noguipty " set the X11 font to use " set guifont=-misc-fixed-medium-r-normal--14-130-75-75-c-70-iso8859-1 " Make command line two lines high set ch=2 " Make shift-insert work like in Xterm map <S-Insert> <MiddleMouse> map! <S-Insert> <MiddleMouse> " Only do this for Vim version 5.0 and later. if version >= 500 " I like highlighting strings inside C comments let c_comment_strings=1 " Switch on syntax highlighting. syntax on " Switch on search pattern highlighting. set hlsearch " For Win32 version, have "K" lookup the keyword in a help file "if has("win32") " let winhelpfile='windows.hlp' " map K :execute "!start winhlp32 -k <cword> " . winhelpfile <CR> "endif " Hide the mouse pointer while typing set mousehide " Set nice colors " background for normal text is light grey " Text below the last line is darker grey " Cursor is green " Constants are not underlined but have a slightly lighter background highlight Normal guibg=grey90 highlight Cursor guibg=Green guifg=NONE highlight NonText guibg=grey80 highlight Constant gui=NONE guibg=grey95 highlight Special gui=NONE guibg=grey95 endif </CODE> <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt> Color Syntax init files --> <sect> Color Syntax init files <p> Instead of using "Syntax" menu you can also manually source in the syntax file. Edit the file with gvim and at : (colon) command give so command. For example - <CODE> gvim foo.pc :so $VIM/syntax/esqlc.vim </CODE> The syntax source files are at /usr/share/vim/syntax/*.vim. Vim supports more than 120 different syntax files!! <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt> VIM Usage --> <sect> VIM Usage <p> You can use Vim in two modes - one with GUI and other without GUI. To use GUI use command - <CODE> gvim foo.cpp </CODE> To use non-gui mode give - <CODE> vim foo.cpp OR plain vanilla mode vi foo.cpp </CODE> <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt> Online VIM help --> <sect> Online VIM help <p> See the online man pages. At unix shell prompt type 'man vim' and 'man gvim'. Or inside the gvim session type :help to get the help page. <tscreen><verb> VIM - main help file Move around: Use the cursor keys, or "h" to go left, "j" to go down, "k" to go up, "l" to go right. Close this window: Use ":q<Enter>". Get out of Vim: Use ":qa!<Enter>" (careful, all changes are lost!). Jump to a subject: Position the cursor on a tag between |bars| and hit CTRL-]. With the mouse: ":set mouse=a" to enable the mouse (in xterm or GUI). Double-click the left mouse button on a tag between |bars|. jump back: Type CTRL-T or CTRL-O. Get specific help: It is possible to go directly to whatever you want help on, by giving an argument to the ":help" command |:help|. It is possible to further specify the context: WHAT PREPEND EXAMPLE ~ Normal mode commands (nothing) :help x Visual mode commands v_ :help v_u Insert mode commands i_ :help i_<Esc> command-line commands : :help :quit command-line editing c_ :help c_<Del> Vim command arguments - :help -r options ' :help 'textwidth' |howto.txt| how to do the most common things |tutor| 30 minutes training course for beginners |copying| About copying Vim and Uganda |www| Vim on the World Wide Web |bugs| Where to send bug reports list of documentation files: |intro.txt| introduction to Vim |help.txt| overview and quick reference (this file) |index.txt| alphabetical index for each mode |quotes.txt| remarks from users of Vim |todo.txt| known problems and desired extensions |autocmd.txt| automatically executing commands on an event |change.txt| delete and replace text </verb></tscreen> <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt> Vim Home page and Vim links --> <sect> Vim Home page and Vim links <p> The home page of vim is at <url url="http://www.vim.org"> and mirror site in US is at <url url="http://www.us.vim.org"> Vim FAQ is at <url url="http://www.grafnetix.com/~laurent/vim/faq.html"> and at <url url="http://www.vim.org/faq"> Eli's Vim Page at <url url="http://www.netusa.net/~eli/src/vim.html"> The Vi Lovers Home Page <url url="http://www.cs.vu.nl/~tmgil/vi.html"> Vim Reference Guide at <url url="http://scisun.sci.ccny.cuny.edu/~olrcc/vim/"> Vim mailing list at <url url="http://www.findmail.com/listsaver/vimannounce.html"> and <url url="http://www.vim.org/mail.html"> Mailing list archives are kept at: <itemize> <item> <url url="http://www.egroups.com/group/vim"> <item> <url url="http://www.egroups.com/group/vimdev"> <item> <url url="http://www.egroups.com/group/vimannounce"> </itemize> Vim macros <url url="http://www.grafnetix.com/~laurent/vim/macros.html"> <!-- ******************************************* ************ End of Section *************** ******************************************* <chapt change> Other Formats of this Document --> <sect> Other Formats of this Document <p> This document is published in 10 different formats namely - DVI, Postscript, Latex, LyX, GNU-info, HTML, RTF(Rich Text Format), Plain-text, Unix man pages and SGML. <itemize> <item> You can get this HOWTO document as a single file tar ball in HTML, DVI, Postscript or SGML formats from - <url url="ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO/other-formats/"> or <url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO/other-formats/"> <item>Plain text format is in: <url url="ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO"> or <url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO"> <item>Translations to other languages like French, German, Spanish, Chinese, Japanese are in <url url="ftp://sunsite.unc.edu/pub/Linux/docs/HOWTO"> or <url url="ftp://metalab.unc.edu/pub/Linux/docs/HOWTO"> Any help from you to translate to other languages is welcome. </itemize> The document is written using a tool called "SGML tool" which can be got from - <url url="http://www.xs4all.nl/~cg/sgmltools/"> Compiling the source you will get the following commands like <itemize> <item>sgml2html vim-howto.sgml (to generate html file) <item>sgml2rtf vim-howto.sgml (to generate RTF file) <item>sgml2latex vim-howto.sgml (to generate latex file) </itemize> This document is located at - <itemize> <item> <url url="http://sunsite.unc.edu/LDP/HOWTO/VIM-HOWTO.html"> </itemize> Also you can find this document at the following mirrors sites - <itemize> <item> <url url="http://www.caldera.com/LDP/HOWTO/VIM-HOWTO.html"> <item> <url url="http://www.WGS.com/LDP/HOWTO/VIM-HOWTO.html"> <item> <url url="http://www.cc.gatech.edu/linux/LDP/HOWTO/VIM-HOWTO.html"> <item> <url url="http://www.redhat.com/linux-info/ldp/HOWTO/VIM-HOWTO.html"> <item> Other mirror sites near you (network-address-wise) can be found at <url url="http://sunsite.unc.edu/LDP/hmirrors.html"> select a site and go to directory /LDP/HOWTO/VIM-HOWTO.html </itemize> In order to view the document in dvi format, use the xdvi program. The xdvi program is located in tetex-xdvi*.rpm package in Redhat Linux which can be located through ControlPanel | Applications | Publishing | TeX menu buttons. <tscreen><verb> To read dvi document give the command - xdvi -geometry 80x90 howto.dvi And resize the window with mouse. See man page on xdvi. To navigate use Arrow keys, Page Up, Page Down keys, also you can use 'f', 'd', 'u', 'c', 'l', 'r', 'p', 'n' letter keys to move up, down, center, next page, previous page etc. To turn off expert menu press 'x'. </verb></tscreen> You can read postscript file using the program 'gv' (ghostview) or 'ghostscript'. The ghostscript program is in ghostscript*.rpm package and gv program is in gv*.rpm package in Redhat Linux which can be located through ControlPanel | Applications | Graphics menu buttons. The gv program is much more user friendly than ghostscript. Ghostscript and gv are also available on other platforms like OS/2, Windows 95 and NT. <tscreen><verb> To read postscript document give the command - gv howto.ps To use ghostscript give - ghostscript howto.ps </verb></tscreen> You can read HTML format document using Netscape Navigator, Microsoft Internet explorer, Redhat Baron Web browser or any other web browsers. You can read the latex, LyX output using LyX a "X-Windows" front end to latex. <!-- ******************************************* ************ End of Section *************** ******************************************* --> </article>