" ============================================================================ " " vimrc " " ============================================================================ " " basic {{{ " ------------------------------------------------------------------------------ " encoding {{{ " `set encoding=utf8` *must* be before `scriptencoding utf8` " because `scriptencoding utf-8` converts source string " from its argument encoding to current `&encoding`. set encoding=utf-8 scriptencoding utf-8 set fileencodings=ucs-bom,iso-2022-jp,euc-jp,utf-8,cp932,utf-16le,utf-16 " }}} " clear command augroup my autocmd! augroup end " user interface in English language messages C language time C let g:did_install_default_menus = 1 function! s:load_local_vimrc(...) let suffix = (a:0 > 0) ? ('.' . a:1) : '' let vimrc = expand('~/.vimrc.local' . suffix) if filereadable(vimrc) try execute 'source' vimrc catch " TODO do not ignore errors endtry endif endfunction " load local config call s:load_local_vimrc('prepare') " environment {{{ let s:in_win = has('win32') || has('win64') let s:in_mac = has('mac') || has('macunix') let s:in_nix = !s:in_mac && has('unix') " }}} " map leader {{{ let mapleader = ',' let maplocalleader = '\' " }}} syntax enable " runtimepath {{{ filetype off " bundle {{{ execute 'source' expand('~/.vim/bundles.vim') " }}} " after bundling, enable filetype filetype plugin on filetype indent on " /runtimepath }}} " color {{{ " auto loading after/colors {{{ function! s:load_after_colors() let color = expand('~/.vim/after/colors/' . g:colors_name . '.vim') if filereadable(color) execute 'source' color endif endfunction autocmd my ColorScheme * call s:load_after_colors() " }}} autocmd my ColorScheme * highlight link FullWidthSpace SpellBad autocmd my Syntax * syntax match FullWidthSpace containedin=ALL / / set background=dark colorscheme scheakur " }}} " /basic }}} " option {{{ " ------------------------------------------------------------------------------ " search {{{ set ignorecase set smartcase set incsearch set hlsearch set wrapscan " }}} " characters {{{ set fileformats=unix,dos,mac set ambiwidth=double " }}} " indent {{{ set smarttab set expandtab set tabstop=4 set shiftwidth=4 set softtabstop& set shiftround " for Vim script. see help: ft-vim-indent let g:vim_indent_cont=0 " }}} " matching parenthesis {{{ set showmatch set matchtime=2 set cpoptions-=m set matchpairs+=<:> " }}} " fold {{{ set foldenable set foldmethod=marker " vert:\| is default set fillchars=fold:\ ,vert:\| " for xml folding let g:xml_syntax_folding = 1 " }}} " backup & swap & undo {{{ set nowritebackup set nobackup set directory-=. let &directory = $HOME . '/tmp/vim,' . &directory set undofile let &undodir = $HOME . '/tmp/vim/undo,' . &directory " make tmp directory if !isdirectory($HOME . '/tmp/vim/undo') call mkdir($HOME . '/tmp/vim/undo', 'p') endif set history=1024 set viminfo='128,<512,s64,h " }}} " invisible characters {{{ set list set listchars=tab:»_,trail:・ " highlight column 81 set colorcolumn=81 set textwidth=0 " }}} " footer (statusline, cmdheight) {{{ set cmdheight=2 set laststatus=2 autocmd my BufLeave,WinLeave * call s:set_statusline_nc() autocmd my BufEnter,WinEnter * call s:set_statusline() function! s:set_statusline_nc() let &l:statusline = s:make_statusline(3, 4) endfunction function! s:set_statusline() let &l:statusline = s:make_statusline(1, 2) endfunction function! s:make_statusline(hi1, hi2) let st = join([ \ '%' . a:hi2 . '* %{&ft} ', \ '%' . a:hi1 . '* %h%w%m%r ', \ '%0* %<%f ', \ '%=', \ '%0* %{(&fenc != "") ? &fenc : &enc} ', \ '%' . a:hi1 . '* %{&ff} ', \ '%' . a:hi2 . '* %lL %2vC %3p%%', \], '') return st endfunction " }}} " misc {{{ set hidden set backspace=indent,eol,start set clipboard=unnamed if has('unnamedplus') set clipboard+=unnamedplus endif set modeline " Do not increase/decrease as octal number or hexadecimal number set nrformats& nrformats-=octal,hex set virtualedit=all set formatoptions=tcroqnlM1 " show the number of lines of selection set showcmd set updatetime=1000 set maxfuncdepth=256 " show preview window at the bottom set splitbelow " }}} " tabpages {{{ set showtabline=2 set tabline=%!MyTabLine() function! MyTabLine() let titles = map(range(1, tabpagenr('$')), 's:tabpage_label(v:val)') let tabpages = join(titles, '') . ' ' . '%#TabLineFill#%T' let info = fnamemodify(getcwd(), ":~") . ' ' return tabpages . '%=' . info endfunction function! s:tabpage_label(n) let title = s:tabpage_title(a:n) let bufnrs = tabpagebuflist(a:n) let mods = filter(copy(bufnrs), 'getbufvar(v:val, "&modified")') let mod = len(mods) ? '*' : '' let label = ' ' . title . mod . ' ' let hi = a:n is tabpagenr() ? '%#TabLineSel#' : '%#TabLine#' return '%' . a:n . 'T' . hi . label . '%T%#TabLineFill#' endfunction function! s:tabpage_title(n) let bufnrs = tabpagebuflist(a:n) let title = gettabvar(a:n, '__title__') if !len(title) let curbufnr = bufnrs[tabpagewinnr(a:n) - 1] let title = fnamemodify(bufname(curbufnr), ':t') let title = len(title) ? title : '[No Name]' endif return title endfunction function! s:set_tabpage_title(title) if !empty(a:title) let t:__title__ = a:title else let n = tabpagenr() let title = input("Tab's title : ", s:tabpage_title(n)) if !empty(title) let t:__title__ = title endif endif redraw! endfunction command! -nargs=? SetTabTitle call s:set_tabpage_title() " }}} " completion {{{ set wildignore& wildignore+=.git,.svn,*.class set nowildmenu set wildmode=list:longest,full " }}} " /option }}} " command {{{ " ------------------------------------------------------------------------------ " file encoding & line feed code {{{ command! -bang -bar -complete=file -nargs=? Utf8 \ edit ++enc=utf-8 command! -bang -bar -complete=file -nargs=? Iso2022jp \ edit ++enc=iso-2022-jp command! -bang -bar -complete=file -nargs=? Cp932 \ edit ++enc=cp932 command! -bang -bar -complete=file -nargs=? Euc \ edit ++enc=euc-jp command! -bang -bar -complete=file -nargs=? Utf16 \ edit ++enc=utf-16le command! -bang -bar -complete=file -nargs=? Utf16be \ edit ++enc=utf-16 command! -bang -bar -complete=file -nargs=? Unix \ edit ++fileformat=unix command! -bang -bar -complete=file -nargs=? Mac \ edit ++fileformat=mac command! -bang -bar -complete=file -nargs=? Dos \ edit ++fileformat=dos " }}} " remove spaces {{{ command! -range=% TrimSpace ,s!\s*$!!g | nohlsearch command! -range ShrinkSpace \ ,s![^ ]\zs\s\{2,}! !g \ | normal gv \ | nohlsearch " }}} " insert a blank line every N lines {{{ command! -range -nargs=1 InsertBlankLineEvery \ ,s!\v(.*\n){}!&\r \ | nohlsearch " }}} " rename file command! -nargs=1 -complete=file Rename f |w|call delete(expand('#')) " remove trail ^M command! -range=% RemoveTrailM ,s!\r$!!g | nohlsearch " command CD {{{ command! -nargs=? -complete=dir -bang CD call s:change_dir('', '') function! s:change_dir(directory, bang) if a:directory == '' lcd %:p:h else execute 'lcd' a:directory endif if a:bang == '' pwd endif endfunction nnoremap cd :CD " }}} " format JSON command! -range FormatJson ,!python -m json.tool " capture outputs of command {{{ " ref. http://d.hatena.ne.jp/tyru/20100427/vim_capture_command command! -nargs=+ -complete=command Capture call s:cmd_capture() function! s:cmd_capture(q_args) " {{{ redir => output silent execute a:q_args redir END let output = substitute(output, '^\n\+', '', '') belowright new silent file `=printf('[Capture: %s]', a:q_args)` setlocal buftype=nofile bufhidden=unload noswapfile nobuflisted call setline(1, split(output, '\n')) endfunction " }}} " }}} " draw underline " {{{ " ref. http://vim.wikia.com/wiki/Underline_using_dashes_automatically command! -nargs=? Underline call s:underline() function! s:underline(chars) let chars = empty(a:chars) ? '-' : a:chars let nr_columns = virtcol('$') - 1 let uline = repeat(chars, (nr_columns / len(chars)) + 1) put =strpart(uline, 0, nr_columns) endfunction " }}} " delete buffers without breaking window layout {{{ command! Bdelete call s:delete_buffer() function! s:delete_buffer() if (empty(bufname('%'))) " no operation return endif let curr = bufnr('%') let prev = bufnr('#') if (prev > 0 && buflisted(prev) && curr != prev) execute 'buffer' prev else enew endif if (curr && buflisted(curr)) execute 'bdelete' curr endif endfunction " }}} " clear quickfix list command! Qclear call setqflist([]) " clear location list command! Lclear call setloclist(0, []) command! -nargs=0 RandomString call s:random_string(8) function! s:rand(n) " http://vim-users.jp/2009/11/hack98/ let match_end = matchend(reltimestr(reltime()), '\d\+\.') + 1 return reltimestr(reltime())[match_end : ] % (a:n + 1) endfunction function! s:random_string(n) let s = [] let chars = split('0123456789abcdefghijklmnopqrstuvwxyz', '\ze') let max = len(chars) - 1 for x in range(a:n) call add(s, (chars[s:rand(max)])) endfor let @+ = join(s, '') endfunction " /command }}} " keymap {{{ " ------------------------------------------------------------------------------ " vimrc {{{ nnoremap s. :source $MYVIMRC nnoremap . :edit $MYVIMRC nnoremap s> :source ~/.gvimrc nnoremap > :edit ~/.gvimrc " }}} " basic {{{ noremap ; : noremap : ; noremap ' ` noremap ` ' noremap j gj noremap k gk noremap gj j noremap gk k nnoremap J nnoremap K nnoremap j 16j nnoremap k 16k noremap H b noremap L w nnoremap Y y$ nnoremap n nzz nnoremap N Nzz nnoremap zz nnoremap zz nnoremap gm `[v`] vnoremap gm :normal gm onoremap gm :normal gm inoremap u inoremap u inoremap nnoremap O :call append(expand('.'), '')j nnoremap M :marks:mark " }}} " copy(yank) and paste with clipboard {{{ if s:in_nix inoremap p + cnoremap p + vnoremap y "+y vnoremap Y "+y$ else inoremap p * cnoremap p * vnoremap y "*y vnoremap Y "*y$ endif " }}} " command line mode {{{ cnoremap cnoremap cnoremap cnoremap cnoremap / (getcmdtype() == '/') ? '\/' : '/' cnoremap ? (getcmdtype() == '?') ? '\?' : '?' cnoremap cnoremap cnoremap cnoremap " }}} " toggle option {{{ function! s:toggle_option(option_name) execute 'setlocal' a:option_name . '!' execute 'setlocal' a:option_name . '?' endfunction nnoremap ow :call toggle_option('wrap') nnoremap nu :call toggle_option('number') nnoremap hl :call toggle_option('hlsearch') nnoremap et :call toggle_option('expandtab') " }}} " current date/time {{{ inoremap dF =strftime('%Y-%m-%dT%H:%M:%S%z') inoremap df =strftime('%Y-%m-%d %H:%M:%S') inoremap dd =strftime('%Y-%m-%d') inoremap dm =strftime('%Y-%m') inoremap dy =strftime('%Y') inoremap dT =strftime('%H:%M:%S') inoremap dt =strftime cnoremap d strftime('%Y-%m-%d') cnoremap t strftime('%Y-%m-%d-%H%M%S') " }}} " completion {{{ imap inoremap " }}} " text-objects {{{ " onoremap aa a> vnoremap aa a> onoremap ia i> vnoremap ia i> " [rectangle] onoremap ar a] vnoremap ar a] onoremap ir i] vnoremap ir i] " 'quote' onoremap aq a' vnoremap aq a' onoremap iq i' vnoremap iq i' " "double quote" onoremap ad a" vnoremap ad a" onoremap id i" vnoremap id i" " textobj-function (default f) omap af (textobj-function-a) vmap af (textobj-function-a) omap if (textobj-function-i) vmap if (textobj-function-i) " textobj-between (default f) omap a; (textobj-between-a) vmap a; (textobj-between-a) omap i; (textobj-between-i) vmap i; (textobj-between-i) " }}} " handle window {{{ nnoremap - 3- nnoremap > 3> nnoremap < 3< nnoremap + 3+ " split window nmap wj (split-to-j) nmap wk (split-to-k) nmap wh (split-to-h) nmap wl (split-to-l) nnoremap (split-to-j) \ :execute 'belowright' (v:count == 0 ? '' : v:count) 'split' nnoremap (split-to-k) \ :execute 'aboveleft' (v:count == 0 ? '' : v:count) 'split' nnoremap (split-to-h) \ :execute 'topleft' (v:count == 0 ? '' : v:count) 'vsplit' nnoremap (split-to-l) \ :execute 'botright' (v:count == 0 ? '' : v:count) 'vsplit' " }}} " yank filename {{{ if s:in_mac nnoremap yf :let @*=expand("%:p") nnoremap yy :let @*=expand("%") else nnoremap yf :let @+=expand("%:p") nnoremap yy :let @+=expand("%") endif " }}} " quickfix {{{ nnoremap [Quickfix] nmap q [Quickfix] nnoremap Q q nnoremap [Quickfix]n :cnext nnoremap [Quickfix]p :cprevious nnoremap [Quickfix]j :cnext nnoremap [Quickfix]k :cprevious nnoremap [Quickfix]r :crewind nnoremap [Quickfix]N :cfirst nnoremap [Quickfix]P :clast nnoremap [Quickfix]fn :cnfile nnoremap [Quickfix]fp :cpfile nnoremap [Quickfix]l :clist nnoremap [Quickfix]q :cc nnoremap [Quickfix]o :copen nnoremap [Quickfix]c :cclose nnoremap [Quickfix]en :cnewer nnoremap [Quickfix]ep :colder nnoremap [Quickfix]m :make " }}} " misc {{{ function! s:search_without_move() let @/ = '\<' . expand('') . '\>' call histadd('/', @/) endfunction nnoremap * \ :call search_without_move()zz:set hlsearch " search with the selected text " ref. http://vim-users.jp/2009/11/hack104/ function! s:get_selected_text() let tmp = @@ silent normal! gvy let selected = @@ let @@ = tmp return selected endfunction function! s:search_with_selected_text() let text = s:get_selected_text() let @/ = '\V' . substitute(escape(text, '\/'), "\n", '\\n', 'g') call histadd('/', @/) endfunction vnoremap * \ :call search_with_selected_text()zz:set hlsearch vnoremap \ :call search_with_selected_text()zz:set hlsearch " identify the syntax highlighting group used at the cursor " http://vim.wikia.com/wiki/Identify_the_syntax_highlighting_group_used_at_the_cursor function! s:show_hilite() let l = line('.') let c = col('.') let hilite = '' let hilite .= 'hilite <' . synIDattr(synID(l, c, 1), 'name') . '>, ' let hilite .= 'trans <' . synIDattr(synID(l, c, 0), 'name') . '>, ' let hilite .= 'link <' . synIDattr(synIDtrans(synID(l, c, 1)), 'name') . '>' echo hilite endfunction command! ShowHilite call s:show_hilite() nnoremap :ShowHilite " }}} " hlsearch (search and highlight) {{{ nnoremap :nohlsearch " }}} " completion {{{ inoremap pumvisible() ? "\" : "\" " }}} " /keymap }}} " plugin {{{ " ------------------------------------------------------------------------------ " unite {{{ " option {{{ let g:unite_split_rule = 'aboveleft' let g:unite_update_time = 100 " }}} " keymap {{{ nnoremap [Unite] nmap [Unite] nnoremap [Unite] :Unite nnoremap [Unite]f :Unite buffer_tab file_mru file nnoremap [Unite]g :UniteWithBufferDir file file_rec nnoremap [Unite]b :Unite buffer_tab nnoremap [Unite]v :Unite buffer nnoremap [Unite]r :Unite register nnoremap [Unite]q :Unite quickfix " }}} " change default action for buffer/buffer_tab {{{ call unite#custom_default_action('buffer_tab', 'goto') call unite#custom_default_action('buffer', 'goto') call unite#custom#profile('default', 'context', { \ 'no_split': 1 \}) " }}} " unite alias {{{ let g:unite_source_alias_aliases = { \ 'opera' : { \ 'source': 'file_rec', \ 'args': expand('~/Dropbox/config/opera/'), \ }, \ 'vim' : { \ 'source': 'file_rec/async', \ 'args': expand('~/.vim/.'), \ }, \ 'keymap' : { \ 'source': 'output', \ 'args': join(['map', 'map!', 'lmap'], '|'), \ }, \ } " }}} if executable('ag') let g:unite_source_grep_command = 'ag' let g:unite_source_grep_default_opts = '-i --nocolor --nogroup' let g:unite_source_grep_recursive_opt = '' endif let g:unite_source_grep_max_candidates = 100 " /unite }}} " commentary {{{ vmap / Commentary nmap / CommentaryLine " }}} " matchit {{{ source $VIMRUNTIME/macros/matchit.vim " }}} " quickrun {{{ let g:quickrun_config = { \ '_': { \ 'runner': 'vimproc', \ 'runner/vimproc/updatetime': 100, \ 'outputter/buffer/split': 'aboveleft', \ }, \ 'watchdogs_checker/_': { \ 'hook/close_quickfix/enable_exit': 1, \ 'hook/back_tabpage/enable_exit': 1, \ 'hook/back_tabpage/priority_exit': -2000, \ 'hook/back_window/enable_exit': 1, \ 'hook/back_window/priority_exit': -1000, \ }, \ 'sql': { \ 'command': 'sqlplus', \ 'cmdopt': '-S', \ 'args': '%{MyGetOracleConnection("quickrun")}', \ 'tempfile': '%{tempname()}.sql', \ 'exec': '%c %o %a \@%s', \ 'outputter/buffer/filetype': 'quickrun.sqloutput', \ }, \ 'rst': { \ 'command': 'rst2html', \ 'outputter': 'browser', \ }, \} if (s:in_mac) call extend(g:quickrun_config, { \ 'mkd' : { \ 'command': 'open', \ 'cmdopt': '-a', \ 'tempfile': '%{tempname()}.md', \ 'exec': '%c %s %o /Applications/Marked.app', \ 'outputter': 'null', \ }, \}) endif " to quickrun sql {{{ function! MyGetOracleConnection(mode) let user_pass = s:get_option('oracle_user_pass', 'system/oracle') let sid = s:get_option('oracle_sid', 'localhost/xe') let sep = (a:mode == 'quickrun') ? '\\\@' : '@' let conn = user_pass . sep . sid return conn endfunction function! s:get_option(option_name, ...) if exists('b:' . a:option_name) return eval('b:' . a:option_name) endif if exists('g:' . a:option_name) return eval('g:' . a:option_name) endif if a:0 > 0 " default value return a:1 endif endfunction " }}} " }}} " watchdogs {{{ let g:watchdogs_check_BufWritePost_enables = { \ 'javascript': 1, \} " }}} " dois.vim {{{ let g:dois_file = $HOME . '/Dropbox/tmp/doinglist.taskpaper' let g:dois_dir = $HOME . '/Dropbox/tmp/doinglist' nmap (dois:n:add-daily-task) " }}} " operator-replace {{{ nmap s (operator-replace) nmap S (operator-replace)$x nmap ss (operator-replace)(textobj-line-a)x " }}} " neosnippet {{{ let g:neosnippet#snippets_directory = join([ \ expand('~/.vim/snippet'), \ expand('~/.vim.local/snippet'), \], ',') let g:neosnippet#disable_runtime_snippets = { \ '_' : 1, \} " }}} " vim-javascript {{{ let g:html_indent_inctags = 'html,body,head,tbody,th,td,tr,tfoot,thead' let g:html_indent_script1 = 'inc' let g:html_indent_style1 = 'inc' let g:javascript_enable_domhtmlcss = 1 " }}} " vim-skrap {{{ let g:skrap_directory = expand('~/Dropbox/tmp/skrap') let g:skrap_types = [ \ 'md', 'js', 'txt', 'vim', 'sql', 'xml', 'html', 'css', 'go' \] " }}} " vim-demitas {{{ let g:demitas_directory = expand('~/Dropbox/tmp/demitas') call extend(g:unite_source_alias_aliases, { \ 'demitas' : { \ 'source': 'file_rec', \ 'args': g:demitas_directory, \ 'description': 'my tumblr files', \ }, \}) call unite#custom_source('demitas', 'sorters', 'sorter_reverse') " }}} " vim-markdown {{{ let g:vim_markdown_folding_disabled = 1 " }}} " open-browser {{{ let g:netrw_nogx = 1 nmap gx (openbrowser-smart-search) vmap gx (openbrowser-smart-search) " }}} " the tab key {{{ imap imap \ neosnippet#expandable_or_jumpable() ? "\(neosnippet_expand_or_jump)" : \ pumvisible() ? "\" : \ "\" smap \ neosnippet#expandable_or_jumpable() ? "\(neosnippet_expand_or_jump)" : \ "\" inoremap pumvisible() ? "\" : "\" xmap (neosnippet_expand_target) " }}} " operator-camerize {{{ map _ (operator-camelize-toggle) " }}} " godef {{{ let g:godef_split = 0 " }}} " altr {{{ nmap (altr-forward) nmap (altr-back) " }}} " neomru {{{ let g:neomru#time_format = '(%m/%d %H:%M) ' " }}} " operator-insert {{{ map i (operator-insert-i) map a (operator-insert-a) " }}} " operator-surround {{{ map sa (operator-surround-append) map sd (operator-surround-delete) map sr (operator-surround-replace) " }}} " /plugin }}} " autocmd {{{ " ------------------------------------------------------------------------------ " Create non-existing diretories automatically when the file is saved. function! s:auto_mkdir(dir) if isdirectory(a:dir) return endif call mkdir(iconv(a:dir, &encoding, &termencoding), 'p') endfunction autocmd my BufWritePre * call s:auto_mkdir(expand(':p:h')) " Open quickfix window after executing make. autocmd my QuickfixCmdPost make copen " Avoid saving files with keyboard misstroke " ref. http://d.hatena.ne.jp/tyru/20130419/avoid_tyop function! s:ignore_invalid_file(file) echomsg 'Invalid file name: "' . a:file . '"' endfunction autocmd my BufWriteCmd *; call s:ignore_invalid_file(expand('')) " Reload a file on WinEnter if the file has been modified set autoread autocmd my WinEnter * checktime " key mapping in vimdiff function! s:config_in_diff_mode() if !&diff return endif nnoremap [c nnoremap ]c endfunction autocmd my FilterWritePre * call s:config_in_diff_mode() " }}} " finally {{{ " ------------------------------------------------------------------------------ call s:load_local_vimrc() call watchdogs#setup(g:quickrun_config) set secure " /finally }}} " @see :help modeline " vim: set noexpandtab : " vim: set foldenable foldmethod=marker : " vim: set formatoptions& formatoptions-=ro :