Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Wednesday, August 4, 2010

advanced regular expression 高级正则表达式

断言
零宽度正预测先行断言 and 零宽度正回顾后发断言

(1)零宽度: 这表示匹配是一个位置(Loaction)而不是子表达式。
(2)预测先行 vs 回顾后发
预测先行:仅当子表达式在此位置的右侧匹配时才继续匹配。返回与子表达式匹配的前面位置,从做左到右匹配。
回顾后发:仅当子表达式在此位置的左侧匹配时才继续匹配。返回与子表达式匹配的后边位置,从右到左匹配。
(3)正向 vs 负向:
正向:当子表达式满足时才匹配。
负向:当子表达式不满足时才匹配。


总共四种断言:
零宽度正预测先行断言
vim: \@=
vim例:\w\+\(ing\)\@=     匹配以ing结尾的单词的前面部分(除了ing以外的部分)
   如果用magic, 则为 \v\w+(ing)@\=
零宽度正回顾后发断言
vim: \@<=
vim例:\(re\)\@<=\w+     匹配以re开头的单词的后半部分(除了re以外的部分)
   如果用magic, 则为 \v(re)@\<\=\w+
零宽度负预测先行断言
vim: \@!
vim例:foo\(bar\)\@!     匹配后面不带 "bar" 的 "foo"
零宽度负回顾后发断言
vim: \@<!
vim例:\(foo\)\@<!bar  匹配前面不带 "foo" 的 "bar"

PCRE 对应的格式为:
(?= 子表达式), (?<= 子表达式)
(?! 子表达式), (?<! 子表达式)

for perl help:
# perldoc perlre

