" ============================ " Some contributions by geoff mcnamara " Some contributions from Matt Spong " Some taken from Sven Guckes guckes@vim.org (guckes@math.fu-berlin.de) " Some from an on-line ibmdeveloperworks post: " Damian Conway, Dr. (damian@conway.org), CEO and Chief Trainer, Thoughtstream " May 6 2009 Scripting the Vim editor, Part 1 " " NOTES: " * install vim-full " * colors will fail unless TERM allows it (vt100 does not) " * Comments start with a double quote mark " * One settings is set for a dark background - you may have a light " colored background that requires a different setting " - see [ set background=dark ] below " * This script also tries to use templates for some scripting (files " ending in .sh for example) - see [ autocmd BufNewFile ] below " =========================== " $Id: sample.vimrc,v 1.5 2009-07-03 11:12:15 geoffm Exp $ " Last update " this stuff below can bust things so I took it out "set mousehide "set mousefocus "set mouse=a "set mousem=popup " Movement " fx (goes forward to the next "x") " Fx (goes backward to the previous "x") " tx (goes forward to the char before the next "x") " 2tx (goes to the char just before the second "x") " dG deletes all lines from cursor to EOF " :g/^#/s/foo/bar/g <-finds every line starting with "#" and replaces all " foo with bar " use this to see your runtime path " :set runtimepath " keep in mind the ~/.vim dir and the ~/.vim/after dir " nmap = normal mode map " imap = insert mode map "===== set up a menu and map to F4 ========== "if has("menu") " source $VIMRUNTIME/menu.vim " set wildmenu " "set cpo-=< " set wcm= " map :menu "endif set spell "noremap :so `$VIMRUNTIME/tools/vimspell.sh %` "highlights errors "noremap :syntax clear SpellErrors " unhighlights errors "if has("syntax") " function Swapcolor() " if exists("g:syntax_on") " syntax off " set nohlsearch " else " syntax on " set hlsearch " endif " endfunction "endif function! ToggleSyntax() if exists("g:syntax_on") syntax off else syntax enable endif endfunction nmap ;s :call ToggleSyntax() nmap function! CapitalizeCenterAndMoveDown() s/\<./\u&/g "Built-in substitution capitalizes each word center "Built-in center command centers entire line +1 "Built-in relative motion (+1 line down) endfunction nmap \C :call CapitalizeCenterAndMoveDown() nmap ]] :let &tabstop += 1 nmap [[ :let &tabstop -= &tabstop > 1 ? 1 : 0 "===== set stuff ============= set fileformat=unix set nosecure " allows running of extended macros (some are below) "set nocompatible " turns off vi compatibility set autowrite set autoread set noerrorbells " thank goodness! set visualbell "set t_vb= " this sets the terminal visual bell off! set tabstop=2 " so it will fit on the screen (list mode can help here too) set shiftwidth=2 " same reason as above set backspace=2 " matches what is going on above "set smarttab " this generally works well for me set nosmartindent " helps when pasting text "set ai " sets autoindenting on " bad news when pasting text set noautoindent " stoopid copy-past problems without this set expandtab " essential! - trashes tabs and replaces withspaces "set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [LINE=%04l,%04v][%p%%]\ [LEN=%L] set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [LINE=%04l,%04v][%p%%] set laststatus=2 " always show laststatus line set showmode " is this html, perl, C++, script blah blah blah set showcmd " always show command set ruler " I want to see where I am over in the bottom right corner " report: show a report when N lines were changed. " report=0 thus means "show all changes"! " Not used if statusine is set (see above) set report=0 " set list " shows tabs and end of line symbols " set nu " set numberred lines " set verbose=9 "set showmatch "set suffixes=.bak,~,.o,.h,.info "set gfn=-adobe-courier-medium-r-*-*-*-120-*-*-*-*-*-* " blink matching parens! "set showmatch " Allow jump commands for left/right motion to wrap to previous/next " line when cursor is on first/last character in the line: "set whichwrap=<,>,h,l,[,] set wrapmargin=0 set wrapscan " continue searching after hitting bottom "set comments=b:#,:%,fb:-,n:>,n:) set comments= " blank this out "setlocal comments= " blank this out setlocal comments-=:# " keep comments *except* the # set commentstring= " blank this out "setlocal fo-=t fo-=r fo-=q fo-=o " will prevent vim from doing anything to your comments automatically. see :help fo-table set noloadplugins set history=50 set report=0 set ttymouse=xterm2 set viminfo='20,"50 "filetype plugin off set fileencoding=utf-8 "set incsearch " jumps to search word as you type - can be annoying set hidden " allow hidden buffers "set iskeyword=@,48-57,_,192-255,-,.,:,/,@-@ " wildchar the char used for "expansion" on the command line " default value is "" but I prefer the tab key: "set wildchar= " If you prefer backspace and delete in Insert mode to have the old behavior, " put this line in your vimrc: " inoremap   " And you may also want to add these, to fix the values for and : " set t_kb=^H " set t_kD=^? " (Enter ^H with CTRL-V CTRL-H and ^? with CTRL-V CTRL-? or .) " If the value for t_kb is correct, but the t_kD value is not, use the ":fixdel" " command. It will set t_kD according to the value of t_kb. This is useful if " you are using several different terminals. |:fixdel| " When ^H is not recognized as or , it is used like a backspace. "set t_kb= " fixes the backspace issue "set t_kD=[3~ " fixes the delete key issue """""""""""""""""""""""""""""""""""""" " force using hjkl$ " :noremap :echoerr "Use k instead!"$ " :noremap :echoerr "Use j instead!"$ " :noremap :echoerr "Use l instead!"$ ":noremap :echoerr "Use h instead!"$, """"""""""""""""""""""""""""" " To start, we must first tell vim where our dictionary is located. " This is done via the 'dictionary' option. Below is an example. " Your location may vary. See :help 'dictionary' " for hints as to where you should look. " " :set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words " " Now, to use this list we have to enter insert mode completion. " This is done by hitting CTRL-X " while in insert mode. Next, you have to specify what you want to complete. " For dictionaries use CTRL-K. Once in this mode the keys CTRL-N " and CTRL-P will cycle through the matches. So, to complete the " word "acknowledgeable" I would do the following in insert mode: " " acknow " " It can be cumbersome to type CTRL-X CTRL-K for many different completions. " So, vim gives us a shortcut. While in insert mode CTRL-N and CTRL-P " will cycle through a predetermined set of completion sources. " By default, dictionary completion is not a part of this set. This set is " defined by the 'complete' option. Therefore, we must add dictionary " to this as shown below: " " :set complete-=k complete+=k " " Now, while in insert mode we can type the following to complete " our example: " " acknow """""""""""""""""""""""""""""""""""""" "set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words "set complete-=k complete+=k if has("folding") set foldmethod=marker "uses the default {{{ to mark VIM folds endif "use the z commands to control folds zc=closes them zo=opens them " for outlining set this "set foldmethod=indent " set ic " set ignore case change with set noic "======= abbreviations ========== "Note: means Ctrl-R inserts contents of a numbered or named register. "iab MYRULER 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789- "iab _ruler 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789- "iab MYDATE =strftime("%b %d %Y") "iab MYDTIME =strftime("%Y%m%d-%T") " insert mode: "_date " "iab _date =strftime("%b %d %Y ") "iab _dtime =strftime("%b %d %Y %H:%M ") " stooopid keyboard :-) - I always miss type the word the "iab hte the "========= maps =============== " make _G grep for the word under the cursor in all " .c, .cc, .h, etc., files in the current directory and then " make :cn, :cp, :cl, etc... use the list grep returns "map _G :let efsave=&eflet &ef=tempname()exe ':!grep -n -w "" *.[cChH] *.cc *.cpp *.hpp *.txt >'.&ef:cf:exe ":!rm ".&ef:let &ef=efsaveunlet efsave:cc " This runs ispell against the file map _S :w:!ispell % "map spell :w:!ispell % " Use this when you are doing mail - it turns on word wrap at 75 chars "map _W :set textwidth=75:set wrap:set wrapmargin=1 "use :set textwidth=80 to set the text width to 80 characters. With gggqG "you can reformat the whole text or you can visually select some code and "press gq to reformat it. " Insert the date and time right where the cursor is now "map _D mmi:r!date +"\%b \%d, \%Y \%H:\%M \%p":j`m:j " hmmm some system do not seem to respect the symbolic coding so... map _D O:r!date +"\%b \%d, \%Y \%H:\%M \%p" kdd " or ":map a=strftime("%c") " or " give current date "iab DATE =strftime("%a %b %d %T %Z %Y") " or "With these mappings in your .vimrc, typing CTRL-D twice while in Insert mode causes Vim to call its built-in strftime() function and insert the resulting date, while double-tapping CTRL-T likewise inserts the current time. " NOTE: INSERT MODE ! imap =strftime("%e %b %Y") imap =strftime("%l:%M %p") iabbrev PWD =getcwd() imap =string(eval(input("Calculate: "))) "Locate and return character "above" current cursor position... function! LookUpwards() "Locate current column and preceding line from which to copy... let column_num = virtcol('.') let target_pattern = '\%' . column_num . 'v.' let target_line_num = search(target_pattern . '*\S', 'bnW') "If target line found, return vertically copied character... if !target_line_num return "" else return matchstr(getline(target_line_num), target_pattern) endif endfunction "Reimplement CTRL-Y within insert mode... imap =LookUpwards() " Looks for the string Last update and changes the date-needs _date "map _L 1G/Last update:\s*/e+1C_date "map _L 1G/Last update\s*/eC_date " Turn off smartindent - usefull when pasting external lines "map _N :set nosmartindent "map _I :set smartindent " to turn smart indent back on set pastetoggle= " alternates set paste vs set nopaste " use shell with ctrl-z map :shell " convert text2html "map ,h :runtime! syntax/2html.vim " Because my pinky is slow to release the shift key - this only works sometimes " I have no idea why... "map :W :w " To cause a quick save invoke the next lines - no chance to stop it though "map :Q :q "map _Q :wq! " this writes regardless and quits "map :wq :wq "map :WQ :wq " map F8 to switch on and off syntax highlighting "map :call Swapcolor() " set up split windows "map :splitw "map :n " next window/session/file-must be rw files "map :rew " previous window/session/file-must be rw files "map _T :.,$ s/^*//:%s/^ *// " strips leading tabs out " from here to end of buffer "map _C :% s// /g " change all tabs to 2 spaces "cmap s/^ *// " get rid of begining white spaces " after you put in an address "map :1 " hit the top of the file "map :$ " Ahh - bottom of file " 980527 I often reformat a paragraph to fit some textwidth - " and I use the following mapping to adjust it to the " current position of the cursor: " map #tw :set textwidth==col(".") " Disable the suspend for ^Z. " I use Vim under "screen" where a suspend would lose the " connection to the " terminal - which is what I want to avoid. "=== autocmds =========== if has("autocmd") autocmd! autocmd BufNewFile *.sh 0r~/vimrc-files/vimrc-sh.tem autocmd BufWritePost *.sh !chmod +x % autocmd BufNewFile *.html 0r~/vimrc-files/vimrc-html.tem " or "au BufWinLeave *.html,*.pg mkview "au BufNewFile *.html 0r ~/.vim/template.html "au BufNewFile *.html set ts=2 "au BufWinEnter *.html,*.pg silent loadview "autocmd BufNewFile *.php 0r~/vimrc-files/vimrc-php.tem " automatically read tags file for C/C++ files " au BufNewFile,BufRead *.cc,*.c,*.h,*.cpp,*.hpp so tags.vim " make { automatically add a matching } for C/C++ files "au BufNewFile,BufRead *.cc,*.c,*.h,*.cpp,*.hpp inoremap { {}ki " make /* add a closing */ for C/C++ files "au BufNewFile,BufRead *.cc,*.c,*.h,*.cpp,*.hpp inoremap /* 0i/*0i*/k0i " we want C-style indenting only for C/C++ files "au BufNewFile,BufRead *.cc,*.c,*.h,*.cpp,*.hpp set cindent endif "======= play with colors ================= " t_Co = number of colors "if &t_Co > 1 "if has("syntax") set background=dark " syntax on " set hlsearch set nohlsearch hi! Comment term=NONE cterm=NONE hi! Comment ctermfg=cyan ctermbg=black guifg=blue guibg=black "endif if has("gui_running") set background=light "set background=dark syntax on set hlsearch " set nohlsearch endif if ( $TERM =="xterm" ) set background=light " you need the vim-full version for syntax on syntax on " set nohlsearch endif "if &diff " setup for diff mode "else " setup for non-diff mode "endif "========= set color scheme for gui ==================== " set up my cheesy color scheme (GUI only for now, I suppose " I should set up some colors for term at some point) "highlight Comment gui=italic guifg=gray60 "highlight Comment term=bold ctermfg=Cyan guifg=#80a0ff gui=bold highlight Comment term=bold ctermfg=Cyan guifg=#00a0f0 gui=bold highlight String guifg=DarkBlue highlight Statement gui=bold guifg=Blue highlight Conditional gui=bold guifg=Blue highlight Number guifg=DarkCyan highlight String guifg=DarkCyan highlight Operator gui=NONE guifg=Blue highlight Type gui=NONE guifg=Blue highlight Structure guifg=Blue highlight StorageClass guifg=Blue highlight Paren guifg=DarkGray highlight Function guifg=Blue highlight Constant guifg=Blue highlight Normal guibg=White guifg=Black highlight Tag guifg=#008888 gui=NONE highlight Folded term=bold ctermfg=Red ctermbg=black guifg=Red guibg=#DDDDDD highlight Visual guifg=Yellow guibg=Black " 001010 Do the Color Test! "map ,CT :sp $VIMRUNTIME/syntax/colortest.vim:so % "#################################################################