Home

Resume

Tech

Tim's HOWTOs
GuruVim
GuruTcsh

Trip Reports

Europe Trip Report (2000)

Pictures

Still Here

OOFNet Cinema
OOF Music
FiSSH

External

ArtKomiChen.com
(Mom's Paintings)

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

VIM (VI iMproved) Tweaks

This is not meant to be a VIM tutorial or quick start guide. This is just a place where I document things that I found hard to find from VIM's documentation and/or through the website. Mostly these are tips for hard core VIM geeks.

Currently I'm using version 5.6. That may not reflect the latest version. But most of these things should work in 5.x.


My VIMRC file

Here's my _vimrc/.vimrc file. I do not keep a separate _gvimrc file and in general use this file for both UNIX and Windows NT VIM initialization - hence the weird backupdir line.

Notes on Options:


set viminfo='100,\"50,n~/.viminfo

It's easier to view the help on your options here (:help viminfo). The viminfo file maintains your histories (search, commandline, registers etc...) between file edits. It'll be like you never left VIM! :)

set statusline=%1*%F%*\ %y\ %2*%r%m%*\ %=%l/%L\ (%p%%)
set laststatus=2

My status line has filepath, ReadOnly Status (RO), Modified Status(+), Current Line / Total Lines, and percentage of file traversed. If you use my highlightings, the RO and + should appear in red. :set laststatus=2 ensures that all my windows will have statuslines.

if &term == "xterm-color"
set t_kb=^H
fixdel
endif

Take note that the ^H there is really a CTRL-H, so make sure you prepend a CTRL_V during your insertion of it in your vimrc file (hit CTRL-V CTRL-H so a literal control character is inserted). Depending on your terminal, this might fix your backspace problem. Do a help :term to see some specific terminal issues - this one can drive you batty!

:map <F2> :!p4 edit %<CR> :e! <CR>
:map <F3> :!p4 diff %<CR>
:map <F7> :!perl -c %<CR>
:map <F11> :!p4 add %<CR>
:map <F12> :!p4 submit %<CR>

These options are only useful if you are a Perl developer or use Perforce for source control. You can map them to whatever key you want. I've chosen <F7> here to stay consistent with the "compile" key in Windows/Dos compilers. Based on these I'm sure you can come up with the right command to map things for CVS as well.

If you are setting them explicitly in command mode during a VI session, after you type :map you can type the key you want to map it to. If the keyname doesn't appear, your terminal might not support that keystroke. Do a :set termcap to see what VIM thinks your terminal settings are.


Other Useful Initiations

These are some initiations that are not explicitly in my _vimrc/.vimrc startup file but that I cinlude from time to time. Put it in a file wherever you want and when you want to use just :source your_filename from the VIM command line.

function InitWin32Gui()
winpos 600 0
set guifont=Lucida_Console:h10
set lines=76
map <F6> :!form.pl %<CR>:e!<CR>
menu PopUp.-SEP3- :
menu PopUp.Format\ From\ Here gqG
endfunction

:autocmd GUIEnter * call InitWin32Gui()

This actually is in my NT _vimrc file, my only exception to the "one vimrc" file rule. And the only reason why is because I haven't figured out how to only call this in Windows. If someone knows, please let me know! :)

The :winpos 600 0 call explicitly sets the VIM windows position and the :set lines=76 explicitly sets the window size.

map <C-F3> :%s/<FORM .*>\|<INPUT .*>\|<SELECT .*>\|<OPTION .*>/&/gi

Occasionally I'll map this key. Basically if I'm trying to put together a script for an HTML page, this is useful when I have the HTML page in another window. Since I have :set hlsearch (highlight searches) it makes it easy to pick out various FORM tags. This command does a global search on the pattern and replaces the pattern with itself (&). I use the :%s instead of / because / doesn't implement the trailing /i option to ignore case. Hopefully they'll implement that soon.


Perl Tagging

This information was not the easiest to dig up. But if you want more information do a :help perl_info and some information should be there with additional links on where you can dig up more.

In general there are two things to do:

  • Generate a perl "tags" file. Here's the script (ptags.pl) referenced to in the VIM help files that generates perl tags.

    Here are the usage lines from the script:

    USAGE: ptags.pl [-mvwV] [-t <file>] <files>
    -m Merge with existing tags file.
    -t <file> Name of tags file to create. (default is 'tags')
    -v Include variable definitions. (variables mentioned at the start of a line)
    -V Print version information.
    -w Suppress "duplicate tag" warnings.
    <files> List of files to scan for tags

    If you want the tags file to include libaries just use the -m option to merge it in or create a new one with all the library paths (lib_path/*). Something like:

find /usr/lib/perl5 -name *.pm -type f -exec ptags.pl -m -t path_to_tags_file {} \;

  • Set the tags variable if needed in your .vimrc. (Do a :set tags to see what the tags search path currently looks for).
  • You're done! Edit any perl file and hover your cursor over a function name. Try hitting CTRL-] (Control - Right Square Bracket) in command mode, it should jump you to the function declaration. Press CTRL-T to get back to your original location/file. You can jump "in" to as many functions are you want, your return location is kept in a stack, so continual tapping of CTRL-T should return you back to your original file.

Limitations of Perl tagging (at least with the script here): Tagging uses the first match found in the tagging file. As a result only unique function names can be found (usually not a problem) but with the advent of Perl OO common functions like "new" do not work well with tagging. Also explicit object referencing via :: also does not work. However most function names are unique so this often is not a problem. It certainly makes life easier when going through a lot of perl files!


VIM Commands and Tips

Getting around faster:

*/#
(forward/backward) searches for the word under your cursor
|
Goes to 0th column
gf
Goto Filename under cursor. Uses isfname option to determine what characters are filenames. Useful if you're editing HTML.

Quick Windowing Commands:

CTRL W n
Start a new window
CTRL W c
Close a window
CTRL W j
Goto next window
CTRL W k
Goto previous window

Other Useful Commands:

ga or :as
Prints out the ascii value of the chacter under the cursor in decimal, hex, and octal.
CTRL-V [keystroke] Will insert the next chacter literally (useful for inserting raw CTRL characters).

Tips:

  • Remember VIM keeps your histories! Do your previous search by pressing / and then the up arrow to get your previous search. You can do the same thing with things entered on the : command line. You can uses this to repeat commands or global replacements.
  • Help and filename completion are available at the command line. For help, typing :help [partial-word] followed by a TAB will show you a help topic match. The same thing if you wish to :e another file. The TAB will present matching. choices from your partial-word. If you wish to match ALL choices, then you can try CTRL-A.
  • On the ex command line (:) a % refers to the current file, %< to current file without its extension. This is useful for key mappings or writing functions.


Looking for a Visual/Graphical Diff?

Look no further than the VIM package itself. I don't see this reference anywhere in the formal documentation but if you go poking around in the shared VIM directories you'll find a program called gvimdiff. On my system, I find it here:

/usr/local/share/vim/vim55/tools/gvimdiff

This is a csh script that uses diff and gvim to produce a great visual diff. It works in console mode as well if you can't run gvim, just symbolic link vimdiff to gvimdiff and it'll figure things out.


Last Updated: March 28, 2000
Maintainer: Timothy Chen <babyduck@massconfusion.com>

All Content is Copyrighted 1998-2003 by Timothy Chen
All Rights Reserved unless clearly stated otherwise.