Skip to content

Instantly share code, notes, and snippets.

@planbnet
Last active September 28, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save planbnet/1422486 to your computer and use it in GitHub Desktop.
Save planbnet/1422486 to your computer and use it in GitHub Desktop.
My .vimrc
scriptencoding utf-8
set encoding=utf-8
set nocompatible
""""""""""""""" SETUP VUNDLER """""""""""""""""""""""
if (filereadable($HOME."/.vim/bundle/vundle/autoload/vundle.vim"))
"(only if it's correctly installed)
filetype off
set rtp+=~/.vim/bundle/vundle/
:silent! call vundle#rc()
Bundle 'gmarik/vundle'
"lots of color schemes
Bundle 'flazz/vim-colorschemes.git'
"Required util library
Bundle 'L9'
"Displays a file system tree (currently mapped to F6)
Bundle "The-NERD-tree"
"Auto alignment script
"usage: select text and type ":Align =<Enter>"
"to align along the = character
Bundle "Align"
"Alternate file plugin with objc support
"use :A to switch between header and implementation file
Bundle "nacitar/a.vim"
"Use ctrl-a/ctrl-x to swap values like "true" -> "false"
Bundle "mjbrownie/swapit"
"Automatic html/xml tag closing with ctrl-<space>
Bundle "closetag.vim"
"Handle surroundings
"""viwS<div> to surround current word with a div
"""ds' delete surrounding ' (my shortcut: -')
"""cs([ change ( to [
"""ysiw' insert ' (my shortcut: +')
Bundle "tpope/vim-surround.git"
Bundle "tpope/vim-repeat.git"
"git integration
Bundle "tpope/vim-fugitive"
"Improves the % key (find matching paranthesis) to handle html tags.
Bundle 'tsaleh/vim-matchit.git'
"File finder (CTRL-P)
Bundle "kien/ctrlp.vim.git"
" Snippets (use tab to expand)
Bundle "msanders/snipmate.vim"
"syntax checking
Bundle 'scrooloose/syntastic'
"undo tree
Bundle "sjl/gundo.vim.git"
"""""""""""""""""""""""""""""""""""""""Language specific plugins
"Powerful html coding plugin
"ex.: select multiple lines and do ctrl-z ul>li*
"ex.: type div>p#foo$*3>a and hit CTRL-Z
Bundle "mattn/zencoding-vim"
"markdown support and syntax highlighting
Bundle "plasticboy/vim-markdown.git"
"HTML5 and CSS3 syntax and completion
Bundle "othree/html5.vim"
Bundle "css3"
endif
"""""""""""""""""" SET VARIABLES """""""""""""""""""""""""""""""""
let g:is_posix = 1
let mapleader = ","
let maplocalleader = ","
let g:user_zen_expandabbr_key = '<c-z>'
"zen leader key is c-y by default, use c-y n to jump to next insert point
let g:use_zen_complete_tag = 1
let g:xml_syntax_folding = 1
let g:closetag_html_style = 1
let g:indent_guides_guide_size = 1
" Stop Syntastic checking angular html templates"
let g:syntastic_html_checkers=[]
set history=256 " Number of things to remember in history.
set timeoutlen=500 " Time to wait after ESC (default causes an annoying delay)
set modeline
set modelines=5 " default numbers of lines to read for modeline instructions
set autowrite " Writes on make/shell commands
set autoread
set backup
set writebackup
set directory=~/.vim/tmp,~/tmp,/var/tmp,/tmp
set backupdir=~/.vim/tmp,~/tmp,/var/tmp,/tmp
:if has('persistent_undo')
set undodir=~/.vim/tmp,~/tmp,/var/tmp,/tmp
set undofile " Enable undo after close
endif
" Remember last position in files
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
set hidden " The current buffer can be put to the background without writing to disk
set hlsearch " highlight search
set ignorecase
set smartcase " be case sensitive when input has a capital letter
set incsearch " show matches while typing
set magic " sensible regexp special char handling
set nowrap
set textwidth=0 " Don't wrap lines by default
if exists('+colorcolumn')
set colorcolumn=+1 " if cc is supported and hard wrap is on, show a colored column
endif
set tabstop=2 " tab size eql 2 spaces
set softtabstop=2
set shiftwidth=2 " default shift width for indents
set expandtab " replace tabs with ${tabstop} spaces
set smarttab
set backspace=indent
set backspace+=eol
set backspace+=start
set autoindent
set cindent
set indentkeys-=0# " do not break indent on #
set cinkeys-=0#
set cinoptions=:s,ps,ts,cs
set cinwords=if,else,while,do
set cinwords+=for,switch,case
set number " line numbers
set showmatch " Show matching brackets.
set matchtime=2 " Bracket blinking.
set wildmode=longest,list " At command line, complete longest common string, then list alternatives.
set novisualbell " No blinking
set noerrorbells " No noise.
set vb t_vb= " disable any beeps or flashes on error
set laststatus=2 " always show status line.
set shortmess=atI " shortens messages
set showcmd " display an incomplete command in statusline
set statusline=%<%f\
set stl+=[%{&ff}]
set stl+=[%{&fenc==\"\"?&enc:&fenc}]
set stl+=%{((exists(\"+bomb\")\ &&\ &bomb)?\",B\":\"\")}
set stl+=%y%m%r%=
set stl+=%-14.(%l,%c%V%)\ %P
set foldenable " Turn on folding
set foldmethod=syntax
set foldopen=block,hor,tag " what movements open folds
set foldopen+=percent,mark
set foldopen+=quickfix
set foldlevel=100 "Don't autofold
set splitright
set nolist "do not display unprintable characters (F4 toggles)
set listchars=tab:▸\ ,eol:¬,trail:·,extends:»,precedes:«
:silent! set completeopt=longest,menu,preview "
set scrolloff=4 "Start scrolling when we're 4 lines away from margins
set sidescrolloff=15
set sidescroll=1
"config for Ctrl-p extension"
set wildignore+=*eclipse*,*/tmp/*,*/target/*,*/node_modules/*,*.jar,*.zip,*.so,*.swp,*.class
""""""""""""""""""" SPECIFIC FILE HANDLING """"""""""""""""""""
"Handle scss files like css files
au BufRead,BufNewFile *.scss set filetype=css
au BufRead,BufNewFile *.less set filetype=css
"auto find git base path
autocmd BufNewFile,BufRead let git_base_path = substitute(system("git rev-parse --show-toplevel"), '\n', '', '')
""""""""""""" CUSTOM COMMANDS """""""""""""""""""""
"enable writing as root
command W execute 'w !sudo tee % >/dev/null' | e! %
"force quit command
command Q qa!
"command for replacing stupid windows endings
command UnixEndings set ff=unix
"use ruby to inflate/deflate current selection
command -range Deflate :'<,'>!ruby -rzlib -e "puts Zlib::Deflate.deflate(ARGF.read)"
command -range Inflate :'<,'>!ruby -rzlib -e "puts Zlib::Inflate.inflate(ARGF.read)"
"Converts a httpd group to a svn auth group
command SVNGroup norm!^Wdt i = <CR><ESC>:call setline('.',join(sort(split(substitute(getline('.'), ',', '', 'g'))), ', '))<CR>kJ
""""""""""" CUSTOM KEY MAPPINGS """"""""""""""""""
"use leader-f for formatiing
nnoremap <leader>f gg=G
vnoremap <leader>f =
"special formatting for different file types
autocmd filetype xml map <leader>f gg!Gxmllint --format -<CR>
autocmd filetype html map <leader>f gg!Gtidy -q -i 2>/dev/null<CR>
autocmd filetype c map <leader>f gg!Gastyle -c -s2<CR>
autocmd filetype cpp map <leader>f gg!Gastyle -c -s2<CR>
autocmd filetype java map <leader>f gg!Gastyle --mode java -c -s2<CR>
autocmd filetype text map <leader>f gggqG
autocmd filetype markdown map <leader>f gggqG
autocmd filetype javascript set omnifunc=javascriptcomplete#CompleteJS
"indent 2 for html
autocmd FileType html setlocal ts=2 sts=2 sw=2
"use ,j to join the whole paragraph to one line
nnoremap <leader>j vipJ0
"use ,w to (re)wrap the current paragraph
nnoremap <leader>w gqip
nmap + ysiW
nmap - ds
"switch to hex view and back
nnoremap <leader>x :%!xxd<CR>
nnoremap <leader>X :%!xxd -r<CR>
vnoremap <leader>x :!xxd<CR>
vnoremap <leader>X :!xxd -r<CR>
"spell checker
map <leader>s :w!<cr>:!aspell -c %<cr>:e! %<cr>
"use ,, to cycle between last and current buffer
nnoremap <leader><leader> <c-^>
"Select last pasted text
nnoremap <leader>v V`]
"Use tab to switch between windows
nnoremap <TAB> <C-w>w
"split lines from normal mode
nnoremap <C-J> a<CR><Esc>k$
"Insert newline from command mode by using Enter
nnoremap <silent> <CR> o<Esc>d$
"shortcuts to align selection using the align plugin
vnoremap <silent> <leader><bar> :Align <bar><CR>
vnoremap <silent> <leader>, :Align ,<CR>
vnoremap <silent> <leader>- :Align -<CR>
vnoremap <silent> <leader>= :Align =<CR>
vnoremap <silent> <leader>" :Align "<CR>
"use ctrl-space to trigger intelligent (omnifunc) auto-completion
"where possible and word completion in other cases
inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-@> <C-Space>
"fix home/end keys (in some terminals)
map OH <Home>
map OF <End>
imap OH <Home>
imap OF <End>
cmap OH <Home>
cmap OF <End>
" Allow Ctrl-S for saving
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-C>:update<CR>
"...and make them smart:
"first press goes to first/last visible char, second one to beginning/end of line
noremap <expr> <Home> (col('.') == matchend(getline('.'), '^\s*')+1 ? '0' : '^')
noremap <expr> <End> (col('.') == match(getline('.'), '\s*$') ? '$' : 'g_')
vnoremap <expr> <Home> (col('.') == matchend(getline('.'), '^\s*')+1 ? '0' : '^')
vnoremap <expr> <End> (col('.') == match(getline('.'), '\s*$') ? '$h' : 'g_')
imap <Home> <C-o><Home>
imap <End> <C-o><End>
"leave insert mode when pressing jk
imap jk <Esc>
"deleting a line in insert mode
imap <C-d> <Esc>d^xi
""""""""""""""" FUNCTION KEYS """""""""""""""""""""""""""
"toggle paste mode
set pastetoggle=<f2>
nnoremap <silent> <f2> :set invpaste paste?<cr>
inoremap <silent> <f2> <C-\><C-O>:set invpaste paste?<cr>
"toggle line number display
nnoremap <silent> <f3> :set invnumber<cr>
inoremap <silent> <f3> <C-\><C-O>:set invnumber<cr>
"toggle display of invisible characters
nnoremap <silent> <f4> :set invlist<cr>
inoremap <silent> <f4> <C-\><C-O>:set invlist<cr>
"toggle word wrapping
nnoremap <silent> <f5> :set invwrap wrap?<cr>
nnoremap <c-f5> :call ToggleTextWidth()<cr>
"in insert mode, toggle auto hard-wrap
inoremap <silent> <f5> <esc> :call ToggleTextWidth()<cr>i
inoremap <silent> <c-f5> <C-\><C-O> :call ToggleTextWidth()<cr>
"toggle NERD tree tab
nnoremap <silent> <f6> :NERDTreeToggle<cr>
"in insert mode, insert current file name
inoremap <silent> <f6> <C-R>=expand("%:p")<cr>
"toggle undo tree on ctrl F6
nnoremap <silent> <C-f6> :GundoToggle<cr>
inoremap <silent> <C-f6> <C-\><C-O> :GundoToggle<cr>
"previous buffer
nnoremap <silent> <f7> :bprev<cr>
"previous paragraph in insert mode
inoremap <silent> <f7> <esc>{i
"ctrl p plugin: show all open buffers
nnoremap <silent> <c-f7> :CtrlPBuffer<cr>
"next buffer
nnoremap <silent> <f8> :bnext<cr>
"next paragraph in insert mode
inoremap <silent> <f8> <esc>}i
"ctrl p plugin: show recently used files
nnoremap <silent> <c-f8> :CtrlPMRUFiles<cr>
"close current buffer without closing tab
nnoremap <silent> <f9> :bprevious<CR>:bdelete #<CR>
inoremap <silent> <f9> <C-\><C-O>:bprevious<CR>:bdelete #<CR>
nnoremap <silent> <C-f9> :q!<CR>
inoremap <silent> <C-f9> <C-\><C-O>:q!<CR>
"hard shutdown (closes all buffers even if unsaved)
nnoremap <silent> <f10> :qa!<cr>
inoremap <silent> <f10> <C-\><C-O>:qa!<cr>
"toggle automatic tab expansion to spaces
nnoremap <silent> <f12> :set invexpandtab expandtab?<cr>
inoremap <silent> <f12> <C-\><C-O>:set invexpandtab expandtab?<cr>
""""""""""""""""" SYNTAX HIGHLIGHTING AND COLORS """"""""""""""""
filetype plugin indent on " Automatically detect file types.
syntax enable
set t_Co=256
"try different colorschemes (inverse order of preference)
set background=dark
:silent! colorscheme slate
:silent! colorscheme zenburn
hi Normal ctermbg=NONE
""""""""""""""""""""""""" GUI STUFF """""""""""""""""""""""
if has('gui_running')
" set guioptions=cMg " console dialogs, do not show menu and toolbar
if has('mac')
set guifont=Andale\ Mono:h13
set fuoptions=maxvert,maxhorz ",background:#00AAaaaa
else
set guifont=Consolas:h11
endif
"set background=light
":silent! colorscheme solarized
vnoremap <C-Del> "+x
vnoremap <C-Insert> "+y
map <S-Insert> "+gP
cmap <S-Insert> <C-R>+
imap <S-Insert> <C-R>+
nnoremap <silent> <F11> :call ToggleWindowSize(0)<CR>
imap <silent> <F11> <C-O><F11>
endif
"""""""""""""" FUNCTIONS """"""""""""""""""""""
command! -nargs=* Make write | let g:make_args="<args>" | make <args> | cwindow 6
function! Make2()
if !exists("g:make_args")
let g:make_args = ""
endif
wall
exec "make " . g:make_args
cwindow 6
redraw
endfunction
inoremap <C-B> <ESC>:call Make2()<CR><C-L>
noremap <C-B> :call Make2()<CR><C-L>
function ToggleTextWidth()
if &tw
let s:save_tw = &tw
set tw=0
echo "Hard wrap off"
else
if exists("s:save_tw")
let &tw = s:save_tw
unlet s:save_tw
else
" default value if 'tw' is zero the first time
set tw=73
endif
echo "Hard wrap on"
endif
endfunction
function ToggleWindowSize(act)
if a:act < 0 || a:act > 2 | return | endif
let posX = getwinposx()
let posY = getwinposy()
let actTab = "XXX__X_XR__XX_X__RRRR__R"
let idx = ((exists("g:twsWM") + exists("g:twsHM") * 2) * 3 + a:act) * 2
let actW = strpart(actTab, idx, 1)
let actH = strpart(actTab, idx + 1, 1)
" note. g:tws + [Width,Height,X,Y] + [Maximized,Saved]
if actW == "X"
let g:twsWS = &columns | let g:twsXS = posX
set columns=999
let posX = getwinposx()
let g:twsWM = &columns | let g:twsXM = posX
elseif actW == "R"
if g:twsWM == &columns
let &columns = g:twsWS
if g:twsXM == posX | let posX = g:twsXS | endif
endif
unlet g:twsWM g:twsWS g:twsXM g:twsXS
endif
if actH == "X"
let g:twsHS = &lines | let g:twsYS = posY
set lines=999
let posY = getwinposy()
let g:twsHM = &lines | let g:twsYM = posY
elseif actH == "R"
if g:twsHM == &lines
let &lines = g:twsHS
if g:twsYM == posY | let posY = g:twsYS | endif
endif
unlet g:twsHM g:twsHS g:twsYM g:twsYS
endif
execute "winpos " . posX . " " . posY
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment