>> Noel O'Boyle

Vim

Blurb on Vim

How to colorize code examples on a web page

Open the file in vim, and turn on syntax highlighting if necessary:

:syntax enable

If you then enter the following command, Vim creates the necessary HTML, which appears in a second pane on your screen:

:TOhtml
Older versions of Vim used the following instead:
:runtime! syntax/2html.vim

Replace the yellow color with a brown to increase readability on a white background (check out this chart of HTML colour codes).

:%s/ffff00/cc9933/g

To automate this process, map CTRL+H as shown in the .vimrc below. Next, open the .html file into which you wish to insert the colourised code, and navigate to the position where you want to insert the code. Then open up the file with the code in a split screen (CTRL+W,N followed by :e myfile). Then just press CTRL+H. The required html code will be entered into your original .html file.

Useful .vimrc settings

Here's what I have...


" Settings for python programming
au BufNewFile,BufRead *.py set tabstop=4
au BufNewFile,BufRead *.py set shiftwidth=4
au BufNewFile,BufRead *.py set autoindent
au BufNewFile,BufRead *.py set expandtab
au BufNewFile,BufRead *.py source ~/Tools/VimPlugins/python.vim

" Incremental search
" (as you type in the search query, it will show you 
" whether your query currently matches anything)
set incsearch

" To enable syntax-highlighting
syntax enable

" Shows the matching bracket when entering expressions
" (you'll never miss one again!)
set showmatch

" Maps CTRL+H (for HTML) to the command for creating HTML
" representing the coloured text shown in the Vi window
" Note: if 'cc9933' isn't present, the resulting error
" message is suppressed by the 'e' flag of 's'ubstitute
map <C-H> :runtime! syntax/2html.vim<Enter>:%s/cc9933/cc9933/ge<Enter>:/<pre>/,/<\/pre>/ y<Enter>:q!<Enter>:q!<Enter>p


" Help for viminfo is at:  :he 'viminfo'
"   '10  : marks will be remembered for up to 10 previously edited files
"   "100 : will save up to 100 lines for each register
"   :20  : up to 20 lines of command-line history will be remembered
"   %    : saves and restores the buffer list
"   n... : where to save the viminfo files
set viminfo='10,\"100,:20,%,n~/.viminfo
autocmd BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

Useful plugins

If you take a look at the most popular plugins for Vim, the taglist plugin is far and away the most highly-rated. It makes it easy to navigate between different functions/classes in your program.

Once installed, type :Tlist to show the list of tags. Useful shortcuts include 'p' for preview, 'u' for update and 'x' for toggling the zoom.

I also use python.vim, which adds a couple of useful commands, and related menus (in gvim), that are useful in Python programming (for example, selecting a block, indenting a block, commenting out a section).

I also like colorize.vim, which changes the screen colour (in gvim) depending on whether you're in insert mode or not. Note that in Linux, you need to run dos2unix on colorize.vim, and then make some small changes to the file (read the comments in the file for more information).

Some other useful plugins are LaTeX-Suite and xmledit. The former is available as a Debian package, vim-latexsuite. In fact, a lot of these plugins are available through the Debian package, vim-scripts.

Useful commands I keep forgetting