Monday, February 22, 2010

My Vim setup

[updated: 11.4.10; 23.2.10; 17.1.10; 29.8.09; 31.8.09]

A description of my Vim setup, mainly for my own future reference, though could be of use for others. Intending to update this post when I make changes to the setup.


$VIM/_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
" for OpenVMS: sys$login:.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

set ch=2 " Make command line two lines high

set mousehide " Hide the mouse when typing text

" 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 if it wasn't on yet.
if !exists("syntax_on")
syntax on
endif

" 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

" Set nice colors
" background for normal text is light grey
" Text below the last line is darker grey
" Cursor is green, Cyan when ":lmap" mappings are active
" Constants are not underlined but have a slightly lighter background
" highlight Normal guibg=grey90
" highlight Cursor guibg=Green guifg=NONE
" highlight lCursor guibg=Cyan guifg=NONE
" highlight NonText guibg=grey80
" highlight Constant gui=NONE guibg=grey95
" highlight Special gui=NONE guibg=grey95

endif

set scrolloff=6
set shiftwidth=4
set softtabstop=4
set expandtab
set linebreak " so it won't linebreak in the middle of a word
set ignorecase smartcase
set columns=75
set lines=36
set backup " turn on backup
set backupdir=C:\james\Data\vimTempFiles " Set where to store backups
set dir=c:\temp " Set where to store swap files
set autoindent
set path+=c:\james\PhD\sources\notes\
set browsedir=current " open file browser in current dir

iabbrev nad and
iabbrev adn and
iabbrev teh the
iabbrev hte the
iabbrev taht that
iabbrev htose those
iabbrev waht what
iabbrev htings things
iabbrev somethign something
iabbrev somehting something
iabbrev followign following
iabbrev thigns things
iabbrev scuh such
iabbrev everythign everything
iabbrev htink think
iabbrev thign thing

" the following is so the XML escaping/URL encoding mappings
" in allml.vim are available globally

let g:allml_global_maps = 1

" for snipMate - set it so snippets can automatically use it
let g:snips_author = "James Cole"


set selectmode=key



" jump to start of next section (but dont wrap around past end of file)

function! NextSection()
call search("^:", "W")
endfunction

map <C-j> :call NextSection()<CR>




" jump to end of previous section (but dont wrap around past start of file)

function! PrevSection()
call search("^:", "bW")
endfunction


map <C-k> :call PrevSection()<CR>


" jump to start of this sections parent section

function! ParentSection()
let currHeadingLine = getline(search("^:", "bnW"))
let currHeadingLvl = strlen( matchstr( currHeadingLine, "^:\\+") )
let parentHeadingLvl = repeat(":", currHeadingLvl - 1)
let parentHeadingSearchPat = "^" . parentHeadingLvl . "[^:]"
call search(parentHeadingSearchPat, "bW")
endfunction

map <C-h> :call ParentSection()<CR>




set laststatus=2
highlight statusline guibg=LightGray guifg=DarkGray gui=none

