| Beginner's guide to Vi Improved (vim) | ||
|---|---|---|
| Prev | ||
Example A-1. My ~/.vimrc
" Global variable which holds the path to my customization files
" You'll need to edit this so it matches your system
let g:VIM_CUSTOM = "/home/jesse/.vim_custom/"
set visualbell
set background=dark
set tabstop=4
set showmatch
set showcmd
set autowrite
""""""""""""""""""""""""""""""""""""""""""""""""""
" Color scheme "
""""""""""""""""""""""""""""""""""""""""""""""""""
" Some custom color modifications. reference :help highlight and :help cterm
highlight ModeMsg cterm=bold ctermfg=2 ctermbg=black " set mode message ( --INSERT-- ) to green
highlight StatusLine ctermfg=7 ctermbg=9 " set the active statusline to black on white
highlight StatusLineNC ctermfg=8 ctermbg=9 " set inactive statusline to black on grey
syntax on
""""""""""""""""""""""""""""""""""""""""""""""""""
" Function Key maps "
""""""""""""""""""""""""""""""""""""""""""""""""""
" Re-source the default vimrc
map <F2> :execute Clean_up()<CR> :source $HOME/.vimrc<CR>
" C/C++ Programming
map <F3> :execute Clean_up()<CR> :execute Re_source("c-vimrc")<CR>
" shell programming
map <F4> :execute Clean_up()<CR> :execute Re_source("bash-vimrc")<CR>
" php programming
map <F5> :execute Clean_up()<CR> :execute Re_source("php-vimrc")<CR>
" sgml editing
map <F6> :execute Clean_up()<CR> :execute Re_source("sgml-vimrc")<CR>
" Once you invoke this you need to delete rows and type in the # you wish
" to process
map <F11> :execute Dump_extra_whitespace(rows)
" Reverse the background color
map <F12> :execute ReverseBackground()<CR>
""""""""""""""""""""""""""""""""""""""""""""""""""
" Custom functions "
""""""""""""""""""""""""""""""""""""""""""""""""""
" Re-source the rc files
:function! Re_source(file_name)
: let path_file_name = g:VIM_CUSTOM . a:file_name
: if filereadable(path_file_name)
: execute 'source ' . path_file_name
: echo path_file_name . " Loaded sucessfully"
: else
: echo path_file_name . " does NOT exist"
: return 0
: endif
:endfunction
" This function allows me to quickly remove extra tabs and whitespace
" from the beginning of lines. This seems to be a problem when I cut
" and paste or when people don't use resizeable tabs.
" TODO The only problem with this is after you execute it it jumps to the
" beginning of the file. I need to figure out how to fix that.
:function! Dump_extra_whitespace(rows)
: let com = ".,+" . a:rows . "s/^[ ]*//g"
: execute com
:endfunction
" This function was created by Dillon Jones (much better than my first attempt)
" it reverses the background color for switching between vim/gvim which have
" different defaults.
" TODO The only problem with this is after you execute it it jumps to the
" beginning of the file. I need to figure out how to fix that.
:function! ReverseBackground()
: let Mysyn=&syntax
: if &bg=="light"
: se bg=dark
: else
: se bg=light
: endif
: syn on
: exe "set syntax=" . Mysyn
": echo "now syntax is "&syntax
:endfunction
" Cleanup
:function! Clean_up()
:set visualbell&
:set background&
:set tabstop&
:set showmatch&
:set showcmd&
:set autowrite&
:endfunction |
Example A-2. My ~/.vim_custom/bash-vimrc
:set visualbell " Silence the bell, use a flash instead
:set formatoptions=tcqor " t=text, c=comments, q=format with gq command, o,r=autoinsert comment leader
:set shiftwidth=4 " set shiftwidth to 4 spaces
:set tabstop=4 " set tab to 4 spaces
:set showmatch " Show matching brackets/braces/parantheses.
:set background=dark " set background to dark
:set showcmd " Show (partial) command in status line.
:set autowrite " Automatically save before commands like :next and :make
:set textwidth=98 " My terminal is 98 characters wide
:set ai " set autoindent
:syntax on
"
" Shell Programming
:map <F2> :execute Clean_up()<CR> :source $HOME/.vimrc<CR>
:map <F3> gg:execute License_notice("bash_gpl_notice")<CR>dd18jO<ESC>
:map <F4> ofor var in $list<ESC>odo<ESC>odone<ESC>koecho $var<ESC>I
:map! <F4> for var in $list<ESC>odo<ESC>odone<ESC>koecho $var<ESC>I
:map <F5> ofor((i=0;; i++)); <ESC>odo<ESC>odone<ESC>2kI<ESC>f;a<SPACE>
:map! <F5> for((i=0;; i++)); <ESC>odo<ESC>odone<ESC>2kI<ESC>f;a<SPACE>
:map <F6> owhile ;<ESC>odo<ESC>odone<ESC>2kI<ESC>f;i
:map! <F6> while ;<ESC>odo<ESC>odone<ESC>2kI<ESC>f;i
:map <F7> oif [ ]; then<ESC>ofi<ESC>kf[a<SPACE>
:map! <F7> if [ ]; then<ESC>ofi<ESC>kf[a<SPACE>
:map <F8> ocase $list in<CR>);;<CR>);;<CR>*);;<CR>^Desac<ESC>3kf)i
:map! <F8> case $list in<CR>);;<CR>);;<CR>*);;<CR>^Desac<ESC>3kf)i
:map <F9> oPS3='Prompt: '<ESC>oselect choice in $list<ESC>odo<ESC>oecho "Here's your choice: $choice"<CR>break<CR>done<ESC>4kf'l
:map! <F9> PS3='Prompt: '<ESC>oselect choice in $list<ESC>odo<ESC>oecho "Here's your choice: $choice"<CR>break<CR>done<ESC>4kf'l
:function! License_notice(file_name)
: let path_file_name = g:VIM_CUSTOM . a:file_name
: execute 'r ' . path_file_name
:endfunction
" Cleanup
:function! Clean_up()
:set visualbell&
:set formatoptions&
:set shiftwidth&
:set tabstop&
:set showmatch&
:set background&
:set showcmd&
:set autowrite&
:set textwidth&
:set ai&
:unmap <F3>
:unmap! <F3>
:unmap <F4>
:unmap! <F4>
:unmap <F5>
:unmap! <F5>
:unmap <F6>
:unmap! <F6>
:unmap <F7>
:unmap! <F7>
:unmap <F8>
:unmap! <F8>
:unmap <F9>
:unmap! <F9>
:endfunction |
Example A-3. My ~/.vim_custom/php-vimrc
set visualbell " Silence the bell, use a flash instead
set cinoptions=:.5s,>1s,p0,t0,(0,g2 " :.5s = indent case statements 1/2 shiftwidth
" >1s = indent 1 shiftwidth
" p0 = indent function definitions 0 spaces
" t0 = indent function return type 0 spaces
" (0 = indent from unclosed parantheses
" g2 = indent C++ scope resolution 2 spaces
set cinwords=if,else,while,do,for,switch,case " Which keywords should indent
set formatoptions=tcqor " t=text, c=comments, q=format with "gq", o,r=autoinsert comment leader
set cindent " indent on cinwords
set shiftwidth=4 " set shiftwidth to 4 spaces
set tabstop=4 " set tab to 4 spaces
set showmatch " Show matching brackets/braces/parantheses.
set background=dark " set background to dark
set showcmd " Show (partial) command in status line.
set autowrite " Automatically save before commands like :next and :make
set textwidth=98 " My terminal is 98 characters wide
syntax on
" PHP Programming
map <F2> :execute Clean_up()<CR> :source $HOME/.vimrc<CR>
map <F3> gg:execute License_notice("c_gpl_notice")<CR>dd18jO<ESC>
map <F4> ofor($i;; $i++) {<ESC>o}<ESC>kI<ESC>f;a<SPACE>
map! <F4> for($i;; $i++) {<ESC>o}<ESC>kI<ESC>f;a<SPACE>
map <F5> oforeach($array as $index=>$value) {<ESC>o}<ESC>kI<ESC>f$l
map! <F5> foreach($array as $index=>$value) {<ESC>o}<ESC>kI<ESC>f$l
map <F6> owhile() {<ESC>o}<ESC>kI<ESC>f(a
map! <F6> while() {<ESC>o}<ESC>kI<ESC>f(a
map <F7> oif() {<ESC>o}<ESC>kf(a
map! <F7> if() {<ESC>o}<ESC>kf(a
map <F8> oswitch() {<ESC>ocase:<CR>break;<CR>case:<CR>break;<CR>default:<CR>}<ESC>6kf(a
map! <F8> switch() {<ESC>ocase:<CR>break;<CR>case:<CR>break;<CR>default:<CR>}<ESC>6kf(a
:function! License_notice(file_name)
: let path_file_name = g:VIM_CUSTOM . a:file_name
: :execute 'r ' . path_file_name
:endfunction
" Cleanup
:function! Clean_up()
: set visualbell&
: set cinoptions&
: set cinwords&
: set formatoptions&
: set cindent&
: set shiftwidth&
: set tabstop&
: set showmatch&
: set background&
: set showcmd&
: set autowrite&
: set textwidth&
: unmap <F3>
: unmap! <F3>
: unmap <F4>
: unmap! <F4>
: unmap <F5>
: unmap! <F5>
: unmap <F6>
: unmap! <F6>
: unmap <F7>
: unmap! <F7>
: unmap <F8>
: unmap! <F8>
:endfunction |
Example A-4. My ~/.vim_custom/c-vimrc
set visualbell " Silence the bell, use a flash instead
set cinoptions=:.5s,>1s,p0,t0,(0,g2 " :.5s = indent case statements 1/2 shiftwidth
" >1s = indent 1 shiftwidth
" p0 = indent function definitions 0 spaces
" t0 = indent function return type 0 spaces
" (0 = indent from unclosed parantheses
" g2 = indent C++ scope resolution 2 spaces
set cinwords=if,else,while,do,for,switch,case " Which keywords should indent
set formatoptions=tcqor " t=text, c=comments, q=format with "gq", o,r=autoinsert comment leader
set cindent " indent on cinwords
set shiftwidth=4 " set shiftwidth to 4 spaces
set tabstop=4 " set tab to 4 spaces
set showmatch " Show matching brackets/braces/parantheses.
set background=dark " set background to dark
set showcmd " Show (partial) command in status line.
set autowrite " Automatically save before commands like :next and :make
set textwidth=98 " My terminal is 98 characters wide
syntax on
" C Programming
map <F2> :execute Clean_up()<CR> :source $HOME/.vimrc<CR>
map <F3> gg:execute License_notice("c_gpl_notice")<CR>dd18jO<ESC>
map <F4> ofor(i=0;; i++) {<ESC>o}<ESC>kI<ESC>f;a<SPACE>
map! <F4> for(i;; i++) {<ESC>o}<ESC>kI<ESC>f;a<SPACE>
map <F5> owhile() {<ESC>o}<ESC>kI<ESC>f(a
map! <F5> while() {<ESC>o}<ESC>kI<ESC>f(a
map <F6> oif() {<ESC>o}<ESC>kf(a
map! <F6> if() {<ESC>o}<ESC>kf(a
map <F7> oswitch() {<ESC>ocase:<CR>break;<CR>case:<CR>break;<CR>default:<CR>}<ESC>6kf(a
map! <F7> switch() {<ESC>ocase:<CR>break;<CR>case:<CR>break;<CR>default:<CR>}<ESC>6kf(a
:function! License_notice(file_name)
: let path_file_name = g:VIM_CUSTOM . a:file_name
: :execute 'r ' . path_file_name
:endfunction
" Cleanup
function! Clean_up()
set visualbell&
set cinoptions&
set cinwords&
set formatoptions&
set cindent&
set shiftwidth&
set tabstop&
set showmatch&
set background&
set showcmd&
set autowrite&
set textwidth&
unmap <F3>
unmap <F4>
unmap <F5>
unmap <F6>
unmap <F7>
endfunction |
Example A-5. My ~/.vim_custom/sgml-vimrc
" These mappings are primarily based on Docbook 3.1 which is probably pretty old
" now. If you upgrade this to a newer version please pass me what you have and
" what version you based it on. Maybe we can set up version specific maps.
set ai " set autoindent
set visualbell
set shiftwidth=4 " set shiftwidth to 4 spaces
set tabstop=4 " set tab to 4 spaces
set showmatch " Show matching brackets/braces/parantheses.
set background=dark " set background to dark
set showcmd " Show (partial) command in status line.
set autowrite " Automatically save before commands like :next and :make
set showmatch " Show matching brackets
"syntax keyword sgmlTODO contained TODO FIXME NEED
"highlight TODO ctermfg=0 ctermbg=3
syntax on
" Re-source the default vimrc
map <F2> :execute Clean_up()<CR> :source $HOME/.vimrc<CR>
map <F3> gg:execute License_notice("sgml_gfdl_comment_notice")<CR>dd13jO<ESC>
" TODO This map works but after it reads it in it takes you to the beginning of the
" file. Need to fix this.
map <F4> :execute License_notice("sgml_gfdl_notice")<CR>
" Insert a new section header
map! ,h1 <sect1 id="" xreflabel=""><CR><title></title><CR><CR></sect1><ESC>3kf"a
map! ,h2 <sect2 id="" xreflabel=""><CR><title></title><CR><CR></sect2><ESC>3kf"a
map! ,h3 <sect3 id="" xreflabel=""><CR><title></title><CR><CR></sect3><ESC>3kf"a
map! ,h4 <sect4 id="" xreflabel=""><CR><title></title><CR><CR></sect4><ESC>3kf"a
map! ,h5 <sect5 id="" xreflabel=""><CR><title></title><CR><CR></sect5><ESC>3kf"a
" paragraphs
map! ,p <para><CR></para><ESC>O
map! ,P <formalpara><title></title><CR><TAB><para><CR></para><CR>^D</formalpara><ESC>3k$2ba
" Moves to end of next tag (this keeps you in insert mode and typing!)
" THIS IS BY FAR the most useful map, Thanks to the authors of "Learning Vi"
" from O'Reilly!
map! ,e <ESC>f>a
map ,e f>
" markup affecting words
map! ,b <emphasis></emphasis><ESC>2ba
map! ,B <emphasis role="bold"></emphasis><ESC>2bla
map! ,f <filename></filename><ESC>2ba
map! ,u <ulink url=""></ulink><ESC>2bla
map! ,le <link linkend=""></link><ESC>2bla
map! ,x <xref linkend="<ESC>a
map! ,gt <glossterm></glossterm><ESC>2ba
map! ,Gt <glossterm baseform=""></glossterm><ESC>2bla
map! ,r <citation></citation><ESC>2ba
map! ,k <keycombo><keycap></keycap><keycap></keycap></keycombo><ESC>8ba
" regular list and then numbered list (arabic)
map! ,li <itemizedlist><CR><TAB><listitem><CR><TAB><para><CR></para><CR>^D</listitem><CR><CR><listitem><CR><TAB><para><CR></para><CR>^D</listitem><CR>^D</itemizedlist><ESC>7kO
map! ,ln <orderedlist numeration="Arabic"><CR><TAB><listitem><CR><TAB><para><CR></para><CR>^D</listitem><CR><CR><listitem><CR><TAB><para><CR></para><CR>^D</listitem><CR>^D</orderedlist><ESC>7kO
map! ,lt <listitem><CR><TAB><para><CR></para><CR>^D</listitem><ESC>kO
" Admonitions
map! ,n <note><CR><TAB><para><CR></para><CR>^D</note><ESC>kO
map! ,N <note><title></title><CR><TAB><para><CR></para><CR>^D</note><ESC>3k$2ba
map! ,t <tip><CR><TAB><para><CR></para><CR>^D</tip><ESC>kO
map! ,T <tip><title></title><CR><TAB><para><CR></para><CR>^D</tip><ESC>3k$2ba
map! ,i <important><CR><TAB><para><CR></para><CR>^D</important><ESC>kO
map! ,I <important><title></title><CR><TAB><para><CR></para><CR>^D</important><ESC>3k$2ba
map! ,c <caution><CR><TAB><para><CR></para><CR>^D</caution><ESC>kO
map! ,C <caution><title></title><CR><TAB><para><CR></para><CR>^D</caution><ESC>3k$2ba
map! ,w <warning><CR><TAB><para><CR></para><CR>^D</warning><ESC>kO
map! ,W <warning><title></title><CR><TAB><para><CR></para><CR>^D</warning><ESC>3k$2ba
" For quick console command examples
map! ,s <ESC>I<screen><CR><prompt>bash$</prompt> <command></command><CR></screen><ESC>k$2ba
" This one is for when commands contain characters that the
" parser wants to parse.
map! ,S <ESC>I<screen><![ CDATA [ <CR>]]<CR></screen><ESC>kO
" TODO test the examp to make sure it works correctly
map! ,E <ESC>o<example id=""><title></title><CR></example><ESC>k^f"a
map! ,q <ESC>I<programlisting><![ CDATA [ <CR>]]><CR></programlisting><ESC>kO
map! ,Q <ESC>I<programlisting><![ CDATA [ <CR>]]><CR></programlisting><ESC>kO
" appendix
map! ,a <appendix xreflabel=""><title></title><CR></appendix><ESC>kf"a
" For inserting graphics
map! ,gr <mediaobject><CR><TAB><imageobject><CR><TAB><imagedata fileref="" format="gif"><CR>^D</imageobject><CR><CR><textobject><CR><TAB><phrase></phrase><CR>^D</textobject><CR>^D</mediaobject><ESC>6kf"a
:function! License_notice(file_name)
: let path_file_name = g:VIM_CUSTOM . a:file_name
: execute 'r ' . path_file_name
:endfunction
" Cleanup
:function! Clean_up()
:set visualbell&
:set background&
:set tabstop&
:set showmatch&
:set autowrite&
:unmap <F3>
:unmap <F4>
:unmap! ,h1
:unmap! ,h2
:unmap! ,h3
:unmap! ,h4
:unmap! ,h5
:unmap! ,p
:unmap! ,P
:unmap! ,e
:unmap ,e
:unmap! ,b
:unmap! ,B
:unmap! ,k
:unmap! ,f
:unmap! ,u
:unmap! ,le
:unmap! ,x
:unmap! ,gt
:unmap! ,Gt
:unmap! ,r
:unmap! ,li
:unmap! ,ln
:unmap! ,lt
:unmap! ,n
:unmap! ,N
:unmap! ,t
:unmap! ,T
:unmap! ,i
:unmap! ,I
:unmap! ,c
:unmap! ,C
:unmap! ,w
:unmap! ,W
:unmap! ,s
:unmap! ,S
:unmap! ,E
:unmap! ,q
:unmap! ,Q
:unmap! ,gr
:endfunction |