.NET Framework中支持有名RE, 并支持注释:
(?<myname>expression) 如果遇到匹配,则把匹配的内容命名为myname,并压入堆栈
(?<-myname>expression') 如果遇到匹配,则从堆栈中弹出 名为myname的匹配内容。
(?(myname)yes|no) 如果堆栈上存在命名为myname的匹配的内容,则去继续匹配yes部分的表达式,否去继续匹配no部分。


 注释
 注释一般通过表达式(?#注释)实现。
以下正则表达式在第一个分组中中添加了注释“不能以数字开头”。
(?<!\d+(?#不能以数字开头))[a-z_A-Z]+
如果要在正则表达式中包含注释,最好打开IgnorePatternWhitespace选项,即忽略模式里的空白字符。

Wednesday, September 24, 2008

my vimrc

""script by Lifeng Chen.
set langmenu=en_US
let $LANG = 'en_US'
syntax on
set nocompatible
set ai
set textwidth=0 ""disable line auto broken
set hlsearch
set incsearch
set magic
"set scrolloff=3 ""to see more context above and below the cursor
set lz " do not redraw while running macros (much faster) (LazyRedraw)
"set shell=csh\ -f
if has("win32")
set backupdir=c:/tmp/vim
set guifont=Lucida_Console:h11:b:cANSI
let myfile = $VIMRUNTIME."/colors/my_murphy.vim"
else
set backupdir=~/tmp/vim
set guifont=MiscFixed\ 12
"set guifont=MiscFixed\ Bold\ 11
let myfile = $HOME."/.vim/colors/my_murphy.vim"
endif
if filereadable(myfile)
colorscheme my_murphy
else
colorscheme murphy
endif
so $VIMRUNTIME/mswin.vim
"set nu ""disable line_number for TERM as it is not intended when copying
set vb t_vb= ""disable any boring beep or flash. Need to set in .gvimrc to make it really work for GUI.
"set autochdir ""don't change working dir automatically for TERM
set nowrap
set backup ""I also need non-default backupdir
set viminfo='10,:20,%,n~/.viminfo ""same viminfo: 'files w marks, :cmd his num,
"set mouse=a
"set mouse=nvc
"set mousemodel=extend
"set nowrapscan
set more
set ve=block
set vb t_vb= " no beep or flash
set fillchars=stl:^,stlnc:-,vert:\|,fold:-,diff:- " better spacing btwn windows

set showmatch "" When a bracket is inserted, briefly jump to the matching one.
set matchtime=5
set wildignore=.svn,CVS,.git,*.o,*.a,*.class,*.mo,*.la,*.so,*.obj,*.swp,*.jpg,*.png,*.xpm,*.gif
set wildmode=list:full "" Completion mode: When more than one match, list all matches and complete first match.
set wildmenu "" command-line completion operates in an enhanced mode: to invoke the completion
" syn keyword myTodo todo contained
" highlight def link myTodo Todo
"" highlight my favorites
match Todo /\c\\|\\|\/

set ru "" Show the line and column number of the cursor position
if version >= 700
set cul "" Highlight the screen line of the cursor with CursorLine
endif
"set cuc "" Highlight the screen column of the cursor with CursorColumn
"" a: Autoselect, Visually highlighted text is available for pasting into other applications as well as into Vim itself
"" b: Bottom (horizontal) scrollbar is present
set guioptions+=abrh
set guioptions-=L "" L: Left-hand scrollbar is present when there is a vertically
set foldmethod=indent
set foldlevelstart=1
"let b:did_ftplugin = 1 ""This disables loading of default plugin completely
autocmd FileType python source ~/.vim/ftplugin/python.vim
"" {{{balloon control
if version >= 700
set ballooneval "" ballon control
fun! FoldSpellBalloon()
let foldStart = foldclosed(v:beval_lnum )
let foldEnd = foldclosedend(v:beval_lnum)
let lines = []
let maxlinenum=60
" Detect if we are in a fold
if foldStart < 0
" Detect if we are on a misspelled word
let lines = spellsuggest( spellbadword(v:beval_text)[ 0 ], 5, 0 )
else
" we are in a fold
let numLines = foldEnd - foldStart + 1
" if we have too many lines in fold, show only the first half_of_max
" and the last half_of_max lines
if ( numLines >= maxlinenum )
let lines = getline( foldStart, foldStart + maxlinenum/2 - 1 )
let lines += [ '..............' ]
let lines += [ '[-- Snipped ' . ( numLines - maxlinenum ) . ' lines --]' ]
let lines += [ '..............' ]
let lines += getline( foldEnd - maxlinenum/2 + 1, foldEnd )
else
"less than maxlinenum lines, lets show all of them
let lines = getline( foldStart, foldEnd )
endif
endif
" return result
return join( lines, has( "balloon_multiline" ) ? "\n" : " " )
endfun
set balloonexpr=FoldSpellBalloon()
set balloondelay=400
"" settings invalid for the KDE, GTK+ or Win32
"set tooltipForeground=
"set tooltipBackground=
"set tooltipFont=
"set balloonevalcode 59
endif
""}}}balloon control

function! ShortStr(str,max_len)
let max_len_lead = a:max_len/2
let total_len=strlen(a:str)
let trim_str = a:str
if total_len > a:max_len
let trim_str = strpart(a:str, 0, max_len_lead) . ".." . strpart(a:str, total_len - a:max_len + max_len_lead + 2)
endif
return trim_str
endfunction
function! CurDir()
let homedir = '/home/'.$USER
let curdir = substitute(getcwd(), homedir, "~", "g")
let curdir=ShortStr(curdir, 18)
return curdir . "/"
endfunction
"set ruler "" overridden by statusline ""Show the line and column number of the cursor position
"set rulerformat=%20(%2*%<%f%=" %m%r" %3l" %c" %p%%%)
"" {{{statusline control
if filereadable(myfile)
"set statusline=%F%m%r%h%w" [FORMAT=%{&ff}]" [TYPE=%Y]" [POS=%l,%v][%p%%]" %{strftime(""%d/%m/%y" -" %H:%M"")}
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]
"set statusline=%F%m%r%h%w\ -\ ascii=x\%02.2B,\ pos=%05l,%3v\ (%p%%\ of\ %L\ lines)
set statusline= ""overwrite any existent statusline
set statusline+=%#StatusLineB1#%f%m%r%h%w%0* ""short name
set statusline+=%#StatusLineB2#[BUF:%-1.2n]%0* ""buffer number
set statusline+=%#StatusLineB3#[%{&fileformat}]%0* ""fileformat
set statusline+=%= ""spacer
"set statusline+=%#StatusLineB3#[%{CurDir()}]%0* ""fileformat
set statusline+=%#StatusLineB4#[%p%%]%0* ""position percentage
set statusline+=%#StatusLineB5#[pos=%05l,%3v]%0* ""horizontal/vertical position
set statusline+=%#StatusLineB6#[LEN=%L]%0* ""total line number
set statusline+=%< ""Left aligned to see file name all the time
set laststatus=2 "" 2: the last window will always have a status line
else
set statusline= ""overwrite any existent statusline
set statusline+=%f%m%r%h%w%0* ""short name
set statusline+=[BUF:%-1.2n]%0* ""buffer number
set statusline+=[%{&fileformat}]%0* ""fileformat
set statusline+=%= ""spacer
set statusline+=[%p%%]%0* ""position percentage
set statusline+=[pos=%05l,%3v]%0* ""horizontal/vertical position
set statusline+=[LEN=%L]%0* ""total line number
set statusline+=%< ""Left aligned to see file name all the time
set laststatus=2 "" 2: the last window will always have a status line
endif
""}}}statusline control

"--------------------------------------for mapping-------------------------------
let mapleader = ","
"{{{
"" fast switch btwn buffers
nnoremap gt
nnoremap gT
nnoremap w
nnoremap h
nnoremap l
nnoremap j
nnoremap k
"" za: open or close a fold
"" zA: open or close a fold recursively
nnoremap za
nnoremap zA

if version >= 700
"open file in new tabpage
vnoremap gf y:sp =escape(@", '\\/.*$^~[]')
nnoremap gf
endif

"" fast search: don't use as I want them echoed
nnoremap / yiw/=escape(@", '\\/.*$^~[]')
nnoremap /b /\<\>
nnoremap /c /\c
nnoremap /e //e
nnoremap /
nnoremap ?
vnoremap y/=escape(@", '\\/.*$^~[]')q
vnoremap y?=escape(@", '\\/.*$^~[]')q
vmap /
nnoremap . .n

nnoremap :help =expand("")
vnoremap y:help "
nnoremap :e#
nnoremap :q
nnoremap :e!
""{{{single-line comment behavior
"nnoremap I#j
"nnoremap ^:,s/\v^(\s*)#\s*/\1/en
autocmd BufWinEnter,BufNewFile * nnoremap I#j
autocmd BufWinEnter,BufNewFile *.c nnoremap I//j
autocmd BufWinEnter,BufNewFile *.cpp nnoremap I//j
autocmd BufWinEnter,BufNewFile *.v nnoremap I//j
autocmd BufWinEnter,BufNewFile *.sv* nnoremap I//j
autocmd BufWinEnter,BufNewFile *.*vim nnoremap I"j
autocmd BufWinEnter,BufNewFile .*vim* nnoremap I"j
autocmd BufWinEnter,BufNewFile * nnoremap ^:,s/\v(^\s*)@\<=#\s*//en
autocmd BufWinEnter,BufNewFile *.c nnoremap ^:,s/\v(^\s*)@\<=(\/\/)\s*//en
autocmd BufWinEnter,BufNewFile *.cpp nnoremap ^:,s/\v(^\s*)@\<=(\/\/)\s*//en
autocmd BufWinEnter,BufNewFile *.v nnoremap ^:,s/\v(^\s*)@\<=(\/\/)\s*//en
autocmd BufWinEnter,BufNewFile *.sv* nnoremap ^:,s/\v(^\s*)@\<=(\/\/)\s*//en
autocmd BufWinEnter,BufNewFile *.*vim nnoremap ^:,s/\v(^\s*)@\<="\s*//en
autocmd BufWinEnter,BufNewFile .*vim* nnoremap ^:,s/\v(^\s*)@\<="\s*//en
"nnoremap I//j
"nnoremap ^:,s/\v^(\s*)\/\//\1/n
"nnoremap I#j
"nnoremap ^:,s/\v^(\s*)#/\1/n
"}}}single-line comment behavior
"" avoid annoying K by mistake
nnoremap K :K
nnoremap @a
nnoremap :lt ""quick return to previous tag stack
vnoremap [ xi[]"
vnoremap { xi{}"
vnoremap ( xi()"
vnoremap " xi"""
vnoremap : :
vnoremap / y/=escape(@", '\\/.*$^~[]')
vnoremap y/=escape(@", '\\/.*$^~[]')
vnoremap y:s/=escape(@", '\\/.*$^~[]')//
""vnoremap y:promptfind
vnoremap >gv
vnoremap inoremap ( ()
inoremap { {}
inoremap [ []
autocmd BufEnter * :if &ft != 'vim' | inoremap " ""| endif
nnoremap sv :set syntax=verilog_systemverilog
if has("win32")
nnoremap ss :source $VIMRUNTIME\..\_vimrc
else
nnoremap ss :source ~/.vimrc
endif
nnoremap v :set pastemui+mv'uV'v=:set nopaste
vnoremap tn y:tabnew =escape(@", '\\/.*$^~[]')
vnoremap te y:tabedit =escape(@", '\\/.*$^~[]')
"noremap
"" allow me to use double to give up
noremap

" Fast diff
" DON'T remove the tailing space, as it is used as deliminater
nnoremap :vertical diffsplit
nnoremap :diffupdate
if version >= 700
set diffopt+=vertical
endif
nnoremap :diffget]c
nnoremap :diffput]c
" Fast grep
nnoremap g :lv /=expand("")/ %:lw
vnoremap g :lv /=GetVisualSelection()/ %:lw
""}}} map

"--------------------------------------for plugins-------------------------------
"-------------------------------------- for markbrowser
"markbrowser setting
nnoremap m :MarksBrowser
"-------------------------------------- lookupfile setting
"{{{lookupfile
let g:LookupFile_MinPatLength = 2
let g:LookupFile_PreserveLastPattern = 0
let g:LookupFile_PreservePatternHistory = 0
let g:LookupFile_AlwaysAcceptFirst = 1
let g:LookupFile_AllowNewFiles = 0
if filereadable("./filenametags")
let g:LookupFile_TagExpr = '"./filenametags"'
endif
nnoremap lk LookupFile
nnoremap ll :LUBufs
nnoremap lw :LUWalk
" lookup file with ignore case
fun! LookupFile_IgnoreCaseFunc(pattern)
let _tags = &tags
try
let &tags = eval(g:LookupFile_TagExpr)
let newpattern = '\c' . a:pattern
let tags = taglist(newpattern)
catch
echohl ErrorMsg | echo "Exception: " . v:exception | echohl NONE
return ""
finally
let &tags = _tags
endtry
" Show the matches for what is typed so far.
let files = map(tags, 'v:val["filename"]')
return files
endfun
let g:LookupFile_LookupFunc = 'LookupFile_IgnoreCaseFunc'
"}}}lookupfile
"----------------------------------------------- for minibufexpl.vim
"" {{{minibuffer
""let loaded_minibufexplorer = 1 " *** Disable minibuffer plugin
""let g:miniBufExplForceSyntaxEnable = 1 " force syntax on
"let g:miniBufExplVSplit = 25
"let winManagerWindowLayout = 'FileExplorer|TagList' "set 'TagList' in the 'winManagerWindowLayout'
let g:miniBufExplMapWindowNavArrow = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplorerMoreThanOne = 2 " Display when more than 2 buffers
let g:miniBufExplSplitToEdge = 1 " Always at top
let g:miniBufExplMaxSize = 3 " The max height is 3 lines
let g:miniBufExplMapWindowNavVim = 1 " map CTRL-[hjkl]
let g:miniBufExplUseSingleClick = 1 " select by single click
let g:miniBufExplModSelTarget = 1 " Dont change to unmodified buffer
let g:miniBufExplSplitBelow = 0
autocmd BufRead,BufNew :call UMiniBufExplorer
""}}}minibuffer

"----------------------------------------------- for bufexplorer.vim
" Let to show buf explorer(bufexplorer plugin)
nmap ,be
"" is reserved for Visual Mark (same as UltraEdit)
"----------------------------------------------- for mru.vim
let MRU_Max_Entries = 100
nnoremap <> :MRU
"----------------------------------------------- for showmarks.vim ""disabled
""let g:showmarks_include="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"


"""svtags
if $USER == "lfchen"
set tags=/proj/lynley/ws/lfchen/mgnmFlow/interconnect/tags,/proj/lynley/ws/lfchen/mgnmFlow/bio/tags
else
set tags=/proj/lynley/ws/lfchen/mgnmFlow/interconnect/tags,/Please/Specify/Your/Own/tags
endif
"""matchit
au BufReadPost * let b:match_ignorecase=0
au BufReadPost * let b:match_words=
\ '^\s*\:^\s*\,' .
\ '^\s*\:^\s*\,' .
\ '^\s*\:^\s*\,' .
\ '\:^\s*\,' .
\ '^\s*\\|\\|\:^\s*\,' .
\ '^\s*\:^\s*\,' .
\ '^\s*\:\\|\\|\:\,' .
\ '^\s*\:\,' .
\ '^\s*\:^\s*\,' .
\ '^\s*`ifdef\>\|^\s*`ifndef\>:^\s*`else\>:^\s*`endif\>,' .
\ '^\s*\:^\s*\,' .
\ '^\s*\:\,' .
\ '^\s*\:\,' .
\ '^\s*\:\,' .
\ '^\s*\:\,' .
\ '^\s*\:^\s*\,' .
\ '^\s*\:^\s*\\|^\s*\|^\s*\,' .
\ '^\s*\:^\s*\,'.
\ '^\s*\:^\s*\'
au BufReadPost *.xml let b:match_words=
\ '^\s*\:^\s*\,'.
\ '<\(\w\+\):,'.
\ '<:>'
"" remember where I was when reopening a file
au BufReadPost * if line("'\"") > 0
au BufReadPost * if line("'\"") <= line("$")
au BufReadPost * exe("norm '\"")
au BufReadPost * else
au BufReadPost * exe "norm $"
au BufReadPost * endif
au BufReadPost * endif

"{{{--------------------------------------for autocmd and filetype------------------
"filetype plugin on
filetype indent on
"" enable file type detection
filetype on
"" backup last search pattern for each window
"au WinEnter * :if exists("w:blsp") && w:blsp != '' | let @/=w:blsp | endif
"au WinLeave * :let w:blsp=@/

fun! StatusLineGetPath() "{{{
let p = expand('%:.:h') "relative to current path, and head path only
let p = substitute(p,'\','/','g')
let p = substitute(p, '^\V' . $HOME, '~', '')
if len(p) > g:statusline_max_path
let p = simplify(p)
let p = pathshorten(p)
endif
return p
endfun ""}}}StatusLineGetPath

fun! StatusLineRealSyn() "{{{
let synId = synID(line('.'),col('.'),1)
let realSynId = synIDtrans(synId)
if synId == realSynId
return 'Normal'
else
return synIDattr( realSynId, 'name' )
endif
endfun ""}}}StatusLineRealSyn

fun! LastModified() "{{{
if &modified
let save_cursor = getpos(".")
let n = min([20, line("$")])
exe '1,' . n . 's#^\(.\{,10\}Last modified\s*:\).*#\1' . strftime('%a %b %d, %Y %I:%M%p') . '#e'
call setpos('.', save_cursor)
endif
endfun ""}}}LastModified
autocmd BufWritePre *.sv* call LastModified()
iab A_date =strftime("%c")

fun! SetHead_scr() "{{{
call setline(1,"#!/usr/bin/env ".&filetype)
call append(line("."), "\#-------------------------------------------------------------------------")
call append(line(".")+1, "\#File name : ".expand("%"))
call append(line(".")+2, "\#Author : ".$USER ." (Full Name please)")
call append(line(".")+3, "\#Created : ".strftime("%c"))
call append(line(".")+4, "\#Description : ")
call append(line(".")+5, "\# : ")
call append(line(".")+6, "\#Notes : ")
call append(line(".")+7, "\#-------------------------------------------------------------------------")
call append(line(".")+8, "\#Copyright 2010 (c)")
call append(line(".")+9, "\#-------------------------------------------------------------------------")
call append(line(".")+10,"")
endfun ""}}}SetHead_scr
fun! SetHead_sv() "{{{
call setline(1,"\//-------------------------------------------------------------------------")
call append(line("."), "\//File name : ".expand("%"))
call append(line(".")+1, "\//Author : ".$USER ." (Full Name please)")
call append(line(".")+2, "\//Created : ".strftime("%c"))
call append(line(".")+3, "\//Description : ")
call append(line(".")+4, "\// : ")
call append(line(".")+5, "\//Notes : ")
call append(line(".")+6, "\//-------------------------------------------------------------------------")
call append(line(".")+7, "\//Copyright 2010 (c)")
call append(line(".")+8, "\//-------------------------------------------------------------------------")
call append(line(".")+9, "")
endfun ""}}}SetHead_sv
fun! s:GetVisualSelection()
let save_a = @a
silent normal! gv"ay
let v = @a
let @a = save_a
let var = escape(v, '\\/.$*')
return var
endfun
func! DeleteTrailingWS()
exe "normal mz"
%s/\s\+$//ge
exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()
autocmd BufWrite *.pl :call DeleteTrailingWS()
autocmd BufWrite *.sv* :call DeleteTrailingWS()
autocmd BufNewFile *.[c|sv]* exec ":call SetHead_sv()"
"autocmd BufNewFile *.sv 0r $mrepo/skeleton.sv
autocmd BufNewFile *.pl exec ":call SetHead_scr()"
autocmd BufNewFile *.py exec ":call SetHead_scr()"
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType make setlocal tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab
autocmd FileType python setlocal tabstop=4 softtabstop=4 shiftwidth=4 expandtab
autocmd FileType * :if &ft != 'python' && &ft != 'make' | set tabstop=2 softtabstop=2 shiftwidth=2 expandtab | endif
"autocmd FileType python set foldmethod=syntax ""not working
autocmd FileType text set spell spelllang=en_us
autocmd BufRead,BufNewFile *.txt :if &ft != 'help' | setfiletype text | endif
" make tab characters and trailing whitespace obvious
autocmd FileType * :if &ft != 'help' | 2match Error /\t\+ \+\| \+\t\+\|\s\+$/ | endif

""}}} autocmd

