M y    b r a i n    h u r t s  !                                           w e                 r e a l l y                 t h i n k                   w h a t                y o u             k n o w

27 November 2009

VIM usability: my .vimrc

My .vimrc compiled from various Internet sources. The goal was to make vim more user-friendly for someone who is familiar with popular shortcuts like Ctr+C for copy and Ctr+V for paste, but still retaining most classic vim shortcuts. Since Ctr+V cannot be used for vertical block selection it still can be done with Ctr+Q if your OS allows it.
"set verbose=9
nnoremap <silent> <Space> :silent noh<Bar>echo<CR>
autocmd VimEnter * set vb t_vb=

"use arrows to move within wrapped lines
noremap <Up> gk
noremap <Down> gj

" Make Alt+Left, Alt+Right work like in eclipse
": retrace your movements in file (backward)
noremap <M-Left> <C-O>
": retrace your movements in file (forward)
noremap <M-Right> <C-I>


"map <M-F3> :guu!<Bar>:gUU?<CR>

" toggle line numbers with F12
map <F12> :set number!<Bar>set number?<CR>
" Toggle line wrapping
nnoremap <F3> :set wrap!<CR>
"nnoremap <F11> :set nonumber!<CR>:set foldcolumn=0<CR>
map <F10> :set paste!<Bar>set paste?<CR>
map <F11> :set hls!<Bar>set hls?<CR>
" run current file on windows http://vim.wikia.com/wiki/Best_Vim_Tips
vmap <silent> <C-F5> :<C-U>let old_reg=@"<CR>gvy:silent!!start rundll32.exe url.dll,FileProtocolHandler <C-R><C-R>"<CR><CR>:let @"=old_reg<CR>


" Use CUA keystrokes for clipboard: CTRL-X, CTRL-C, CTRL-V and CTRL-z
" source $VIMRUNTIME/mswin.vim
" :::from mswin::
" Alt-Space is System menu
if has("gui")
  noremap <M-Space> :simalt ~<CR>
  inoremap <M-Space> <C-O>:simalt ~<CR>
  cnoremap <M-Space> <C-C>:simalt ~<CR>
endif

" CTRL-A is Select all
noremap <C-A> gggH<C-O>G
inoremap <C-A> <C-O>gg<C-O>gH<C-O>G
cnoremap <C-A> <C-C>gggH<C-O>G
onoremap <C-A> <C-C>gggH<C-O>G
snoremap <C-A> <C-C>gggH<C-O>G
xnoremap <C-A> <C-C>ggVG

" CTRL-F4 is Close window
noremap <C-F4> <C-W>c
inoremap <C-F4> <C-O><C-W>c
cnoremap <C-F4> <C-C><C-W>c
onoremap <C-F4> <C-C><C-W>c

" backspace and cursor keys wrap to previous/next line
set backspace=indent,eol,start whichwrap+=<,>,[,]

" backspace in Visual mode deletes selection
vnoremap <BS> d

" CTRL-X and SHIFT-Del are Cut
vnoremap <C-X> "+x
vnoremap <S-Del> "+x

" CTRL-C and CTRL-Insert are Copy
vnoremap <C-C> "+y
vnoremap <C-Insert> "+y

" CTRL-V and SHIFT-Insert are Paste
map <C-V>       "+gP
map <S-Insert>      "+gP

cmap <C-V>      <C-R>+
cmap <S-Insert>     <C-R>+


" switch tabs with C+Tab and C+Sh+Tab
map <C-Tab> :tabn <CR>
map <C-S-Tab> :tabp <CR>
set guitablabel=%n/\ %t\ %M
map <C-t> :tabnew<CR>
map <C-left> :tabp<CR>
map <C-right> :tabn<CR>

set noeb

set vb t_vb=""

set noerrorbells
set backupdir=~/.vim_backup

" highlight current line
set cul
" from http://www.pixelbeat.org/vim.tips.html
map <f9> :w<CR>:!python %<CR>

" Only run the script once
if exists("g:did_myvimrc")
  finish
endif

