Skip to content

Instantly share code, notes, and snippets.

@bling
Created May 16, 2016 14:23
Show Gist options
  • Save bling/7010915bae135fe18871f7f2129230da to your computer and use it in GitHub Desktop.
Save bling/7010915bae135fe18871f7f2129230da to your computer and use it in GitHub Desktop.
" vim: fdm=marker ts=2 sts=2 sw=2 fdl=0
" detect OS {{{
let s:is_windows = has('win32') || has('win64')
let s:is_cygwin = has('win32unix')
let s:is_macvim = has('gui_macvim')
"}}}
" dotvim settings {{{
let s:cache_dir = '~/.vim/.cache'
" initialize default settings
let s:settings = {}
let s:settings.default_indent = 2
let s:settings.max_column = 120
let s:settings.enable_cursorcolumn = 0
"}}}
" functions {{{
function! s:get_cache_dir(suffix) "{{{
return resolve(expand(s:cache_dir . '/' . a:suffix))
endfunction "}}}
function! Source(begin, end) "{{{
let lines = getline(a:begin, a:end)
for line in lines
execute line
endfor
endfunction "}}}
function! Preserve(command) "{{{
" preparation: save last search, and cursor position.
let _s=@/
let l = line(".")
let c = col(".")
" do the business:
execute a:command
" clean up: restore previous search history, and cursor position
let @/=_s
call cursor(l, c)
endfunction "}}}
function! StripTrailingWhitespace() "{{{
call Preserve("%s/\\s\\+$//e")
endfunction "}}}
function! EnsureExists(path) "{{{
if !isdirectory(expand(a:path))
call mkdir(expand(a:path))
endif
endfunction "}}}
function! CloseWindowOrKillBuffer() "{{{
let number_of_windows_to_this_buffer = len(filter(range(1, winnr('$')), "winbufnr(v:val) == bufnr('%')"))
" never bdelete a nerd tree
if matchstr(expand("%"), 'NERD') == 'NERD'
wincmd c
return
endif
if number_of_windows_to_this_buffer > 1
wincmd c
else
bdelete
endif
endfunction "}}}
"}}}
" base configuration {{{
set timeoutlen=300 "mapping timeout
set ttimeoutlen=50 "keycode timeout
set mouse=a "enable mouse
set mousehide "hide when characters are typed
set history=1000 "number of command lines to remember
set ttyfast "assume fast terminal connection
set viewoptions=folds,options,cursor,unix,slash "unix/windows compatibility
set encoding=utf-8 "set encoding for text
if exists('$TMUX')
set clipboard=
else
set clipboard=unnamed "sync with OS clipboard
endif
set hidden "allow buffer switching without saving
set autoread "auto reload if file saved externally
set fileformats+=mac "add mac to auto-detection of file format line endings
set nrformats-=octal "always assume decimal numbers
set showcmd
set tags=tags;/
set showfulltag
set modeline
set modelines=5
if s:is_windows && !s:is_cygwin
" ensure correct shell in gvim
set shell=c:\windows\system32\cmd.exe
endif
if $SHELL =~ '/fish$'
" VIM expects to be run from a POSIX shell.
set shell=sh
endif
set noshelltemp "use pipes
" whitespace
set backspace=indent,eol,start "allow backspacing everything in insert mode
set autoindent "automatically indent to match adjacent lines
set expandtab "spaces instead of tabs
set smarttab "use shiftwidth to enter tabs
let &tabstop=s:settings.default_indent "number of spaces per tab for display
let &softtabstop=s:settings.default_indent "number of spaces per tab in insert mode
let &shiftwidth=s:settings.default_indent "number of spaces when indenting
set list "highlight whitespace
set listchars=tab:│\ ,trail:•,extends:❯,precedes:❮
set shiftround
set linebreak
let &showbreak='↪ '
set scrolloff=1 "always show content after scroll
set scrolljump=5 "minimum number of lines to scroll
set display+=lastline
set wildmenu "show list for autocomplete
set wildmode=list:full
set wildignorecase
set splitbelow
set splitright
" disable sounds
set noerrorbells
set novisualbell
set t_vb=
" searching
set hlsearch "highlight searches
set incsearch "incremental searching
set ignorecase "ignore case for searching
set smartcase "do case-sensitive if there's a capital letter
if executable('ack')
set grepprg=ack\ --nogroup\ --column\ --smart-case\ --nocolor\ --follow\ $*
set grepformat=%f:%l:%c:%m
endif
if executable('ag')
set grepprg=ag\ --nogroup\ --column\ --smart-case\ --nocolor\ --follow
set grepformat=%f:%l:%c:%m
endif
" vim file/folder management {{{
" persistent undo
if exists('+undofile')
set undofile
let &undodir = s:get_cache_dir('undo')
endif
" backups
set backup
let &backupdir = s:get_cache_dir('backup')
" swap files
let &directory = s:get_cache_dir('swap')
set noswapfile
call EnsureExists(s:cache_dir)
call EnsureExists(&undodir)
call EnsureExists(&backupdir)
call EnsureExists(&directory)
"}}}
let mapleader = ","
let g:mapleader = ","
"}}}
" ui configuration {{{
set showmatch "automatically highlight matching braces/brackets/etc.
set matchtime=2 "tens of a second to show matching parentheses
set number
set lazyredraw
set laststatus=2
set noshowmode
set foldenable "enable folds by default
set foldmethod=syntax "fold via syntax of files
set foldlevelstart=99 "open all folds by default
let g:xml_syntax_folding=1 "enable xml folding
set cursorline
autocmd WinLeave * setlocal nocursorline
autocmd WinEnter * setlocal cursorline
let &colorcolumn=s:settings.max_column
if s:settings.enable_cursorcolumn
set cursorcolumn
autocmd WinLeave * setlocal nocursorcolumn
autocmd WinEnter * setlocal cursorcolumn
endif
if has('conceal')
set conceallevel=1
set listchars+=conceal:Δ
endif
if has('gui_running')
" open maximized
set lines=999 columns=9999
if s:is_windows
autocmd GUIEnter * simalt ~x
endif
set guioptions+=t "tear off menu items
set guioptions-=T "toolbar icons
if s:is_macvim
set gfn=Ubuntu_Mono:h14
set transparency=2
endif
if s:is_windows
set gfn=Ubuntu_Mono:h10
endif
if has('gui_gtk')
set gfn=Ubuntu\ Mono\ 11
endif
else
if $COLORTERM == 'gnome-terminal'
set t_Co=256 "why you no tell me correct colors?!?!
endif
if $TERM_PROGRAM == 'iTerm.app'
" different cursors for insert vs normal mode
if exists('$TMUX')
let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
else
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
endif
endif
endif
"}}}
" mappings {{{
" formatting shortcuts
nmap <leader>fef :call Preserve("normal gg=G")<CR>
nmap <leader>f$ :call StripTrailingWhitespace()<CR>
vmap <leader>s :sort<cr>
" eval vimscript by line or visual selection
nmap <silent> <leader>e :call Source(line('.'), line('.'))<CR>
vmap <silent> <leader>e :call Source(line('v'), line('.'))<CR>
nnoremap <leader>w :w<cr>
" toggle paste
map <F6> :set invpaste<CR>:set paste?<CR>
" remap arrow keys
nnoremap <left> :bprev<CR>
nnoremap <right> :bnext<CR>
nnoremap <up> :tabnext<CR>
nnoremap <down> :tabprev<CR>
" smash escape
inoremap jk <esc>
inoremap kj <esc>
" change cursor position in insert mode
inoremap <C-h> <left>
inoremap <C-l> <right>
inoremap <C-u> <C-g>u<C-u>
if mapcheck('<space>/') == ''
nnoremap <space>/ :vimgrep //gj **/*<left><left><left><left><left><left><left><left>
endif
" sane regex {{{
nnoremap / /\v
vnoremap / /\v
nnoremap ? ?\v
vnoremap ? ?\v
nnoremap :s/ :s/\v
" }}}
" command-line window {{{
nnoremap q: q:i
nnoremap q/ q/i
nnoremap q? q?i
" }}}
" folds {{{
nnoremap zr zr:echo &foldlevel<cr>
nnoremap zm zm:echo &foldlevel<cr>
nnoremap zR zR:echo &foldlevel<cr>
nnoremap zM zM:echo &foldlevel<cr>
" }}}
" screen line scroll
nnoremap <silent> j gj
nnoremap <silent> k gk
" auto center {{{
nnoremap <silent> n nzz
nnoremap <silent> N Nzz
nnoremap <silent> * *zz
nnoremap <silent> # #zz
nnoremap <silent> g* g*zz
nnoremap <silent> g# g#zz
nnoremap <silent> <C-o> <C-o>zz
nnoremap <silent> <C-i> <C-i>zz
"}}}
" reselect visual block after indent
vnoremap < <gv
vnoremap > >gv
" reselect last paste
nnoremap <expr> gp '`[' . strpart(getregtype(), 0, 1) . '`]'
" find current word in quickfix
nnoremap <leader>fw :execute "vimgrep ".expand("<cword>")." %"<cr>:copen<cr>
" find last search in quickfix
nnoremap <leader>ff :execute 'vimgrep /'.@/.'/g %'<cr>:copen<cr>
" shortcuts for windows {{{
nnoremap <leader>v <C-w>v<C-w>l
nnoremap <leader>s <C-w>s
nnoremap <leader>vsa :vert sba<cr>
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
"}}}
" tab shortcuts
map <leader>tn :tabnew<CR>
map <leader>tc :tabclose<CR>
" make Y consistent with C and D. See :help Y.
nnoremap Y y$
" hide annoying quit message
nnoremap <C-c> <C-c>:echo<cr>
" window killer
nnoremap <silent> Q :call CloseWindowOrKillBuffer()<cr>
" quick buffer open
nnoremap gb :ls<cr>:e #
" general
nmap <leader>l :set list! list?<cr>
nnoremap <BS> :set hlsearch! hlsearch?<cr>
map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
" helpers for profiling {{{
nnoremap <silent> <leader>DD :exe ":profile start profile.log"<cr>:exe ":profile func *"<cr>:exe ":profile file *"<cr>
nnoremap <silent> <leader>DP :exe ":profile pause"<cr>
nnoremap <silent> <leader>DC :exe ":profile continue"<cr>
nnoremap <silent> <leader>DQ :exe ":profile pause"<cr>:noautocmd qall!<cr>
"}}}
"}}}
" commands {{{
command! -bang Q q<bang>
command! -bang QA qa<bang>
command! -bang Qa qa<bang>
"}}}
" autocmd {{{
" go back to previous position of cursor if any
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe 'normal! g`"zvzz' |
\ endif
autocmd FileType js,scss,css autocmd BufWritePre <buffer> call StripTrailingWhitespace()
autocmd FileType css,scss setlocal foldmethod=marker foldmarker={,}
autocmd FileType css,scss nnoremap <silent> <leader>S vi{:sort<CR>
autocmd FileType python setlocal foldmethod=indent
autocmd FileType markdown setlocal nolist
autocmd FileType vim setlocal fdm=indent keywordprg=:help
"}}}
" finish loading {{{
filetype plugin indent on
syntax enable
"}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment