Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " Rev 103
- " ==============================
- " General Setup
- " ==============================
- " Disable compatibility with vi (must come first)
- set nocompatible
- " Restore screen contents when exiting Vim
- set restorescreen
- " Enable the mouse for all modes
- set mouse=a
- " Hide mouse while typing text
- set mousehide
- " Use all abbreviations in messages
- set shortmess=a
- " Preferably use Unix file format
- set fileformats=unix,dos
- " Display line numbers in front of each line
- set number
- " Never report changes
- set report=10000
- " Display commands in the bottom right corner as they are typed
- set showcmd
- " Size of the history
- set history=150
- " Use a higher timeout for mappings, so that keycodes will time out first
- set timeout ttimeoutlen=40 timeoutlen=45
- " Don't keep backups
- set nobackup
- set writebackup
- " Use <Tab> for command-line completion
- set wildchar=<Tab>
- " Display a list of matches when using command-line completion
- set wildmenu
- set wildmode=full
- set wildignore=*.o,*.obj,*.pyc,*.pyo,*.swp,.sconsign.dblite
- " Disable any kind of bell
- set t_vb=
- set noerrorbells
- set novisualbell
- " Smoother redraws
- set ttyfast
- " Do not redraw when running macros
- set lazyredraw
- " Title of the window
- set title
- set titlestring=%F\ %m
- " Abbrev for tabs
- cnoreabbrev tt tabnew
- cnoreabbrev tc tabclose
- cnoreabbrev tp tabprevious
- cnoreabbrev tn tabnext
- " ==============================
- " Text edition
- " ==============================
- " Syntax highlighting
- syntax on
- " Don't cut lines
- set textwidth=0
- " Don't put two spaces after . ? ! when joining lines
- set nojoinspaces
- " Tab width
- set shiftwidth=4
- set softtabstop=4
- " Uncomment to Insert spaces insteads of tabs
- " set expandtab
- " Allow the backspace key to delete anything in insert mode
- set backspace=indent,eol,start
- " Display trailing spaces using ~ characters and tabs with -> characters
- set listchars=tab:<-,trail:~
- " Don't break words
- set linebreak
- " Display as many lines as possible
- set display=lastline
- " Always keep 20 lines above and below the cursor if possible
- set scrolloff=20
- " Virtual editing in block mode
- set virtualedit=block
- " ==============================
- " Searching/Replacing
- " ==============================
- " Incremental search
- set incsearch
- " Highlight search terms
- set hlsearch
- " Ignore case when searching, but only if all letters are lowercase
- set smartcase
- set ignorecase
- " Wrap search when EOF is reached
- set wrapscan
- " Always replace all occurrences on a line, not just the first one
- set gdefault
- " ==============================
- " Insert mode completion
- " ==============================
- " Don't show possible completions that don't match the case of existing text
- set infercase
- " Don't show more than 10 items in the popup menu
- set pumheight=10
- " Where to look for possible completions
- set complete=.,w,u,b,kspell
- " How to show and insert possible completions
- set completeopt=menu,longest
- " ==============================
- " Status line
- " ==============================
- " Always display the status line
- set laststatus=2
- " Show the current editing status
- set showmode
- " Human readable file size
- fu! StatuslineFilesize()
- let filesize = line2byte(line("$") + 1) - 1
- if filesize < 0
- return ""
- elseif filesize < 1024
- return " | " . filesize . " Bytes"
- elseif filesize < 1024*1024
- return " | " . (filesize / 1024) . " kB"
- else
- return " | " . (filesize / (1024*1024)) . " MB"
- endif
- endf
- " Format string
- set statusline=%t\ \|\ %L\ lines%{StatuslineFilesize()}\ %y%m%=L%-6l\ C%-2c
- " ==============================
- " Template
- " ==============================
- function! LoadTemplate()
- silent! 0r ~/.vim/tmpl.%:e
- endfunction
- autocmd! BufNewFile * call LoadTemplate()
- " ==============================
- " Tabs
- " ==============================
- " Return the string used as the tabline
- fu! TabLine()
- let tabline = ''
- for i in range(tabpagenr('$'))
- let currtabnr = i + 1
- let currbuflst = tabpagebuflist(currtabnr)
- " Set the correct highlighting for the current tab
- let tabline .= ((currtabnr == tabpagenr()) ? '%#TabLineSel#' : '%#TabLine#')
- " Tab number (for mouse clicks)
- let tabline .= '%' . (currtabnr) . 'T'
- " Filename of the current window (no path)
- let filename = bufname(currbuflst[tabpagewinnr(currtabnr) - 1])
- if filename != ''
- let filename = fnamemodify(filename, ':t')
- else
- let filename = '[No Name]'
- endif
- " Check whether one of the buffers has been modified
- let bufmodified = ''
- for bufnr in range(len(currbuflst))
- if getbufvar(currbuflst[bufnr], "&mod") == 1
- let bufmodified = ' [+]'
- break
- endif
- endfor
- " Number of buffers
- let nbbuf = tabpagewinnr(currtabnr, '$')
- if nbbuf == 1
- let nbbuf = ''
- else
- let nbbuf = ':' . nbbuf
- endif
- " Name of the tab
- if currtabnr == tabpagenr()
- let tabline .= filename . nbbuf . bufmodified . '%#TabLine# '
- else
- let tabline .= filename . nbbuf . bufmodified . ' '
- endif
- endfor
- return tabline . '%#TabLineFill#%T'
- endf
- " Shift the current tab to the left (direction == 0) or to the right (direction != 0)
- fu! ShiftTab(direction)
- let tabpos = tabpagenr()
- if a:direction == 0
- if tabpos == 1
- exe 'tabm' . tabpagenr('$')
- else
- exe 'tabm' . (tabpos - 2)
- endif
- else
- if tabpos == tabpagenr('$')
- exe 'tabm ' . 0
- else
- exe 'tabm ' . tabpos
- endif
- endif
- return ''
- endf
- " Use a custom function to draw the tabline
- set tabline=%!TabLine()
- " Shift the current tab to the left/right
- inoremap <silent> <C-S-Left> <C-r>=ShiftTab(0)<CR>
- inoremap <silent> <C-S-Right> <C-r>=ShiftTab(1)<CR>
- noremap <silent> <C-S-Left> :call ShiftTab(0)<CR>
- noremap <silent> <C-S-Right> :call ShiftTab(1)<CR>
- " Ctrl-t creates a new tab and opens the file explorer
- map <silent> <C-t> :Texplore<CR>
- imap <silent> <C-t> <Esc><C-t>
- " ==============================
- " File explorer
- " ==============================
- " Prevent some files/directories from being listed
- let g:netrw_hide=1
- let g:netrw_list_hide='^\./$,^CVS/$,\.o$,\.pyc$,\.pyo$,\.swp$,^\.svn/$,^\.bzr/$,^\.sconsign.dblite$'
- " Reuse the same window when opening a file
- let g:netrw_browse_split=0
- " Always update directories' contents
- let g:netrw_fastbrowse=0
- " Tree view
- let g:netrw_liststyle=3
- " Highlight the current line
- let g:netrw_cursorline=1
- " ==============================
- " Some functions used later on
- " ==============================
- " See :help restore-cursor
- fu! RestoreCursorOnBufRead()
- " Don't restore the cursor position for SVN commit logs
- if line("'\"") > 0 && line("'\"") <= line("$") && bufname('%') != 'svn-commit.tmp'
- exe "normal! g`\""
- endif
- endf
- " See :help restore-position
- fu! SaveCurPos()
- execute "normal msHmtgg"
- endf
- " See :help restore-position
- fu! RestoreCurPos()
- execute "normal 'tzt`s"
- endf
- " Reformat the file
- fu! FormatFile()
- call SaveCurPos()
- execute "normal gg=G"
- call RestoreCurPos()
- endf
- " Remove trailing spaces
- fu! RemoveTrailingSpaces()
- call SaveCurPos()
- %s/\s\+$//e
- call RestoreCurPos()
- endf
- " Remove trailing line feed characters
- fu! Dos2Unix()
- call SaveCurPos()
- execute ":%s/\r$//g"
- call RestoreCurPos()
- endf
- " Comment the latest visual selection when applicable
- " If firstCol is 1, the comment is always put in the first column, no matter the indentation of the lines
- fu! Comment(firstCol)
- call SaveCurPos()
- if &filetype == "python" || &filetype == "zsh" || &filetype == "sh" || &filetype == "gnuplot" || &filetype == "make" || &filetype == "cfg"
- if a:firstCol == 1
- execute ":'<,'>s/^/# /"
- else
- execute ":'<,'>s/\\(^\\s*\\)/\\1\# /"
- endif
- elseif &filetype == "cpp" || &filetype == "c" || &filetype == "nesc" || &filetype == "java" || &filetype == "php"
- if a:firstCol == 1
- execute ":'<,'>s@^@// @"
- else
- execute ":'<,'>s@\\(^\\s*\\)@\\1// @"
- endif
- elseif &filetype == "tex" || &filetype == "bib"
- if a:firstCol == 1
- execute ":'<,'>s/^/% /"
- else
- execute ":'<,'>s/\\(^\\s*\\)/\\1% /"
- endif
- elseif &filetype == "css" || &filetype == "javascript"
- execute ":'<-1put='/*'"
- execute ":'>put='*/'"
- elseif &filetype == "xml" || &filetype == "html" || &filetype == "ant"
- execute ":'<-1put='<!--'"
- execute ":'>put='-->'"
- elseif &filetype == "vim"
- if a:firstCol == 1
- execute ":'<,'>s/^/\" /"
- else
- execute ":'<,'>s/\\(^\\s*\\)/\\1\" /"
- endif
- endif
- call RestoreCurPos()
- endf
- " Insert a tab or complete the current word
- fu! CleverTab(mode)
- if a:mode == 0 || strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
- return "\<tab>"
- elseif a:mode == 1
- return "\<c-p>"
- else
- return "\<c-n>"
- endif
- endf
- " Cycle between spell check off, on (English), and on (French)
- fu! CycleSpellChecking()
- if &spell == 0
- setlocal spelllang=en
- setlocal spell
- echo "Spell check on (English)"
- elseif &spelllang == "en"
- setlocal spelllang=fr
- echo "Spell check on (French)"
- else
- setlocal nospell
- echo "Spell check off"
- endif
- endf
- " Eat the next character, see :help abbreviations
- fu! EatChar(pat)
- let c = nr2char(getchar(0))
- return (c =~ a:pat) ? '' : c
- endf
- " ==============================
- " Commands
- " ==============================
- com! Dos2Unix :call Dos2Unix()
- com! Diff vert new | set bt=nofile | r # | 0d_ | diffthis | wincmd p | diffthis
- com! RcEdit :e ~/.vimrc
- com! RcReload :so ~/.vimrc
- com! RcTabEdit :tabnew ~/.vimrc
- " ==============================
- " Global mappings
- " ==============================
- " Move between screen lines instead of real lines
- noremap <Up> gk
- noremap <Down> gj
- noremap <Home> g<Home>
- noremap <End> g<End>
- inoremap <Up> <C-o>gk
- inoremap <Down> <C-o>gj
- inoremap <Home> <C-o>g<Home>
- inoremap <End> <C-o>g<End>
- " Alt + Right/Left moves between sentences
- noremap <A-Right> )
- noremap <A-Left> (
- inoremap <A-Right> <C-O>)
- inoremap <A-Left> <C-O>(
- " Prevent the cursor from moving when escaping from insert mode
- " The second mapping forces Vim to wait during the full time out before applying the first one
- " Otherwise, the first mapping prevents extended keycodes to work in a terminal
- inoremap <Esc> <Right><Esc>
- inoremap <Esc><Esc> <Esc>
- " Make Y yanks from the cursor to the end of the line
- map Y y$
- " Enter adds a line below the current one in command mode
- map <silent> <CR> o<ESC>
- " Let space and backspace act the same as in insert mode
- map <silent> <Space> i<Space><Esc>
- map <silent> <Backspace> X
- " In visual mode, enclose the selection with the given character
- vnoremap <silent> ' s''<Esc>P
- vnoremap <silent> " s""<Esc>P
- vnoremap <silent> ( s()<Esc>P
- vnoremap <silent> { s{}<Esc>P
- vnoremap <silent> [ s[]<Esc>P
- vnoremap <silent> * s**<Esc>P
- " <F2> cycles between all spell checking states
- map <silent> <F2> :call CycleSpellChecking()<CR>
- imap <silent> <F2> <C-o><F2>
- " <F3> reformats the file
- map <silent> <F3> :call FormatFile()<CR>
- imap <silent> <F3> <C-o><F3>
- " Ctrl-x clears highlight
- map <silent> <C-x> :noh<CR>
- imap <silent> <C-x> <C-o><C-x>
- " Ctrl-a selects everything
- map <C-a> ggVG
- " Completion and tabs
- inoremap <silent> <tab> <C-r>=CleverTab(2)<CR>
- inoremap <silent> <S-Tab> <C-r>=CleverTab(1)<CR>
- inoremap <silent> <C-Tab> <C-r>=CleverTab(0)<CR>
- " Comment the visual selection
- vnoremap <silent> / <Esc>:call Comment(1)<CR>
- " ==============================
- " Global abbreviations
- " ==============================
- iab to= TODO<C-O>V<Esc><C-R>=Comment(0)<CR><C-O>g_<Right>
- iab fix= FIXME<C-O>V<Esc><C-R>=Comment(0)<CR><C-O>g_<Right>
- " ==============================
- " File types
- " ==============================
- filetype on
- filetype indent on
- " Set the correct file type for some files
- au BufRead,BufNewFile,BufFilePost *.nc set filetype=nesc
- au BufRead,BufNewFile,BufFilePost *.plt set filetype=gnuplot
- au BufRead,BufNewFile,BufFilePost sconstruct set filetype=python
- au BufRead,BufNewFile,BufFilePost *.cls,*.tex set filetype=tex
- " Define specific options for some file types
- au FileType xml call ModeXML()
- au FileType css call ModeCSS()
- au FileType tex call ModeLaTeX()
- au FileType bib call ModeBibTeX()
- au FileType help call ModeHelp()
- au FileType python call ModePython()
- au FileType vim,changelog setlocal textwidth=0
- au FileType c,cpp,nesc,java call ModeJavaC()
- " ==============================
- " XML mode
- " ==============================
- fu! ModeXML()
- " Automatically close angle brackets
- inoremap <buffer> <lt> <><Left>
- endf
- " ==============================
- " CSS mode
- " ==============================
- fu! ModeCSS()
- " Automatically insert closing brackets
- inoremap <buffer> { {<CR>}<Up><CR>
- endf
- " ==============================
- " LaTeX mode
- " ==============================
- fu! ModeLaTeX()
- " Some abbreviations
- iab <buffer> fig= \begin{figure}<CR>\centering<CR><CR>\caption{}<CR>\label{fig:}<CR>\end{figure}<Up><Up><Up><C-R>=EatChar('\s')<CR>
- iab <buffer> fig*= \begin{figure*}<CR>\centering<CR><CR>\caption{}<CR>\label{fig:}<CR>\end{figure*}<Up><Up><Up><C-R>=EatChar('\s')<CR>
- iab <buffer> subfig= \subfloat[]{\label{subfig:}\includegraphics[width=]{}}\hfil<C-O>6<Left><C-R>=EatChar('\s')<CR>
- iab <buffer> tab= \begin{table}<CR>\centering<CR>\caption{}<CR>\label{tab:}<CR><CR>\end{table}<Up><C-R>=EatChar('\s')<CR>
- iab <buffer> item= \begin{itemize}<CR>\item<CR>\end{itemize}<Up><End>
- iab <buffer> enum= \begin{enumerate}<CR>\item<CR>\end{enumerate}<Up><End>
- iab <buffer> desc= \begin{description}<CR>\item[]<CR>\end{description}<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> sec= \section{}<CR>\label{sec:}<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> sub= \subsection{}<CR>\label{subsec:}<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> subsub= \subsubsection{}<CR>\label{subsubsec:}<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> graph= \includegraphics[]{}<Left><Left><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> center= \begin{center}<CR>\end{center}<Up><End><CR><C-R>=EatChar('\s')<CR>
- iab <buffer> cite= <BS>~\cite{}<Left><C-R>=EatChar('\s')<CR>
- iab <buffer> eq= \begin{equation}<CR><CR>\end{equation}<Up><C-R>=EatChar('\s')<CR>
- " Beamer document class
- iab <buffer> col= \begin{column}{}<CR>\end{column}<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> cols= \begin{columns}<CR>\end{columns}<Up><End><CR><C-R>=EatChar('\s')<CR>
- " In visual mode, enclose the selection with the given character
- vnoremap <buffer> <silent> $ s$$<Esc>P
- vnoremap <buffer> <silent> ' s`'<Esc>P
- vnoremap <buffer> <silent> " s``''<Esc><Left>P<Right>
- " F8 launches scons
- map <buffer> <silent> <F8> :up<CR>:!scons<CR>
- imap <buffer> <silent> <F8> <C-O>:up<CR><C-O>:!scons<CR>
- endf
- " ==============================
- " BibTeX mode
- " ==============================
- fu! ModeBibTeX()
- " Some abbreviations
- iab <buffer> str= @STRING{ = ""}<C-O>5<left><C-R>=EatChar('\s')<CR>
- iab <buffer> proc= @INPROCEEDINGS{,<CR>author = {},<CR>title = {},<CR>booktitle = ,<CR>year = ,<CR>month = ,<CR>}<C-O>6<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> art= @ARTICLE{,<CR>author = {},<CR>title = {},<CR>journal = ,<CR>volume = ,<CR>number = ,<CR>pages = {--},<CR>year = ,<CR>}<C-O>8<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> tech= @TECHREPORT{,<CR>author = {},<CR>title = {},<CR>year = ,<CR>institution = ,<CR>}<C-O>5<Up><End><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> phd= @PHDTHESIS{,<CR>author = {},<CR>title = {},<CR>year = ,<CR>school = ,<CR>}<C-O>5<Up><End><Left><C-R>=EatChar('\s')<CR>
- endf
- " ==============================
- " Help mode
- " ==============================
- func! ModeHelp()
- " <CR> and <backspace> to navigate
- map <buffer> <CR> <C-]>
- map <buffer> <Backspace> <C-O>
- endfunc
- " ==============================
- " C/C++/Java mode
- " ==============================
- fu! ModeJavaC()
- " Better indentation function
- setlocal cindent
- " Don't automatically open new comment lines
- setlocal formatoptions-=r
- " Don't put preprocessor directives at the start of the line
- setlocal cinkeys-=0#
- " Make sure to automatically format pasted text
- noremap <buffer> p p=`]`]
- noremap <buffer> P P=`]`]
- " Automatically insert closing brackets
- inoremap <buffer> { {<CR>}<Up><CR>
- " Some abbreviations
- iab <buffer> com= /**<CR><Home><Space>*<CR><Home>**/<Up>
- iab <buffer> def= #ifndef<CR>#define <CR><CR>#endif<C-O>gg<End>
- endf
- " ==============================
- " Python mode
- " ==============================
- fu! ModePython()
- " Some abbreviations
- iab <buffer> hb= #!/usr/bin/env python<CR><CR><C-R>=EatChar('\s')<CR>
- iab <buffer> def= def ():<CR>""" """<CR>pass<Up><Up><Left><Left><Left><C-R>=EatChar('\s')<CR>
- iab <buffer> cls= class :<CR><CR>def __init__(self):<CR>""" Constructor """<CR>pass<Up><Up><Up><Up><Left><C-R>=EatChar('\s')<CR>
- endf
- " ==============================
- " Miscellaneous
- " ==============================
- " Remove trailing spaces when saving buffers
- au BufWrite * if ! &bin | call RemoveTrailingSpaces() | endif
- " Restore cursor position when reading a buffer
- au BufReadPost * call RestoreCursorOnBufRead()
- " Always change to the directory of the current file
- au BufEnter * lcd %:p:h
- " :TOhtml configuration
- let use_xhtml = 1
- let html_use_css = 1
- let html_ignore_folding = 1
Add Comment
Please, Sign In to add comment