function SmartTab()

  if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
    return "\<Tab>"
  else
    if &omnifunc != ''
      return "\<C-X>\<C-O>"
    elseif &dictionary != ''
      return "\<C-K>"
    else
      return "\<C-N>"
    endif
  endif

endfunction

" Bind tab to the SmartTab function
" inoremap <Tab> <C-R>=SmartTab()<cr>


if has("autocmd")
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal g'\"" |
  \ endif
endif



nnoremap <M-Right> <C-W><Right>
nnoremap <M-Left> <C-W><Left>
nnoremap <M-Up> <C-W><Up>
nnoremap <M-Down> <C-W><Down>


"http://vim.wikia.com/wiki/Vimrc
" Press space to clear search highlighting and any message already displayed.
nnoremap <silent> <Space> :silent noh<Bar>echo<CR>

set ignorecase
set smartcase
let g:did_myvimrc = 1

" Overview:
" - I use vim 7 in xterminals (gnome-terminal, console, gvim, etc) on light
"   backgrounds.
" - In non-xterminals (like the typical linux terminal) I use dark
"   backgrounds.
" - Generally, I don't change default vim keyboard shortcuts, such that I can
"   use 'pristine' vim installations as well.
" - I enable all the 'cool' features: code folding, syntax highlighting,
"   automatic complete, filetype plugins, indentation, line numbering, custom
"   ruler, custom status line.
" - Even if I use gvim, I actually don't use the GUI features. I disable
"   pretty much everything, except the menus.
" - I use vim modelines.
" - I use the longer names for commands and settings (e.g. 'tabstop' instead
"   of 'ts'), for readability.
" - When saving files the RD_FileWritePre() function checks for $Date: ... $
"   to update the date.
" - I enabled .exrc/.vimrc files per-directory, and I disabled any unsafe
"   commands in such files.
" - New keyboard shortcuts:
"   _' _{ _( _[ _< _t _W _T _c _C _"
"   These are selection wrappers. Select something and type _' to have single
"   quotes around the selection. In normal mode type _W to unwrap the string.
"   For example move over the ' single quote character and type _W: this will
"   remove the ' and the other matching quote.
"   _t and _T are for XML/HTML tags. _t to wrap the selection around a given
"   tag name. _T to strip the tag under the cursor.
"   _c and _C are for comments. It will detect the file type and use propers
"   comments.
" - New command: Fullscreen. Nice for editing text-only documents.
" - I use the matchit and taglist plugins. I do not use XML edit plugins.
" - I have my own XML edit functions, which I like better than those available
"   online in other scripts.
" - New abbreviations:
"   isodate
"   Adds the date in ISO format: 2008-11-04 22:43:09 +0200.
"
"   $Date$
"   Adds the date in ISO format, with a twist: $Date: 2009-05-29 13:18:26 E. Europe Daylight Time $ 
"
"   rdcopy
"   My usual 'copyright' notice.
"
" Problems:
" - _c and _C (comment wrapping) is not very good at the moment.
" - XML/HTML editing still needs improvements, but it's usable.


" Script settings
" ===============

" Memory usage: I allow vim to use a lot of memory.
let s:RD_use_mem = 1

" I allow vim to use colors
let s:RD_use_conn = 1

" I allow vim to save as much information as possible.
let s:RD_privacy = 0

" I change the above values as needed when I copy my .vimrc to different servers.


" Settings
" ========

" I do not like the vi-compatible mode.
set nocompatible

" Make backspace work as usual.
set backspace=indent,eol,start

" Diff option: ignore whitespace
set diffopt+=iwhite

" Allow changing the buffer without saving.
set hidden

if s:RD_use_mem
    " How many lines of history to remember.
    set history=1000

    " Use all the memory needed, for maximum performance.
    set maxmemtot=2000000
    set maxmem=2000000
    set maxmempattern=2000000
endif


" Do not place the cursor at the start of the line when using Page up/down.
set nostartofline

set nrformats=alpha,hex

" Allow the cursor to be positioned where there is no actual character.
" Visual block mode only.
set virtualedit=block

if s:RD_privacy == 1
    set viminfo=''
else
    " Remember information between vim restarts.
    set viminfo='200,<300,s150,%,h,!
endif

" Allow specified keys that move the cursor left/right to move to the previous/next line when the cursor is on the first/last character in the line.
" b (backspace), s (space) and the arrows.
set whichwrap=b,s,<,>


" Auto-complete
" =============

" Scanning included files is really slow for Perl files
" Maybe I should change this only for Perl files
set complete-=i

" Turn on wild menu which shows auto-complete options in the command mode
set wildmenu


" File-related
" ============

" Unicode support
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,latin1,default

" Support all three, in this order
set fileformats=unix,dos,mac

" Detect the type of file and enable filetype-specific indenting.
filetype plugin indent on

" Backups
set backup
set backupcopy=auto
set backupskip=/tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*,*test*,*temp*,*tmp*,*tst*,*~,*bak

" (I decided I like the Visual mode more)
" Start the selection mode with Shift+Arrows/Home/End/PageUp/PageDown, and with
" the mouse
"set selectmode=mouse,key

" Start the Visual/Selection mode with using Shift+Arrows/etc
set keymodel=startsel


" User interface
" ==============

if has("gui_win32")
    set guifont=Bitstream_Vera_Sans_Mono:h9:cANSI
endif

"if (has("gui_running") || $TERM == 'xterm')
"   set background=light
"else
"   set background=dark
"endif

" The command line height.
set cmdheight=2

" As much as possible of the last line in a window will be displayed. When not included, a last line that doesn't fit is replaced with "@" lines.
set display=lastline

" GUI Options:
"if has("gui_running")
"   " GUI cursor: no blinking
"   set guicursor+=a:blinkon0
"
"   " no toolbar
"   set guioptions-=T
"   " no autoselect 
"   set guioptions-=a
"
"   " Use console messages instead of GUI dialogs
"   set guioptions+=c
"endif

" Always show the status line
set laststatus=2

" Don't blink!
set novisualbell

" Show the line numbers.
set number

" Threshold for reporting number of lines changed.
set report=0

" Enable the ruler with the format:
" {buffer number}{modified/readonly flag}: {file type} [current line, current column] {position percentage in file}
set ruler
set rulerformat=%25(%n%m%r:\ %Y\ [%l,%v]\ %p%%%)

" Minimal number of screen lines to keep above and below the cursor.
set scrolloff=3

" Shorter messages.
set shortmess=as

" Show (partial) command in status line.
set showcmd

" Status line format:
" {buffer number}: {file name, relative path to the current working directory}{modified flag}{readonly flag}
" {help flag}{preview flag} [file type, encoding, format] [current line-total lines, current column][position percentage in file]
set statusline=%n:\ %f%m%r%h%w\ [%Y,%{&fileencoding},%{&fileformat}]\ [%l-%L,%v][%p%%]

if s:RD_use_conn
    set ttyfast
endif

" Do not redraw while running macros (much faster).
set lazyredraw

" What to show when you do :set list.
set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$

" Allow using the mouse everywhere.
set mouse=a

" Nice, but too slow..
"
"set cursorline
"set cursorcolumn
"highlight CursorLine guibg=#f7f7f7
"highlight CursorColumn guibg=#f7f7f7


" Formatting and indenting
" ========================

set formatoptions=tcroqn " see help

" Automatic indenting, on new line.
set autoindent

" Enable C-style indenting.
set cindent

" Real tab chars.
"set noexpandtab

" Smart indenting, based on the typed code.
set smartindent

" Tab spacing.
set tabstop=4
set shiftwidth=4
set softtabstop=4

" Use tabs at the start of a line, spaces elsewhere.
"set smarttab 

" If on Vim will wrap long lines at a character in 'breakat' rather than at the last character that fits on the screen.
set linebreak


" Syntax highlighting and related
" ===============================

if s:RD_use_conn
    " Enable syntax highlighting
    syntax on
endif

" Show matching brackets.
set showmatch

" How many tenths of a second to blink matching brackets for
set mat=5

" Do not highlight the string we searched.
set nohlsearch

" Highlight the searched string, while typing.
set incsearch

" Highlight advanced perl vars inside strings
"let perl_extended_vars=1 

" Highlight HTML code inside PHP strings
let php_htmlInStrings=1