vimrc template

it's from http://www.stripey.com/vim/vimrc.html. anyway it's too complex


" .vimrc"
" Smylers's .vimrc
" http://www.stripey.com/vim/

" 2000 Jun 1: for `Vim' 5.6

" This vimrc is divided into these sections:

" * Terminal Settings
" * User Interface
" * Text Formatting -- General
" * Text Formatting -- Specific File Formats
" * Search & Replace
" * Spelling
" * Keystrokes -- Moving Around
" * Keystrokes -- Formatting
" * Keystrokes -- Toggles
" * Keystrokes -- Insert Mode
" * Keystrokes -- For HTML Files
" * `SLRN' Behaviour
" * Functions Referred to Above

" This file contains no control codes and no `top bit set' characters above the
" normal Ascii range, and all lines contain a maximum of 79 characters. With a
" bit of luck, this should make it resilient to being uploaded, downloaded,
" e-mailed, posted, encoded, decoded, transmitted by morse code, or whatever.


" first clear any existing autocommands:
autocmd!


" * Terminal Settings

" `XTerm', `RXVT', `Gnome Terminal', and `Konsole' all claim to be "xterm";
" `KVT' claims to be "xterm-color":
if &term =~ 'xterm'

  " `Gnome Terminal' fortunately sets $COLORTERM; it needs <BkSpc> and <Del>
  " fixing, and it has a bug which causes spurious "c"s to appear, which can be
  " fixed by unsetting t_RV:
  if $COLORTERM == 'gnome-terminal'
  execute 'set t_kb=' . nr2char(8)
  " [Char 8 is <Ctrl>+H.]
  fixdel
  set t_RV=

  " `XTerm', `Konsole', and `KVT' all also need <BkSpc> and <Del> fixing;
  " there's no easy way of distinguishing these terminals from other things
  " that claim to be "xterm", but `RXVT' sets $COLORTERM to "rxvt" and these
  " don't:
  elseif $COLORTERM == ''
  execute 'set t_kb=' . nr2char(8)
  fixdel

  " The above won't work if an `XTerm' or `KVT' is started from within a `Gnome
  " Terminal' or an `RXVT': the $COLORTERM setting will propagate; it's always
  " OK with `Konsole' which explicitly sets $COLORTERM to "".

  endif
