Overview
This document isn’t intended to start, fuel, or even contribute to the vim/Emacs
holy war. I used to be a nano
person, for crying out loud. Eventually I migrated to vi
simply because it
was readily available in my Unix environments.
If you’re looking for a more gentle intro to vim
, or you just want to dive
deeper into what I’ve done here, Google
can help.
With that bit of nonsense aside, I was asked by some of my colleagues to publish
some of my favorite vi
tricks, so here we go.
Configuration File
I’ll break my .vimrc
down into different bits:
Beautifying the Editor
"256-color Mode
:set t_Co=256
:colorscheme desert
"used to dark background, use cyan instead of blue for comments
:highlight darkComment ctermfg=6
:highlight cComment ctermfg=6
:highlight texComment ctermfg=6
:highlight shComment ctermfg=6
:highlight cshComment ctermfg=6
:highlight makeComment ctermfg=2
:highlight gnuplotComment ctermfg=6
"Basic Settings
:filetype on
:syntax on
"Highlight Searching
:set hlsearch
" Spelling
highlight clear SpellBad
highlight SpellBad term=standout ctermfg=1 term=underline cterm=underline
highlight clear SpellCap
highlight SpellCap term=underline cterm=underline
highlight clear SpellRare
highlight SpellRare term=underline cterm=underline
highlight clear SpellLocal
highlight SpellLocal term=underline cterm=underline
Tab/Line Widths and Syntax Highlighting
I write a decent amount of Python, and I am very nitpicky about whitespace in my
documents (and I do a lot of ASCII-art for diagrams and such in notes), so it
was natural for me to change my the \t
character to render as spaces. I also
hate having 8-space tabs, so I moved to 4-space tabs.
"Tab width is 4
:set ts=4
:set tabstop=4
:set shiftwidth=4
"Tabs = Spaces
:set expandtab
I generally try to stick to 80-character lines. Why? I like it. If you care, here’s a few links with more to say:
In general, I’d like to have vim
auto-wrap my lines at 80 characters, but only
when I’m writing prose. If I’m writing code, I generally want to be told that
I’m over 80 characters, but I don’t want it to auto-wrap. Auto-wrapping in code
is about as bad as Microsoft products trying to auto-correct things. As to why
you want to line break at 80 characters, I’ll let you try to edit prose that’s
over 80 characters that’s word-wrapped in vim
(soft instead of hard line
break). You’ll immediately understand.
So, here’s how I fixed that. I have a bit of code which renders an underline
under all characters after 80 in a line. This lets me see when my code line is
too long and allows me to actually decide that I want to fix it. This is
particularly important for things like Python. One thing to mention is that
rendering 80-character code in the LaTeX lstlisting
environment, especially
comments, will tend to overflow in a IEEE two-column layout. But… there you
have it.
"80 char limit
highlight OverLength term=underline cterm=underline
match OverLength /\%81v.\+/"
Now, how do I go about doing file-specific line wrapping? Well, fortunately
there’s stuff for that in vim
.
""asciidoc syntax highlighting
au BufRead,BufNewFile *.txt set filetype=asciidoc|set tw=80|setlocal spell spelllang=en_us
au! Syntax asciidoc source /opt/local/share/vim/vimfiles/syntax/asciidoc.vim
""spice syntax highlighting
au BufRead,BufNewFile *.spi set filetype=spice
au! Syntax spice source ~/.vim/syntax/spice.vim
""LaTeX 80 char limit
au BufRead,BufNewFile *.tex set tw=80|setlocal spell spelllang=en_us
Above, you can see how I’ve set up syntax highlighting and line widths for three different filetypes:
-
*.txt
: I want 80 character lines and syntax highlighting. You can also see that I’ve turned on American English spellchecking. -
*.spi
: These are SPICE files (code) where line breaks are important, so I want syntax highlighting only.vim
doesn’t know how to highlight SPICE natively, so I had to give it a syntax file. -
*.tex
: My LaTeX is basically prose, so I want 80 character lines.vim
already knows how to handle LaTeX files for highlighting so I’ll let it do its thing. I also turned on spellchecking, just like fortxt
files.
Various Tricks
vipgq
80-character lines are going to be all broken and jagged. Fortunately, vim
has a command for that. You can just do gq
on a highlighted block. It’s also
a movement command or whatever the cool kids are calling it these days.
Unfortunately, none of us have the time to manually do everything nowadays, so
you can use the vipgq
command.
vipgq
is visual mode, select inner paragraph, gq (reflow)
Anyway, what that mess actually does is highlight the entire paragraph you’re
working on and reformat it so that every line is as long as possible and within
the 80-character limit (or whatever you’ve set tw
to).
yypv$r?
When you’re writing asciidoc, you often need to put a special character under titles/section headings:
This Is My Title
================
Section Level 2
~~~~~~~~~~~~~~~
Section Level 3
^^^^^^^^^^^^^^^
In any case, you can copy the current line with yy
, paste it under the current
line with p
. This moves your cursor below the original line. Then you start
highlighting with v
, highlight to the end of the line with $
, and then
replace all the characters with r?
. In practice, ?
will be whatever
character you need.
80i-
Sometimes you’ll need to do some line drawing in your files. Remember you can
specify an integer before most vim
commands and then it will do whatever
command you wanted that many times. In this case, vim
will insert 80 -
characters, forming a nice horizontal rule.