While I'm working, I use VIM as my text editor. Before you mock me too much, I do use Xemacs for some things, like reading mail (VM) and news (GNUS). When most [x]emacs users think of vi, they think of the /usr/bin/vi that ships with most systems. Jeez, if I were forced to use this version of vi, I'd hate it just as much as they did. Fortunately, VIM has much more to offer. Instead of ranting about why I think VIM is cool, check out this page for a dicussion of that exact thing. Anyway, the whole reason that I bring this up is that, like a zealous missionary, one of my goals in life it to convert regular old vi users to VIM users.
Please note that this page refers to Vim6. Some of the info on this page will work with previous versions, butthe structure of the custom syntax files works only with version 6.
Here is my .vimrc. I do some stuff that make life easier for me and I'll highlight the coolest things.
I map Ctrl-A and Ctrl-E to act like they do in emacs in every VIM mode. This includes insert and command line mode. I use those keys so much from my mailer (VM) and I've gotten really used to it. I still use 0 and $ though.
One of the great features of VIM is highlight search (hlsearch). This will highlight the text that matched the last search wherever it occurred in the file. This make things very easy to see visually. The only problem is what to do when you don't want to see the matching text stand out anymore. In this case, you may try to cancel the search but you can't really do that. What you really want to do is turn off highlight search. I've mapped F5 to toggle hlsearch on and off. This way, when I search for something, I can hit F5 to see all the matches and then hit it again when I'm done. I think I got the actual code to do this from a VIM newsgroup:
map <F5> :set hls!<bar>set hls?<CR>
When you compile VIM, you can edit feature.h and turn on the restoration of the xterm screen when you exit from VIM (just uncomment the
line). I like this behavior but many people do not. The VIM maintainers have decided (or it just has slipped past them) not to make this a runtime option. But restoring the xterm screen is just a terminal setting. This is how I do it:#define SAVE_XTERM_SCREEN
if &term == "xterm" || &term == "vt220" set t_ti=^[7^[[?47h set t_te=^[[2J^[[?47l^[8 endif
Remember that the ^[
is really an escape character which you can enter literally by typing Ctrl-V ESC.
I like to have the name of the file I'm editing in the title bar. However, I don't want it to be set unless the title can be restored upon exit. Previous to version 6, you didn't have many options to set and restore the window title unless the $WINDOWID environment variable was set. Now, it's much easier. Two things are required, one from the shell and one from the .vimrc
file. First, a few shell aliases:
alias title 'echo -n "^[]2;\!*^G"' alias cwdcmd title ${HOSTNAME}:'$cwd' alias vi 'vim -X \!*; cwdcmd;'
Remember that ^[
is literally an escape and ^G
is Control-G. I have an alias title
that will set the window title by echoing special characters that the X server will recognize. I also have an alias cwdcmd
that is run by my shell (tcsh) each time the current working directory changes. This makes my window title reflect the current directory when I'm just running shell commands (This is my preference to having having the cwd in the shell prompt.).
Lastly, I alias vi
to run vim with the -X
option. This tells vim not to open up a connetion to the X server. Since I don't use gvim, and I'm not going to use X to set/restore the window title, having X off means that vim will start quickly if I run it from an xterm over a slow connection and it also means that it will start period when it otherwise wouldn't. Ever su to root, run vim, and have it complain about not being able to open the display because of X permission problems? I have. Running -X
makes this go away. Yay! Lastly, the vi
alias runs cwdcmd
after it exits to restore the window title. It I didn't have this, I'd get the always annoying Thanks for Flying Vim
window title.
So now in the my .vimrc
, you'll see I unconditionally set the titlestring
variable:
autocmd BufEnter * let &titlestring = $HOSTNAME . ":" . expand("%:p:~")
Next, we set the options that lets all of this work:
if &term == "xterm" || &term == "vt220" " Let the title stuff work even if we don't open the DISPLAY set title set t_ts=^[]2; set t_fs=^G endif
We only set the title
option if we're in an xterm or a vt220 and we set the magical options t_ts
and t_fs
which tell vim how to set the title by echoing characters just like was set above in the title
alias.
So now title are always set and restored, and I never have to open a connection to the X server. Life is good.
I love the incremental search capability in VIM. However, it can be very cumbersome when editting a huge file. Using the new line2byte
function in Vim5.5, I can conditionally turn off incsearch
the number of bytes in the file greater than 10,000,000. The 10 million number is completely arbitrary and should be adjusted based on your own computer's performance.
function! IncSearch() if line2byte(line("$")) < 10000000 set incsearch else set noincsearch endif endfunction autocmd BufReadPost * call IncSearch()
Every language has its own style for comments. I like to map the dash key "-" to comment a line and the underscore key "_" to uncomment a line independent of the type of file I'm editting. This has turned out to be a very handy thing to have. To get this to work, all you have to do is setup an autocmd
to fire off a function that maps those keys depending on the filetype
. Here is the code that sets up comments for Perl, Csh, Sh, etc:
Notice that I also set thefunction! PoundComment() map - 0i#j map _ :s/^\s*# \=//g j set comments=:# endfunction autocmd Filetype perl call PoundComment() autocmd Filetype csh call PoundComment() autocmd Filetype sh call PoundComment()
comments
option so that comments are continued on the next line when I hit return.
Lastly, notice this line:
I have it in there so that my commentizing functions are re-called everytime I switch buffers. This is only necessary because when I switch back and forth between a regular file and aautocmd BufEnter * doautocmd FileType
:help
file, the commentizing mappings would get messed up.
Ok, I'll just say it. I think the default syntax files and colors that come with the VIM distribution really stink! Presented below are my syntax files and screenshots so you can decide for yourself.
First of all, I don't use the GUI version of VIM. I use the 16-color version of xterm that is noted in the VIM docs: http://dickey.his.com/xterm/xterm.html. I also compile it as noted in the VIM docs to get 16 colors:
./configure --disable-bold-color
Next, I have configured my standard xterm to look good with the colors I've chosen. That stuff goes in my .Xdefaults:
xterm*background: black xterm*foreground: gray90 xterm*cursorColor: gray90 xterm*color0: black xterm*color1: red xterm*color2: green xterm*color3: yellow xterm*color4: blue xterm*color5: magenta xterm*color6: cyan xterm*color7: white xterm*color8: burlywood1 xterm*color9: sienna1 xterm*color10: PaleVioletRed1 xterm*color11: LightSkyBlue xterm*color12: white xterm*color13: white xterm*color14: white xterm*color15: white xterm*colorMode: on xterm*dynamicColors: on
A few things to note. My standard background and foreground colors are black and grey90 respectively. I've chosen these because I've found them to be the easiest on my eyes. Having a white foreground seemed too harsh. Besides, it makes an actual white syntax highlight stand out more.
Also note that I've made sure that colors 0 through 7 match the standard ANSI colors for an 8 color terminal. This is so applications that use these color codes will look like they are supposed to. The only four colors I've added are 8 through 11. Since I don't use 12 through 15, I leave them white to minimize the number of colors I allocate in my color map.
Before I get into the actual screenshots, I have to give proper credit to my friend Chris Holt from whom I stole the colors.
The reason that I dislike the ditribution syntax files is not merely the colors. I believe that when it comes to syntax highlighting, a less is more philosophy is desired. I believe that when designing a syntax highlighting scheme, you should strive for two things. The first is that the screen should be pleasing to look at and not glaring and harsh. The second is that the colors you choose should help your eyes decompose the screen into its important parts. This means that files in C, Tcl and Perl should most importantly highlight the function and type names, not the type keywords of the language. For example, in the following C code:
typedef struct MyStruct { unsigned int foo; char *bar; double baz; } MyStruct;
I think the most important thing is for the name of the structure 'MyStruct' to be highlighted as shown above. The default files (and in fact the default files for [x]emacs) will highlight the 'typedef struct' part of the above code like this:
typedef struct MyStruct { unsigned int foo; char *bar; double baz; } MyStruct;
Why? Well, my guess is that it was very easy to write a regular expression to match 'typedef' and 'struct' while it was more difficult to write one to match the declared type name.
The same idea goes for function declarations and prototypes. My syntax files will highlight the name of the function while the default VIM syntax file as well as most of the ones I've seen for [x]emacs will highlight the return type and/or the word 'extern'. I have no idea why it's important to highlight the word 'extern'. I guess it's just easy to do.
Ok, enough of my ranting. Here is a zipped archive of my syntax files. Look through the screen shots below to decide if you like them. If you like what I've highlighted, just not the colors, then it would be easy to take what I've done and pick new colors. I've created my own versions of the syntax files for the following languages:
Here is a screenshot of a vim syntax file that you can use to see exactly what the 16 color look like. It is very similar to the colortest.vim that is provided in the distribution, except that I've modified it to use color numbers (0 through 15) as opposed to color names. I included my version of colortest.vim in the tarball above.