endif


" * User Interface

" have syntax highlighting in terminals which can display colours:
if has('syntax') && (&t_Co > 2)
  syntax on
endif

" have fifty lines of command-line (etc) history:
set history=50
" remember all of these between sessions, but only 10 search terms; also
" remember info for 10 files, but never any on removable disks, don't remember
" marks in files, don't rehighlight old search patterns, and only save up to
" 100 lines of registers; including @10 in there should restrict input buffer
" but it causes an error for me:
set viminfo=/10,'10,r/mnt/zip,r/mnt/floppy,f0,h,\"100

" have command-line completion <Tab> (for filenames, help topics, option names)
" first list the available options and complete the longest common part, then
" have further <Tab>s cycle through the possibilities:
set wildmode=list:longest,full

" use "[RO]" for "[readonly]" to save space in the message line:
set shortmess+=r

" display the current mode and partially-typed commands in the status line:
set showmode
set showcmd

" when using list, keep tabs at their full width and display `arrows':
execute 'set listchars+=tab:' . nr2char(187) . nr2char(183)
" (Character 187 is a right double-chevron, and 183 a mid-dot.)

" have the mouse enabled all the time:
set mouse=a

" don't have files trying to override this .vimrc:
set nomodeline


" * Text Formatting -- General

" don't make it look like there are line breaks where there aren't:
set nowrap