" Highlight mySQL queries inside PHP strings
"let php_sql_query=1

" Highlight PHP code inside ASP-like tags
"let php_asp_tags=1


" Code folding
" ============

"set foldenable 

" Fold column width
"set foldcolumn=4

" Make folding indent sensitive
"set foldmethod=indent 

"set foldminlines=2

" Folds with a higher level will be automatically closed.
"set foldlevel=4

" Don't auto-open folds
"set foldopen=


" (Auto) commands
" ===============

" Add the Eretidy command
"command Eretidy call <SID>RD_EditRetidy()

" This adds the new Fullscreen command.
command Fullscreen call <SID>RD_Fullscreen()

" Skip file change notifications in mounted folders
"autocmd FileChangedShell /mnt/*,~/mnt/* call <SID>RD_FileChanged()

" Automatically update $Date: ... $ when the file is saved. 
"autocmd FileWritePre,BufWritePre * call <SID>RD_FileWritePre()

"` Mappings
" ========

" DISABLED: These cause small issues.
" I like automatic completion for quotes and parentheses.
"inoremap " ""<LEFT>
"inoremap ' ''<LEFT>
"inoremap ( ()<LEFT>
"inoremap [ []<LEFT>
"inoremap ` ``<LEFT>
"inoremap { {}<LEFT>

" Make the Home key jump to the first non-white space, or to the start of the
" line.
inoremap <silent> <Home> <C-o>:call <SID>RD_map_home('i')<CR>
"vnoremap <silent> <Home> :<C-u>call <SID>RD_map_home('v')<CR>
nnoremap <silent> <Home> :call <SID>RD_map_home('n')<CR>

let mapleader='_'

" Quick way to change the directory to the file I am currently editing
noremap <silent> <Leader>. :exe 'cd ' . substitute(expand("%:p:h"), ' ', "\\\\ ", 'g')<CR>

" This allows me to quickly wrap any selection with some usual characters
" (quotes, angle quotes, parentheses, etc)
vnoremap <silent> <Leader>" :<C-u>call <SID>RD_wrapper('"')<CR>
vnoremap <silent> <leader>' :<c-u>call <sid>rd_wrapper("'")<cr>
vnoremap <silent> <leader>` :<c-u>call <sid>rd_wrapper('`')<cr>
vnoremap <silent> <leader>{ :<c-u>call <sid>rd_wrapper('{')<cr>
vnoremap <silent> <leader>( :<c-u>call <sid>rd_wrapper('(')<cr>
vnoremap <silent> <leader>[ :<c-u>call <sid>rd_wrapper('[')<cr>
vnoremap <silent> <leader>< :<c-u>call <sid>rd_wrapper('<')<cr>
vnoremap <silent> <leader>t :<c-u>call <sid>rd_wrapper('t')<cr>
nnoremap <silent> <leader>w :call <sid>rd_unwrapper()<cr>
nnoremap <silent> <leader>t :call <sid>rd_unwrap_tag()<cr>

" add/remove comments
vnoremap <silent> <leader>c :<c-u>call <sid>rd_comment('v')<cr>
vnoremap <silent> <leader>c :<c-u>call <sid>rd_uncomment('v')<cr>
nnoremap <silent> <leader>c :call <sid>rd_comment('n')<cr>
nnoremap <silent> <leader>c :call <sid>rd_uncomment('n')<cr>

" Tag lists toggle.
" If the taglist vim plugin is available.
if (exists('loaded_taglist') && loaded_taglist != 'no')
    noremap <silent> <F8> :TlistToggle<CR>
    noremap <silent> <S-F8> :TlistOpen<CR>
endif

" Autocommands
" ============

" Ensure every file does syntax highlighting (full)
"autocmd BufEnter * :syntax sync fromstart

autocmd FileType html,xml,xhtml call <SID>RD_XML_edit()
autocmd FileType php setlocal iskeyword+=@

" Sets the current working directory based on the file being edited.
autocmd BufEnter * lcd %:p:h

" Abbreviations
" =============

" Current date and time, ISO format.
iab isodate <C-r>=<SID>RD_isodate()<CR>

iab $Date$  <C-r>="$Date: " . <SID>RD_isodate() . " $"<CR>
iab :vimset: <C-r>=<SID>RD_vimset_generate()<CR>
"
" My usual "copyright" line. I put this in almost all files I code.
"iab rdcopy <C-o>:call <SID>RD_copyright()<CR>


" Variables
" =========

" Changes for the :TOhtml command
let use_xhtml = 1
let html_use_css = 1
let html_ignore_folding = 1
let html_number_lines = 0

" Changes for the TagList
let Tlist_Enable_Fold_Column = 0
let Tlist_File_Fold_Auto_Close = 1
let Tlist_Sort_Type = 'order'
let Tlist_Use_Right_Window = 1
let Tlist_Inc_Winwidth = 0

" Functions
" =========

" Output ISO date.
function s:RD_isodate ()
    return strftime("%Y-%m-%d %H:%M:%S %z")
endfunction

" Output a vim:set ... : modeline, based on current settings.
function s:RD_vimset_generate ()
    let l:str = 'vim:set'

    if (&spell && (&filetype == 'html' || &filetype == 'xhtml'))
        let l:str .= ' spell'
        if !empty(&spelllang)
            let l:str .= ' spl=' . &spelllang
        endif
    endif

    let l:str .= ' ts=' . &tabstop . ' sw=' . &shiftwidth . ' sts=' . &softtabstop

    if &smarttab
        let l:str .= ' sta'
    else
        let l:str .= ' nosta'
    endif

    if &expandtab
        let l:str .= ' et'
    else
        let l:str .= ' noet'
    endif

    if !empty(&fileencoding)
        let l:str .= ' fenc=' . &fileencoding
    endif

    if !empty(&fileformat)
        let l:str .= ' ff=' . &fileformat
    endif

    let l:str .= ':'

    return l:str
endfunction

" This function toggles the 'fullscreen' mode. I sometimes need this, so I can
" just view my files, without all the 'clutter': line numbers, folding,
" command line, status line, etc.
let s:fullscreen_toggle = 0
function s:RD_Fullscreen ()
    if s:fullscreen_toggle == 0
        set cmdheight=1
        set nonumber
        set laststatus=0
        set foldcolumn=0
        let s:fullscreen_toggle = 1
    else
        set cmdheight=2
        set number
        set laststatus=2
        set foldcolumn=4
        let s:fullscreen_toggle = 0
    endif
endfunction


" This function is used for quickly opening my ReTidy script (often needed
" when cleaning pages).
" Check out: http://www.robodesign.ro/mihai/my-projects/retidy
"let s:eretidy_toggle = 0
"function s:RD_EditRetidy ()
""  if s:eretidy_toggle == 0
"       let l:cwd = getcwd()
""      cd /path/to/retidy
"       e src/retidy.php
"       e examples/retidy-stdio.php
"       e profiles/awebitor.php
"       e profiles/maximum.php
""      e profiles/custom.php
""      cd `=l:cwd`
""      let s:eretidy_toggle = 1
"   else
"       bdel src/retidy.php examples/retidy-stdio.php profiles/awebitor.php profiles/maximum.php profiles/custom.php
"       let s:eretidy_toggle = 0
"   endif
"endfunction

" This function updates the $Date: ... $ definition to always be the date and
" time of the last file write.
"function s:RD_FileWritePre ()
"   if (&readonly || !&modified)
"       return
"   endif
"
"   let l:my_search = "\\$Date: [0-9- +:]\\+ \\$"
"   let l:my_cpos   = getpos('.')
""  call cursor(1,1)
"   let l:my_spos   = search(l:my_search, 'n', 400)
"
"   if l:my_spos > 0
"       let l:my_date = <SID>RD_isodate()
""      let l:my_line = getline(l:my_spos)
""      let l:my_line = substitute(l:my_line, l:my_search, "$Date: " . l:my_date . " $", "e")
""      call setline(l:my_spos, l:my_line)
"   endif
"
"   call setpos('.', l:my_cpos)
"endfunction
"
" This function avoids an annoying FileChanged dialog for some folders I want
"" (cifs mounted folders)
"function s:RD_FileChanged ()
"   echohl WarningMsg
"
"   if v:fcs_reason == 'deleted'
"       let v:fcs_choice = 'ask'
""      echo 'File deleted!'
""  else
"       echo 'File changed!'
"   endif
"
"   echohl None
"endfunction
"

" This makes the Home key jump to the first non-whitespace character, or to
" the start of the line. Inspired by Cream. (cream.sf.net)
function s:RD_map_home (mode)
    let l:oldcol = col('.')
    if l:oldcol == 1
        return 1
    endif

    normal ^

    let l:newcol = col('.')
    if l:oldcol != l:newcol
        return l:newcol
    endif

    normal 0

    return 1
endfunction


" This function nicely wraps a selection within quotes, curly braces, square
" brackets, angle brackets, tags and more.
function s:RD_wrapper (str) range
    let l:allowed_strs = {
                \ '"' : ['"', '"'],
                \ "'" : ["'", "'"],
                \ '{' : ['{', '}'],
                \ '(' : ['(', ')'],
                \ '[' : ['[', ']'],
                \ '`' : ['`', '`'],
                \ '<' : ['<', '>'],
                \ 't' : ['', '']
                \ }

    if has_key(l:allowed_strs, a:str) != 1
        echohl ErrorMsg
        echo 'Unknown wrapper: "' . a:str . '".'
        echohl None

        return 0
    endif

    " Wrap the selection with a tag.
    if a:str == 't'
        let l:tname = inputdialog('Tag name: ')
        if strlen(l:tname) < 1
            return 0
        endif
        let l:allowed_strs[a:str][0] = '<' . l:tname . '>'
        let l:allowed_strs[a:str][1] = '</' . l:tname . '>'
    endif

    let l:prefix = l:allowed_strs[a:str][0]
    let l:suffix = l:allowed_strs[a:str][1]
    let l:lenp = strlen(l:prefix)

    let l:old_x = @x
    let l:old_reg = @"

    let @x = l:prefix
    normal `<"xP
    let l:line1 = line('.')

    normal `>
    let l:line2 = line('.')

    if l:line1 == l:line2
        exe 'normal ' . lenp . 'l'
    endif

    let @x = l:suffix
    normal "xp

    " If we are wrapping the selection with a tag, lets put the cursor exactly
    " where the user can just press 'i' to start writing the attributes.
    if a:str == 't'
        normal h%h%
    endif

    let @x = l:old_x
    let @" = l:old_reg

    return 1
endfunction


" Similar to the RD_wrapper function, this one removes the quotes, curly
" braces, etc.
function s:RD_unwrapper () range
    let l:allowed_strs = ['"', "'", '{', '}', '(', ')', '[', ']', '`', '<', '>']
    let l:xml_types = ['xhtml', 'html', 'xml']
    let l:str = strpart(getline('.'), col('.') - 1, 1)

    if index(l:allowed_strs, l:str) == -1

        " If this is a known XML/SGML file type, then try to strip the current
        " tag
        if index(l:xml_types, &filetype) != -1
            return <SID>RD_unwrap_tag()
        endif

        echohl ErrorMsg
        echo 'The character under the cursor was not recognized: "' . l:str . '"'
        echohl None

        return 0
    endif

    let l:old_x = @x
    let l:old_reg = @"

    exe 'normal vi' . l:str . 'v'

    normal `<X
    let l:line1 = line('.')

    normal `>
    let l:line2 = line('.')
    if(l:line1 != l:line2)
        normal l
    endif
    normal x

    let @x = l:old_x
    let @" = l:old_reg

    return 1
endfunction




" This function makes the selection commented, based on &filetype.
" This is something simple, sufficient for my needs.
function s:RD_comment (mode) range
    let l:known_types = {
                \ 'vim' : [1, '"'],
                \ 'sh' : [1, '#'],
                \ 'php' : [2, '/*', '*/'],
                \ 'css' : [2, '/*', '*/'],
                \ 'javascript' : [2, '/*', '*/'],
                \ 'html' : [2, '<!--', '-->'],
                \ 'xhtml' : [2, '<!--', '-->'],
                \ 'xml' : [2, '<!--', '-->']
                \ }

    if has_key(l:known_types, &filetype) != 1
        echohl ErrorMsg
        echo 'Unknown file type.'
        echohl None

        return 0
    endif

    if (a:mode != 'v' && a:mode != 'n')
        echohl ErrorMsg
        echo 'Unknown mode.'
        echohl None

        return 0
    endif

    let l:old_x = @x
    let l:old_reg = @"

    let l:ctype = l:known_types[&filetype][0]
    let l:cprefix = l:known_types[&filetype][1]
    let l:lenp = strlen(l:cprefix)

    if l:ctype == 2
        let l:csuffix = l:known_types[&filetype][2]
    endif

    if l:ctype == 1
        if a:mode == 'v'
            exe "'<,'>s~^~" . l:cprefix . "~e"
        else
            exe "s~^~" . l:cprefix . "~e"
        endif
    elseif l:ctype == 2
        if a:mode == 'v'
            normal `<
        else
            normal ^
        endif

        let @x = l:cprefix
        normal "xP
        let l:line1 = line('.')

        if a:mode == 'v'
            normal `>
        else
            normal $
        endif

        let l:line2 = line('.')

        if (l:line1 == l:line2 && a:mode == 'v')
            exe 'normal ' . l:lenp . 'l'
        endif

        let @x = l:csuffix
        normal "xp
    endif

    let @x = l:old_x
    let @" = l:old_reg

    return 1
endfunction


" This function uncomments the text within the selection.
function s:RD_uncomment (mode) range
    let l:known_types = {
                \ 'vim' : {
                    \ '1l' : '"'
                    \ },
                \ 'sh' : {
                    \ '1l' : '#'
                    \ },
                \ 'php' : {
                    \ '1l' : '//\|#',
                    \ 'multi' : ['/\*', '\*/']
                    \ },
                \ 'css' : {
                    \ 'multi' : ['/\*', '\*/']
                    \ },
                \ 'javascript' : {
                    \ '1l' : '//',
                    \ 'multi' : ['/\*', '\*/']
                    \ },
                \ 'html' : {
                    \ 'multi' : ['<!--', '-->']
                    \ },
                \ 'xhtml' : {
                    \ 'multi' : ['<!--', '-->']
                    \ },
                \ 'xml' : {
                    \ 'multi' : ['<!--', '-->']
                    \ }
                \ }

    if has_key(l:known_types, &filetype) != 1
        echohl ErrorMsg
        echo 'Unknown file type.'
        echohl None

        return 0
    endif

    if (a:mode != 'v' && a:mode != 'n')
        echohl ErrorMsg
        echo 'Unknown mode.'
        echohl None

        return 0
    endif

    let l:ctype = l:known_types[&filetype]

    if has_key(l:ctype, '1l') == 1
        if a:mode == 'v'
            exe "'<,'>s~^\\(\\s*\\)" . l:ctype['1l'] . "~\\1~e"
        else
            exe "s~^\\(\\s*\\)" . l:ctype['1l'] . "~\\1~e"
        endif
    endif

    if has_key(l:ctype, 'multi') == 1
        if a:mode == 'v'
            exe "'<s~" . l:ctype['multi'][0] . "~~e"
            exe "'>s~" . l:ctype['multi'][1] . "~~e"
        else
            exe "s~" . l:ctype['multi'][0] . "~~e"
            exe "s~" . l:ctype['multi'][1] . "~~e"
        endif
    endif

    return 1
endfunction


" This function nicely generates the copyright string I want, in a comment
" suited for the current filetype
function s:RD_copyright ()
    exe "normal i Âc " . strftime("%Y") . " ROBO Design\rhttp://www.robodesign.ro "

    normal Vk_c$
endfunction


" This function makes some changes which enhance the editing of XML/SGML
" documents.
" Inspired by http://www.pinkjuice.com/howto/vimxml/setup.xml
function s:RD_XML_edit()
    setlocal matchpairs+=<:>
    inoremap <silent> <buffer> > ><Esc>:call <SID>RD_autocomplete_tag()<Cr>
    inoremap <buffer> <LocalLeader>> >
endfunction

" This function automatically adds the close tag as you type, in (X)HTML and
" XML
function s:RD_autocomplete_tag( )
    " Save registers
    let l:old_reg = @"

    let l:tmp = strpart (getline ("."), col (".") - 2, 2)
    if l:tmp == '/>'
        silent! normal l
        if col('.') == (col('$')-1)
            startinsert!
        else
            startinsert
        endif
        return 0
    endif

    if (!exists("g:xml_no_auto_nesting") && l:tmp == '>>')
        let l:multiline = 1
        normal X
    else
        let l:multiline = 0
    endif

    if (&filetype == 'html' || &filetype == 'xhtml')
        let l:html_mode = 1
        let l:html_tags = ['area', 'base', 'br', 'frame', 'hr', 'img', 'input', 'link', 'meta', 'param']
    else
        let l:html_mode = 0
    endif

    normal y%%
    let l:ltag = @"
    let l:space = stridx(l:ltag, ' ')
    let l:lbracket = stridx(l:ltag, '<', 1)
    let l:rbracket = stridx(l:ltag, '>')

    " Check if this is a malformed tag
    if (l:lbracket != -1 || l:rbracket != ( strlen(l:ltag)-1 ) || (l:space != -1 && l:space < 2) )
        let @" = l:old_reg
        startinsert
        return 0
    endif

    " Get the tag name
    if l:space != -1
        let l:tname = strpart(l:ltag, 1, l:space-1)
    else
        let l:tname = strpart(l:ltag, 1, strlen(l:ltag)-2)
    endif

    " Tag name too short ... or it's the end tag.
    " Also check if this is <!...> or <?...>.
    " Additionally, don't autocomplete some known HTML tags
    if ( strlen(l:tname) < 1 || stridx(l:tname, '/') != -1 || stridx(l:tname, '!') != -1 || stridx(l:tname, '?') != -1 || (l:html_mode == 1 && index(l:html_tags, l:tname) != -1) )
        let @" = l:old_reg
        silent! normal l
        if col('.') == (col('$')-1)
            startinsert!
        else
            startinsert
        endif
        return 0
    endif

    let @" = l:tname

    " Add the ending tag if multiline!=1
    if l:multiline == 1
        exe "silent! normal! ax\<Esc>r\nOx\<Esc>x"
        startinsert!
    else
        exe "normal! a</\<Esc>pa>\<Esc>%"
        startinsert
    endif

    let @" = l:old_reg

    return 1
endfunction


" The end
" =======

" Enable .exrc files (.vimrc files per-directory)
set exrc

" Disable any unsafe commands in .exrc files
set secure

" vim:set spell spl=en ts=4 sw=4 sts=4 sta noet fenc=utf-8 ff=unix:
" save with F2 
nmap <F2> :w<CR>

set wildmenu
set wcm=<Tab>
menu Encoding.latin1   :e ++enc=latin1<CR>
menu Encoding.koi8-r   :e ++enc=koi8-r<CR>
menu Encoding.windows-1251 :e ++enc=cp1251<CR>
menu Encoding.windows-1252 :e ++enc=cp1252<CR>
menu Encoding.ibm-866      :e ++enc=ibm866<CR>
menu Encoding.utf-8        :e ++enc=utf-8 <CR>
map <F8> :emenu Encoding.<TAB>

" Search for selected text, forwards or backwards.
" replaced vsearch.vim plugin
vnoremap <silent> * :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy/<C-R><C-R>=substitute(
  \escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
  \let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \gvy?<C-R><C-R>=substitute(
  \escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \gV:call setreg('"', old_reg, old_regtype)<CR>

vmap <C-k> *

" save both with :w and :W
:command -nargs=? -range=% -complete=file -bang W <line1>,<line2>w<bang> <args>
:command -bang Q <line1>,<line2>q<bang> <args>


:command WQ wq
:command Wq wq
iab MoN January February March April May June July August September October November December
iab MoO Jan Feb Mar Apr May Jun Jul Aug Sep Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
iab NuM 12345678901234567890123456789012345678901234567890123456789012345678901234567890
iab RuL ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0


No comments :

Post a Comment

The Java Posse (Most Recent Podcasts)