set statusline=
set statusline+=%{getline(search(\"^:\",\ \"bnW\"))} " show curr heading
set statusline+=%= " show rest right justified.
set statusline+=\ \ %c\,\ %l\ (%L) " col, line (tot lines)



function! NoHLSearch(pat)
call search(a:pat)
set nohls
set hls
endfunction
command -nargs=1 S :call NoHLSearch(<f-args>)

" F4 - show list of headings in document
map <F4> :g/^:/<CR>

" F5 - toggle highlight search
map <F5> :set hls!<bar>set hls?<CR>

" F6 - change all the non-displayed chars to plain-text equivs
" (note how have to escape pipes between commands, and
" double escape pipes within replace commands)
map <F6> :%s/—/--/ge \| %s/…/.../ge \| %s/“\\|”/"/ge \| %s/‘\\|’/'/ge<CR>


" F7 - to condense selected lines
" very quick a dirty way to do it, should put all the words in a datastructure
" and handle words and their variants ending with s better
map <silent> <F7> :s/\<and\>/\&/ge \| '<,'>s/\<that\>/tt/ge \| '<,'>s/\<between\>/b\/ween/ge \| '<,'>s/\<with\>/w\//ge \| '<,'>s/\<information\>/info/ge \| '<,'>s/\<computation\>/compu/ge \| '<,'>s/\<are\>/r/ge \| '<,'>s/\<picture\>/pic/ge \| '<,'>s/\<to\>/2/ge \| '<,'>s/\<into\>/in2/ge \| '<,'>s/\<be\>/b/ge \| '<,'>s/\<defined\>/defn'd/ge \| '<,'>s/\<for\>/4/ge \| '<,'>s/\<However\>/Howevr/ge \| '<,'>s/\<definition\>/defn /ge \| '<,'>s/\<particular\>/partic/ge \| '<,'>s/\<thing\>/thng/ge \| '<,'>s/\<things\>/thngs/ge \| '<,'>s/\<which\>/whch/ge \| '<,'>s/\<part\>/prt/ge \| '<,'>s/\<argument\>/arg/ge \| '<,'>s/\<you\>/u/ge\| '<,'>s/\<want\>/wnt/ge \| '<,'>s/\<other\>/othr/ge \| '<,'>s/\<correspond\>/corresp/ge \| '<,'>s/\<for\>/4/ge\| '<,'>s/\<represents\>/reps/ge \| '<,'>s/\<above\>/abve/ge \| '<,'>s/\<character\>/char/ge \| '<,'>s/\<characters\>/chars/ge \| '<,'>s/\<required\>/req'd/ge \| '<,'>s/\<undefined\>/undef'd/ge \| '<,'>s/\<replacement\>/replacemnt/ge \| '<,'>s/\<destination\>/dest/ge \| '<,'>s/\<source\>/src/ge \| '<,'>s/\<your\>/ure/ge \| '<,'>s/\<languages\>/langs/ge \| '<,'>s/\<from\>/frm/ge \| '<,'>s/\<original\>/orig/ge \| '<,'>s/\<about\>/abt/ge \| '<,'>s/\<property\>/prop/ge \| '<,'>s/\<attribute\>/attrib/ge \| '<,'>s/\<question\>/qu/ge \| '<,'>s/\<different\>/diff/ge \| '<,'>s/\<philosophy\>/philos/ge \| '<,'>s/\<implementation\>/impl/ge \| '<,'>s/\<required\>/req'd/ge \| '<,'>s/\<elements\>/elems/ge \| '<,'>s/\<element\>/elem/ge<CR>

" F11 - to current phd writing directory
map <F11> :cd c:\james\phd\working\2010-1<CR>

" F12 - to phd sources notes directory
map <F12> :cd c:\james\phd\sources\notes<CR>


set splitbelow


" coz align.vim tells me i need to do this
" (it also says to set nocp, but im not sure if should)
filetype plugin on


set gfn=Lucida_Sans_Typewriter:h10:cANSI

" unicode so will display chars pasted from web-pages, PDFs etc (see usr_45.txt)
set encoding=utf-8


colorscheme morning

" insert '.x' at the start of the line - i've got syntax highlighting so
" that this line is 'crossed off' (as a plain text equiv so strikethrough)
" i use it for my planning docs and the 'to do' sorts of items in it
map <F8> <ESC>I.x <ESC>


set textwidth=75
set formatoptions+=a


let g:Tlist_Auto_Open=1
set updatetime=100
let tlist_txt_settings = 'txt;h:headings'
nnoremap <silent> <F9> :TlistToggle<CR>
" to update tagslist whenever saving
nnoremap <C-s> :w<CR> <BAR>:TlistUpdate<CR>
inoremap <C-s> <C-o>:w<CR><ESC>:TlistUpdate<CR>
" To not display the Vim fold column in the taglist window
let g:Tlist_Enable_Fold_Column = 0
" only show contents of the current file in current tab
let g:Tlist_Show_One_File = 1



cd c:\james\phd\working\2010-1


set guitablabel=%m\ %t

" 'frozen cursor' scrolling
unmap <C-y>
" ^ give C-Y its 'orig' behavior of scrolling down 1 line
map <A-k> <C-Y>
noremap <A-k> <C-Y>k
map <C-Y> <C-R> " this makes C-Y redo again,
noremap <A-j> <C-E>j




function! JamesTabLabel()
let label = ''
let bufnrlist = tabpagebuflist(v:lnum)

" Add '+' if one of the buffers in the tab page is modified
for bufnr in bufnrlist
if getbufvar(bufnr, "&modified")
let label = '+'
break
endif
endfor

" Append the number of windows in the tab page if more than one
" let wincount = tabpagewinnr(v:lnum, '$')
" if wincount > 1
" let label .= wincount
" endif
" if label != ''
" let label .= ' '
" endif
" ^ commented out because it includes taglist window in count
" an enhancement would be to filter it out

let fileName = bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])

" show only the filename (get rid of all the path details)
let fileName = substitute(fileName, ".*\\", "", "")

" 'escape' ampersands, so they 1) aren't hidden
" & 2) make character following them underlined
let fileName = substitute(fileName, "&", "&&", "g")

" don't show '.txt' extensions
let fileName = substitute(fileName, "\.txt$", "", "")

let label = label . fileName

return label
endfunction

set guitablabel=%{JamesTabLabel()}

set guioptions-=T " no toolbar


" pump out lines below the cursor
nnoremap <C-i> o<ESC>k



$VIM/vimfiles/ftdetect/txt.vim

au BufNewFile,BufRead *.txt setf txt


$HOME/ctags.cnf (unix/mac: $HOME/.ctags. ctags website).

--langdef=txt
--langmap=txt:.txt
--txt-kinds=+h
--regex-txt=/^(:.*)$/\1/h,headings/

($HOME is an environment variable ctags uses; that's where it looks for the configuration file - I've set mine to same dir as $VIM)


$VIM/vimfiles/syntax/txt.vim

syntax match lvl1Heading /^:[^:].*$/
syntax match lvl2Heading /^::[^:].*$/
syntax match lvl3Heading /^:::[^:].*$/
syntax match lvl4Heading /^::::[^:].*$/
syntax match lvl5Heading /^:::::.*$/

highlight lvl1Heading guifg=#0000FF
highlight lvl2Heading guifg=#0075FF
highlight lvl3Heading guifg=#0095FF
highlight lvl4Heading guifg=#60B9FF
highlight lvl5Heading guifg=#90DCFF


syntax match crossedOutLine /^\s*\.x.*$/
highlight crossedOutLine guifg=#AAAAAA



Add-on scripts
  • matchit
  • three related ones by Tim Pope
  • snipMate - TextMate-style snippets for Vim
    • though i haven't used this one much yet
  • taglist
    • for this i've installed ctags, and put it in my PATH environment variable (in my case in c:\programs\ctags58)
    • to get it to work properly with tabs (the 'tabs' for selecting different files, not the 'tabs' for indenting) apply the following patch
      --- plugin/taglist.vim 2008-08-13 19:04:48 +0000
      +++ plugin/taglist.vim 2008-08-27 19:24:49 +0000
      @@ -4097,6 +4097,12 @@
      " window. Used after entering a tab. If this is not done, then the folds
      " are not properly created for taglist windows displayed in multiple
      tabs.
      function! s:Tlist_Refresh_Folds()
      +
      + " Not needed when one file is displayed.
      + if g:Tlist_Show_One_File
      + return
      + endif
      +
      let winnum = bufwinnr(g:TagList_title)
      if winnum == -1
      return



How I've dealt with Vim's temp files

Details here.