" use indents of 2 spaces, and have them copied down lines:
set shiftwidth=2
set shiftround
set expandtab
set autoindent

" normally don't automatically format `text' as it is typed, IE only do this
" with comments, at 79 characters:
set formatoptions-=t
set textwidth=79

" get rid of the default style of C comments, and define a style with two stars
" at the start of `middle' rows which (looks nicer and) avoids asterisks used
" for bullet lists being treated like C comments; then define a bullet list
" style for single stars (like already is for hyphens):
set comments-=s1:/*,mb:*,ex:*/
set comments+=s:/*,mb:**,ex:*/
set comments+=fb:*

" treat lines starting with a quote mark as comments (for `Vim' files, such as
" this very one!), and colons as well so that reformatting usenet messages from
" `Tin' users works OK:
set comments+=b:\"
set comments+=n::


" * Text Formatting -- Specific File Formats

" enable filetype detection:
filetype on

" recognize anything in my .Postponed directory as a news article, and anything
" at all with a .txt extension as being human-language text [this clobbers the
" `help' filetype, but that doesn't seem to prevent help from working
" properly]:
augroup filetype
  autocmd BufNewFile,BufRead */.Postponed/* set filetype=mail
  autocmd BufNewFile,BufRead *.txt set filetype=human
augroup END

" in human-language files, automatically format everything at 72 chars:
autocmd FileType mail,human set formatoptions+=t textwidth=72

" for C-like programming, have automatic indentation:
autocmd FileType c,cpp,slang set cindent

" for actual C (not C++) programming where comments have explicit end
" characters, if starting a new line in the middle of a comment automatically
" insert the comment leader characters:
autocmd FileType c set formatoptions+=ro

" for Perl programming, have things in braces indenting themselves:
autocmd FileType perl set smartindent

" for CSS, also have things in braces indented:
autocmd FileType css set smartindent

" for HTML, generally format text, but if a long line has been created leave it
" alone when editing:
autocmd FileType html set formatoptions+=tl

" for both CSS and HTML, use genuine tab characters for indentation, to make
" files a few bytes smaller:
autocmd FileType html,css set noexpandtab tabstop=2

" in makefiles, don't expand tabs to spaces, since actual tab characters are
" needed, and have indentation at 8 chars to be sure that all indents are tabs
" (despite the mappings later):
autocmd FileType make set noexpandtab shiftwidth=8


" * Search & Replace

" make searches case-insensitive, unless they contain upper-case letters:
set ignorecase
set smartcase

" show the `best match so far' as search strings are typed:
set incsearch

" assume the /g flag on :s substitutions to replace all matches in a line:
set gdefault


" * Spelling

" define `Ispell' language and personal dictionary, used in several places
" below:
let IspellLang = 'british'
let PersonalDict = '~/.ispell_' . IspellLang

" try to avoid misspelling words in the first place -- have the insert mode
" <Ctrl>+N/<Ctrl>+P keys perform completion on partially-typed words by
" checking the Linux word list and the personal `Ispell' dictionary; sort out
" case sensibly (so that words at starts of sentences can still be completed
" with words that are in the dictionary all in lower case):
execute 'set dictionary+=' . PersonalDict
set dictionary+=/usr/dict/words
set complete=.,w,k
set infercase

" correct my common typos without me even noticing them:
abbreviate teh the
abbreviate spolier spoiler
abbreviate Comny Conmy
abbreviate atmoic atomic

" Spell checking operations are defined next. They are all set to normal mode
" keystrokes beginning \s but function keys are also mapped to the most common
" ones. The functions referred to are defined at the end of this .vimrc.

" \si ("spelling interactive") saves the current file then spell checks it
" interactively through `Ispell' and reloads the corrected version:
execute 'nnoremap \si :w<CR>:!ispell -x -d ' . IspellLang . ' %<CR>:e<CR><CR>'

" \sl ("spelling list") lists all spelling mistakes in the current buffer,
" but excludes any in news/mail headers or in ("> ") quoted text:
execute 'nnoremap \sl :w ! grep -v "^>" <Bar> grep -E -v "^[[:alpha:]-]+: " ' .
  \ '<Bar> ispell -l -d ' . IspellLang . ' <Bar> sort <Bar> uniq<CR>'

" \sh ("spelling highlight") highlights (in red) all misspelt words in the
" current buffer, and also excluding the possessive forms of any valid words
" (EG "Lizzy's" won't be highlighted if "Lizzy" is in the dictionary); with
" mail and news messages it ignores headers and quoted text; for HTML it
" ignores tags and only checks words that will appear, and turns off other
" syntax highlighting to make the errors more apparent [function at end of
" file]:
nnoremap \sh :call HighlightSpellingErrors()<CR><CR>
nmap <F9> \sh

" \sc ("spelling clear") clears all highlighted misspellings; for HTML it
" restores regular syntax highlighting:
nnoremap \sc :if &ft == 'html' <Bar> sy on <Bar>
  \ else <Bar> :sy clear SpellError <Bar> endif<CR>
nmap <F10> \sc

" \sa ("spelling add") adds the word at the cursor position to the personal
" dictionary (but for possessives adds the base word, so that when the cursor
" is on "Ceri's" only "Ceri" gets added to the dictionary), and stops
" highlighting that word as an error (if appropriate) [function at end of
" file]:
nnoremap \sa :call AddWordToDictionary()<CR><CR>
nmap <F8> \sa


" * Keystrokes -- Moving Around

" have the h and l cursor keys wrap between lines (like <Space> and <BkSpc> do
" by default), and ~ covert case over line breaks; also have the cursor keys
" wrap in insert mode:
set whichwrap=h,l,~,[,]

" page down with <Space> (like in `Lynx', `Mutt', `Pine', `Netscape Navigator',
" `SLRN', `Less', and `More'); page up with - (like in `Lynx', `Mutt', `Pine'),
" or <BkSpc> (like in `Netscape Navigator'):
noremap <Space> <PageDown>
noremap <BS> <PageUp>
noremap - <PageUp>
" [<Space> by default is like l, <BkSpc> like h, and - like k.]

" scroll the window (but leaving the cursor in the same place) by a couple of
" lines up/down with <Ins>/<Del> (like in `Lynx'):
noremap <Ins> 2<C-Y>
noremap <Del> 2<C-E>
" [<Ins> by default is like i, and <Del> like x.]

" use <F6> to cycle through split windows (and <Shift>+<F6> to cycle backwards,
" where possible):
nnoremap <F6> <C-W>w
nnoremap <S-F6> <C-W>W

" use <Ctrl>+N/<Ctrl>+P to cycle through files:
nnoremap <C-N> :next<CR>
nnoremap <C-P> :prev<CR>
" [<Ctrl>+N by default is like j, and <Ctrl>+P like k.]

" have % bounce between angled brackets, as well as t'other kinds:
set matchpairs+=<:>

" have <F1> prompt for a help topic, rather than displaying the introduction
" page, and have it do this from any mode:
nnoremap <F1> :help<Space>
vmap <F1> <C-C><F1>
omap <F1> <C-C><F1>
map! <F1> <C-C><F1>


" * Keystrokes -- Formatting

" have Q reformat the current paragraph (or selected text if there is any):
nnoremap Q gqap
vnoremap Q gq

" have the usual indentation keystrokes still work in visual mode:
vnoremap <C-T> >
vnoremap <C-D> <LT>
vmap <Tab> <C-T>
vmap <S-Tab> <C-D>

" have Y behave analogously to D and C rather than to dd and cc (which is
" already done by yy):
noremap Y y$


" * Keystrokes -- Toggles

" Keystrokes to toggle options are defined here. They are all set to normal
" mode keystrokes beginning \t but some function keys (which won't work in all
" terminals) are also mapped.

" have \tp ("toggle paste") toggle paste on/off and report the change, and
" where possible also have <F4> do this both in normal and insert mode:
nnoremap \tp :set invpaste paste?<CR>
nmap <F4> \tp
imap <F4> <C-O>\tp
set pastetoggle=<F4>

" have \tf ("toggle format") toggle the automatic insertion of line breaks
" during typing and report the change:
nnoremap \tf :if &fo =~ 't' <Bar> set fo-=t <Bar> else <Bar> set fo+=t <Bar>
  \ endif <Bar> set fo?<CR>
nmap <F3> \tf
imap <F3> <C-O>\tf

" have \tl ("toggle list") toggle list on/off and report the change:
nnoremap \tl :set invlist list?<CR>
nmap <F2> \tl

" have \th ("toggle highlight") toggle highlighting of search matches, and
" report the change:
nnoremap \th :set invhls hls?<CR>


" * Keystrokes -- Insert Mode

" allow <BkSpc> to delete line breaks, beyond the start of the current
" insertion, and over indentations:
set backspace=eol,start,indent

" have <Tab> (and <Shift>+<Tab> where it works) change the level of
" indentation:
inoremap <Tab> <C-T>
inoremap <S-Tab> <C-D>
" [<Ctrl>+V <Tab> still inserts an actual tab character.]

" abbreviations:
iabbrev lfpg Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
iabbrev hse he/she
iabbrev sm Smylers


" * Keystrokes -- For HTML Files

" Some automatic HTML tag insertion operations are defined next. They are
" allset to normal mode keystrokes beginning \h. Insert mode function keys are
" also defined, for terminals where they work. The functions referred to are
" defined at the end of this .vimrc.

" \hc ("HTML close") inserts the tag needed to close the current HTML construct
" [function at end of file]:
nnoremap \hc :call InsertCloseTag()<CR>
imap <F8> <Space><BS><Esc>\hca

" \hp ("HTML previous") copies the previous (non-closing) HTML tag in full,
" including attributes; repeating this straight away removes that tag and
" copies the one before it [function at end of file]:
nnoremap \hp :call RepeatTag(0)<CR>
imap <F9> <Space><BS><Esc>\hpa
" \hn ("HTML next") does the same thing, but copies the next tag; so \hp and
" \hn can be used to cycle backwards and forwards through the tags in the file
" (like <Ctrl>+P and <Ctrl>+N do for insert mode completion):
nnoremap \hn :call RepeatTag(1)<CR>
imap <F10> <Space><BS><Esc>\hna

" there are other key mappings that it's useful to have for typing HTML
" character codes, but that are definitely not wanted in other files (unlike
" the above, which won't do any harm), so only map these when entering an HTML
" file and unmap them on leaving it:
autocmd BufEnter * if &filetype == "html" | call MapHTMLKeys() | endif
function! MapHTMLKeys(...)
" sets up various insert mode key mappings suitable for typing HTML, and
" automatically removes them when switching to a non-HTML buffer

  " if no parameter, or a non-zero parameter, set up the mappings:
  if a:0 == 0 || a:1 != 0

  " require two backslashes to get one:
  inoremap \\ \

  " then use backslash followed by various symbols insert HTML characters:
  inoremap \& &
  inoremap \< <
  inoremap \> >
  inoremap \. ·

  " em dash -- have \- always insert an em dash, and also have _ do it if
  " ever typed as a word on its own, but not in the middle of other words:
  inoremap \- —
  iabbrev _ —

  " hard space with <Ctrl>+Space, and \<Space> for when that doesn't work:
  inoremap \<Space>
  imap <C-Space> \<Space>

  " have the normal open and close single quote keys producing the character
  " codes that will produce nice curved quotes (and apostophes) on both Unix
  " and Windows:
  inoremap ` ‘
  inoremap ' ’
  " then provide the original functionality with preceding backslashes:
  inoremap \` `
  inoremap \' '

  " curved double open and closed quotes (2 and " are the same key for me):
  inoremap \2 “
  inoremap \" ”

  " when switching to a non-HTML buffer, automatically undo these mappings:
  autocmd! BufLeave * call MapHTMLKeys(0)

  " parameter of zero, so want to unmap everything:
  else
  iunmap \\
  iunmap \&
  iunmap \<
  iunmap \>
  iunmap \-
  iunabbrev _
  iunmap \<Space>
  iunmap <C-Space>
  iunmap `
  iunmap '
  iunmap \`
  iunmap \'
  iunmap \2
  iunmap \"

  " once done, get rid of the autocmd that called this:
  autocmd! BufLeave *

  endif " test for mapping/unmapping

endfunction " MapHTMLKeys()


" * `SLRN' Behaviour

" when using `SLRN' to compose a new news article without a signature, the
" cursor will be at the end of the file, the blank line after the header, so
" duplicate this line ready to start typing on; when composing a new article
" with a signature, `SLRN' includes an appropriate blank line but places the
" cursor on the following one, so move it up one line [if re-editing a
" partially-composed article, `SLRN' places the cursor on the top line, so
" neither of these will apply]:
autocmd VimEnter .article if line('.') == line('$') | yank | put |
  \ elseif line('.') != 1 | -

" when following up articles from people with long names and/or e-mail
" addresses, the `SLRN'-generated attribution line can have over 80 characters,
" which will then cause `SLRN' to complain when trying to post it(!), so if
" editing a followup for the first time, reformat the line (then put the cursor
" back):
autocmd VimEnter .followup if line('.') != 1 | normal gq${j


" * Functions Referred to Above

function! HighlightSpellingErrors()
" highlights spelling errors in the current window; used for the \sh operation
" defined above;
" requires the ispell, sort, and uniq commands to be in the path;
" requires the global variable IspellLang to be defined above, and to contain
" the preferred `Ispell' language;
" for mail/news messages, requires the grep command to be in the path;
" for HTML documents, saves the file to disk and requires the lynx command to
" be in the path
"
" by Smylers http://www.stripey.com/vim/
" (inspired by Krishna Gadepalli and Neil Schemenauer's vimspell.sh)

" 2000 Jun 1: for `Vim' 5.6

  " for HTML files, remove all current syntax highlighting (so that
  " misspellings show up clearly), and note it's HTML for future reference:
  if &filetype == 'html'
  let HTML = 1
  syntax clear

  " for everything else, simply remove any previously-identified spelling
  " errors (and corrections):
  else
  let HTML = 0
  if hlexists('SpellError')
  syntax clear SpellError
  endif
  if hlexists('Normal')
  syntax clear Normal
  endif
  endif

  " form a command that has the text to be checked piping through standard
  " output; for HTML files this involves saving the current file and processing
  " it with `Lynx'; for everything else, use all the buffer except quoted text
  " and mail/news headers:
  if HTML
  write
  let PipeCmd = '! lynx --dump --nolist % |'
  else
  let PipeCmd = 'write !'
  if &filetype == 'mail'
  let PipeCmd = PipeCmd . ' grep -v "^> " | grep -E -v "^[[:alpha:]-]+:" |'
  endif
  endif

  " execute that command, then generate a unique list of misspelt words and
  " store it in a temporary file:
  let ErrorsFile = tempname()
  execute PipeCmd . ' ispell -l -d '. g:IspellLang .
  \ ' | sort | uniq > ' . ErrorsFile

  " open that list of words in another window:
  execute 'split ' . ErrorsFile

  " for every word in that list ending with "'s", check if the root form
  " without the "'s" is in the dictionary, and if so remove the word from the
  " list:
  global /'s$/ execute 'read ! echo ' . expand('<cword>') .
  \ ' | ispell -l -d ' . g:IspellLang | delete
  " (If the root form is in the dictionary, ispell -l will have no output so
  " nothing will be read in, the cursor will remain in the same place and the
  " :delete will delete the word from the list. If the root form is not in the
  " dictionary, then ispell -l will output it and it will be read on to a new
  " line; the delete command will then remove that misspelt root form, leaving
  " the original possessive form in the list!)

  " only do anything if there are some misspellings:
  if strlen(getline('.')) > 0

  " if (previously noted as) HTML, replace each non-alphanum char with a
  " regexp that matches either that char or a &...; entity:
  if HTML
  % substitute /\W/\\(&\\|\&\\(#\\d\\{2,4}\\|\w\\{2,8}\\);\\)/e
  endif

  " turn each mistake into a `Vim' command to place it in the SpellError
  " syntax highlighting group:
  % substitute /^/syntax match SpellError !\\</
  % substitute /$/\\>!/
  endif

  " save and close that file (so switch back to the one being checked):
  exit

  " make syntax highlighting case-sensitive, then execute all the match
  " commands that have just been set up in that temporary file, delete it, and
  " highlight all those words in red:
  syntax case match
  execute 'source ' . ErrorsFile
  call delete(ErrorsFile)
  highlight SpellError term=reverse ctermfg=DarkRed guifg=Red

  " with HTML, don't mark any errors in e-mail addresses or URLs, and ignore
  " anything marked in a fix-width font (as being computer code):
  if HTML
  syntax case ignore
  syntax match Normal !\<[[:alnum:]._-]\+@[[:alnum:]._-]\+\.\a\+\>!
  syntax match Normal
  \ !\<\(ht\|f\)tp://[-[:alnum:].]\+\a\(/[-_.[:alnum:]/#&=,]*\)\=\>!
  syntax region Normal start=!<Pre>! end=!</Pre>!
  syntax region Normal start=!<Code>! end=!</Code>!
  syntax region Normal start=!<Kbd>! end=!</Kbd>!
  endif

endfunction " HighlightSpellingErrors()


function! AddWordToDictionary()
" adds the word under the cursor to the personal dictonary; used for the \sa
" operation defined above;
" requires the global variable PersonalDict to be defined above, and to contain
" the `Ispell' personal dictionary;
"
" by Smylers http://www.stripey.com/vim/

" 2000 Apr 30: for `Vim' 5.6

  " get the word under the cursor, including the apostrophe as a word character
  " to allow for words like "won't", but then ignoring any apostrophes at the
  " start or end of the word:
  set iskeyword+='
  let Word = substitute(expand('<cword>'), "^'\\+", '', '')
  let Word = substitute(Word, "'\\+$", '', '')
  set iskeyword-='

  " override any SpellError highlighting that might exist for this word,
  " `highlighting' it as normal text:
  execute 'syntax match Normal #\<' . Word . '\>#'

  " remove any final "'s" so that possessive forms don't end up in the
  " dictionary, then add the word to the dictionary:
  let Word = substitute(Word, "'s$", '', '')
  execute '!echo "' . Word . '" >> ' . g:PersonalDict

endfunction " AddWordToDictionary()


function! InsertCloseTag()
" inserts the appropriate closing HTML tag; used for the \hc operation defined
" above;
" requires ignorecase to be set, or to type HTML tags in exactly the same case
" that I do;
" doesn't treat <P> as something that needs closing;
" clobbers register z and mark z

" by Smylers http://www.stripey.com/vim/
" 2000 May 4

  if &filetype == 'html'

  " list of tags which shouldn't be closed:
  let UnaryTags = ' Area Base Br DD DT HR Img Input LI Link Meta P Param '

  " remember current position:
  normal mz

  " loop backwards looking for tags:
  let Found = 0
  while Found == 0
  " find the previous <, then go forwards one character and grab the first
  " character plus the entire word:
  execute "normal ?\<LT>\<CR>l"
  normal "zyl
  let Tag = expand('<cword>')

  " if this is a closing tag, skip back to its matching opening tag:
  if @z == '/'
  execute "normal ?\<LT>" . Tag . "\<CR>"

  " if this is a unary tag, then position the cursor for the next
  " iteration:
  elseif match(UnaryTags, ' ' . Tag . ' ') > 0
  normal h

  " otherwise this is the tag that needs closing:
  else
  let Found = 1

  endif
  endwhile " not yet found match

  " create the closing tag and insert it:
  let @z = '</' . Tag . '>'
  normal `z
  if col('.') == 1
  normal "zP
  else
  normal "zp
  endif

  else " filetype is not HTML
  echohl ErrorMsg
  echo 'The InsertCloseTag() function is only intended to be used in HTML ' .
  \ 'files.'
  sleep
  echohl None

  endif " check on filetype

endfunction " InsertCloseTag()


function! RepeatTag(Forward)
" repeats a (non-closing) HTML tag from elsewhere in the document; call
" repeatedly until the correct tag is inserted (like with insert mode <Ctrl>+P
" and <Ctrl>+N completion), with Forward determining whether to copy forwards
" or backwards through the file; used for the \hp and \hn operations defined
" above;
" requires preservation of marks i and j;
" clobbers register z

" by Smylers http://www.stripey.com/vim/

" 2000 May 4: for `Vim' 5.6

  if &filetype == 'html'

  " if the cursor is where this function left it, then continue from there:
  if line('.') == line("'i") && col('.') == col("'i")
  " delete the tag inserted last time:
  if col('.') == strlen(getline('.'))
  normal dF<x
  else
  normal dF<x
  if col('.') != 1
  normal h
  endif
  endif
  " note the cursor position, then jump to where the deleted tag was found:
  normal mi`j

  " otherwise, just store the cursor position (in mark i):
  else
  normal mi
  endif

  if a:Forward
  let SearchCmd = '/'
  else
  let SearchCmd = '?'
  endif

  " find the next non-closing tag (in the appropriate direction), note where
  " it is (in mark j) in case this function gets called again, then yank it
  " and paste a copy at the original cursor position, and store the final
  " cursor position (in mark i) for use next time round:
  execute "normal " . SearchCmd . "<[^/>].\\{-}>\<CR>mj\"zyf>`i"
  if col('.') == 1
  normal "zP
  else
  normal "zp
  endif
  normal mi

  else " filetype is not HTML
  echohl ErrorMsg
  echo 'The RepeatTag() function is only intended to be used in HTML files.'
  sleep
  echohl None

  endif

endfunction " RepeatTag()


" end of Smylers's .vimrc