" " Vim configuration " Author: Alisue " URI: http://hashnote.net/ " Platform: Linux, Mac OS X " " skip if the live Vim is vim-tiny or vim-small if !1 | finish | endif " reset settings for reloading vimrc augroup MyAutoCmd autocmd! augroup END " disable unnecessary default plugins let g:loaded_gzip = 1 let g:loaded_tar = 1 let g:loaded_tarPlugin = 1 let g:loaded_zip = 1 let g:loaded_zipPlugin = 1 let g:loaded_rrhelper = 1 let g:loaded_2html_plugin = 1 let g:loaded_vimball = 1 let g:loaded_vimballPlugin = 1 let g:loaded_getscript = 1 let g:loaded_getscriptPlugin = 1 let g:loaded_netrw = 1 let g:loaded_netrwPlugin = 1 let g:loaded_netrwSettings = 1 let g:loaded_netrwFileHandlers = 1 if !has('gui_running') " disable matchparen in non GUI Vim let g:loaded_matchparen = 1 endif " environmental variables {{{ if has('nvim') set shell=/bin/zsh else set shell=/bin/bash endif " Add several required path to $PATH call rook#add_path([ \ '/usr/local/texlive/2013/bin/x86_64-linux', \ '/usr/local/texlive/2013/bin/x86_64-darwin', \ '~/.pyenv/bin', \ '~/.plenv/bin', \ '~/.rbenv/bin', \ '~/.ndenv/bin', \ '~/.pyenv/shims', \ '~/.plenv/shims', \ '~/.rbenv/shims', \ '~/.ndenv/shims', \ '~/.anyenv/envs/pyenv/bin', \ '~/.anyenv/envs/plenv/bin', \ '~/.anyenv/envs/rbenv/bin', \ '~/.anyenv/envs/ndenv/bin', \ '~/.anyenv/envs/pyenv/shims', \ '~/.anyenv/envs/plenv/shims', \ '~/.anyenv/envs/rbenv/shims', \ '~/.anyenv/envs/ndenv/shims', \ '~/.cabal/bin', \ '~/.vim/bundle/vim-themis/bin', \]) let $MYVIMRUNTIME = expand('~/.vim') let $MYVIMRC = resolve(rook#normpath('vimrc')) let $MYGVIMRC = resolve(rook#normpath('gvimrc')) let $PYENV_ROOT = \ isdirectory(expand('~/.anyenv/envs/pyenv')) \ ? expand('~/.anyenv/envs/pyenv') \ : isdirectory(expand('~/.pyenv')) \ ? expand('~/.pyenv') \ : '' " }}} " fundemental config {{{ " history ':' commands and search patterns as much as possible (max: 10000) if &history != 10000 set history=10000 endif " define and let g:mapleader = ';' let g:maplocalleader = ',' " remove any existing keymap for leader and localleader nnoremap ; xnoremap ; nnoremap , xnoremap , if !has('gui_running') if !has('nvim') " neovim does not have the following features set ttyfast " enable fast terminal connection set ttyscroll=3 " the maximum number of lines for scrolling. " use small value here to prefer redraw the screen " instead of scrolling endif set t_Co=256 " use 256 colors " to enable cursor keys in terminal set notimeout ttimeout ttimeoutlen=100 else set timeout timeoutlen=1000 ttimeoutlen=100 endif " }}} " ancient features {{{ set noswapfile set nobackup set nowritebackup set noerrorbells set novisualbell set t_vb= " }}} " language and encoding {{{ " prefer English interface language message C " prefer English help set helplang=en,ja " set default language for spell check " cjk - ignore spell check on Asian characters (China, Japan, Korea) set nospell set spelllang=en_us,cjk let &spellfile = expand('~/Copy/Apps/Vim/system/spellfile.utf-8.add') " prefer UTF-8 if has('multi_byte') if has('vim_starting') || !has('nvim') set encoding=utf-8 set termencoding=utf-8 set ambiwidth=double endif scriptencoding utf-8 set fileencodings=ucs-bom,utf-8,euc-jp,iso-2022-jp,cp932,utf-16,utf-16le set fileformats=unix,dos,mac endif " }}} " search {{{ set smartcase " override the ignorecase if the search pattern contains " upper case characters set incsearch " use incremental search set nowrapscan " do not wrap scan set hlsearch " highlight found terms " K to search the help with the cursor word set keywordprg=:help " automatically escape / or ? cnoremap / getcmdtype() == '/' ? '\/' : '/' cnoremap ? getcmdtype() == '?' ? '\?' : '?' " remove highlight with pressing ESC twice nnoremap :nohlsearch " }}} " edit {{{ set nomodeline " do not use modeline set smarttab " insert blanks according to shiftwidth set expandtab " use spaces instead of TAB set tabstop=8 " the number of spaces that a TAB counts for set softtabstop=4 " the number of spaces that a TAB counts for set shiftwidth=4 " the number of spaces of an indent set shiftround " round indent to multiple of shiftwidth with > and < set autoindent " copy indent from current line when starting a new line set copyindent " copy the structure of the existing lines indent when " autoindenting a new line " every wrapped line will continue visually indented if has('&breakindent') set breakindent endif set hidden " hide the buffer instead of close set switchbuf=useopen " use an existing buffer instaed of creating a new one set autoread " automatically read the changed contents " store cursor, folds, slash, unix on view set viewoptions=cursor,folds,slash,unix " change default view directory let &viewdir = rook#normpath('.view') if !isdirectory(&viewdir) call mkdir(&viewdir, 'p', 0700) endif " use clipboard register if has('unnamedplus') set clipboard=unnamedplus,unnamed else set clipboard=unnamed endif " keep undo history on undofile if has('persistent_undo') let &undodir = rook#normpath('.undo') if !isdirectory(&undodir) call mkdir(&undodir, 'p', 0700) endif set undofile endif " allow backspacing over everything in insert mode set backspace=indent,eol,start " }}} " cursor {{{ set showmatch " highlight a partner of cursor character set matchtime=1 " highlight a partner ASAP set matchpairs& mps+=<:> " add < and > pair set updatetime=1000 " increase speed of CursorHold autocommand set virtualedit=all " allow virtual editing in all modes set nostartofline " let C-D, C-U,... to keep same column " move cursor previous/next line when the cursor is first/last character in the " line and user move the cursor left/right set whichwrap=b,s,<,>,~,[,] " do not display cursorline/column (it makes Vim really slow) set nocursorline set nocursorcolumn " }}} " folding {{{ set foldenable set foldmethod=marker set foldlevelstart=99 set foldnestmax=3 " maximum fold nesting level set foldcolumn=3 " show fold guide " }}} " interface {{{ set number " show line numbers set list " show invisible characters set laststatus=2 " always shows statusline set scrolloff=4 " minimum number of screen lines to keep in scroll set sidescrolloff=4 " minimum number of screen columns to keep in scroll set cmdheight=2 " the number of screen lines to use for command line set showcmd " show command on statusline set nolazyredraw " do not redraw while command execution set nosplitbelow " split to top set splitright " vsplit to right set report=0 " report any changes set display=lastline " display last line in a window AMAP set showtabline=2 " display tabline always set wrap " wrap long text set textwidth=0 " do not automatically break set breakat=\ \ ;:,!? " characters which might cause a line break " do not display greeting message set shortmess=aTI " use rich completion system in command line set wildmenu set wildmode=list:longest,full set wildoptions=tagfile " completion settings set completeopt=menu,longest set complete=. " don't complete from other buffer set pumheight=20 " height of popup menu set showfulltag " show both the tag name and the search pattern if has('patch-7.4.314') " don't give ins-completion-menu messages set shortmess+=c endif " Conceal set conceallevel=2 set concealcursor=c set colorcolumn=80 if has('multi_byte') && $LANG !=# 'C' set listchars=tab:»-,trail:-,extends:»,precedes:«,nbsp:%,eol:$ else set listchars=tab:>-,trail:-,extends:>,precedes:<,nbsp:%,eol:$ endif " }}} " key mapping {{{ " disable middle-click paste map map <2-MiddleMouse> map <3-MiddleMouse> map <4-MiddleMouse> imap imap <2-MiddleMouse> imap <3-MiddleMouse> imap <4-MiddleMouse> " remap j and k to act as expected when used on long, wrapped, lines nnoremap j gj nnoremap k gk nnoremap gj j nnoremap gk k " Emacs like binding in Insert mode inoremap ^ inoremap $ inoremap w inoremap b inoremap x " Ctrl-h/j/k/l to move around in Insert mode inoremap h inoremap j inoremap k inoremap l " Y to yank the end of line nnoremap Y y$ " to paste from 0 register nnoremap "0p " vv to select the line, like yy, dd nnoremap vv 0v$ " simple window resize navigation nnoremap < nnoremap > nnoremap - nnoremap + " tag navigation nnoremap [t :tprevious nnoremap ]t :tnext nnoremap g[t :tfirst nnoremap g]t :tlast " quickfix navigation nnoremap [q :cprevious nnoremap ]q :cnext nnoremap g[q :cfirst nnoremap g]q :clast nnoremap [l :lprevious nnoremap ]l :lnext nnoremap g[l :lfirst nnoremap g]l :llast " file navigation nnoremap {f :previous nnoremap }f :next nnoremap g{f :first nnoremap g}f :last " tab operation (make similar mapping with window operation) " ref. " n Create a new window and start editing an empty file in it. " q Quit the current window. (:quit) " c Close the current window. (:close) " o Only " w Move to below/right " W Move to top/left " p Move to previous function! s:tab_quit() abort if tabpagenr('$') == 1 quit else tabclose endif endfunction nnoremap (my-tab-new) :tabnew nnoremap (my-tab-quit) :call tab_quit() nnoremap (my-tab-close) :tabclose nnoremap (my-tab-only) :tabonly nnoremap (my-tab-next) :tabnext nnoremap (my-tab-prev) :tabprevious nnoremap (my-tab-move-0) :0tabmove nnoremap (my-tab-move-L) :-tabmove nnoremap (my-tab-move-R) :+tabmove nnoremap (my-tab-move-$) :$tabmove nnoremap (my-tab-0) :tabfirst nnoremap (my-tab-1) :tabnext1 nnoremap (my-tab-2) :tabnext2 nnoremap (my-tab-3) :tabnext3 nnoremap (my-tab-4) :tabnext4 nnoremap (my-tab-5) :tabnext5 nnoremap (my-tab-6) :tabnext6 nnoremap (my-tab-7) :tabnext7 nnoremap (my-tab-8) :tabnext8 nnoremap (my-tab-9) :tabnext9 nnoremap (my-tab-$) :tablast nmap n (my-tab-new) nmap (my-tab-new) nmap q (my-tab-quit) nmap (my-tab-quit) nmap c (my-tab-close) nmap o (my-tab-only) nmap (my-tab-only) nmap t (my-tab-next) nmap (my-tab-next) nmap l (my-tab-next) nmap (my-tab-next) nmap T (my-tab-prev) nmap h (my-tab-prev) nmap (my-tab-prev) nmap j (my-tab-$) nmap (my-tab-$) nmap k (my-tab-0) nmap (my-tab-0) nmap L (my-tab-move-R) nmap H (my-tab-move-L) nmap J (my-tab-move-$) nmap K (my-tab-move-0) nmap 0 (my-tab-0) nmap 1 (my-tab-1) nmap 2 (my-tab-2) nmap 3 (my-tab-3) nmap 4 (my-tab-4) nmap 5 (my-tab-5) nmap 6 (my-tab-6) nmap 7 (my-tab-7) nmap 8 (my-tab-8) nmap 9 (my-tab-9) nmap $ (my-tab-$) " switch options nnoremap (my-switch) nmap s (my-switch) nnoremap (my-switch)s :setl spell! spell? nnoremap (my-switch)l :setl list! list? nnoremap (my-switch)t :setl expandtab! expandtab? nnoremap (my-switch)w :setl wrap! wrap? nnoremap (my-switch)p :setl paste! paste? nnoremap (my-switch)b :setl scrollbind! scrollbind? nnoremap (my-switch)y :call toggle_syntax() function! s:toggle_syntax() abort if exists('g:syntax_on') syntax off redraw echo 'syntax off' else syntax on redraw echo 'syntax on' endif endfunction " }}} " macros {{{ " save the file as root with 'sudo" cabbr w!! w !sudo tee > /dev/null % " save and restore the cursor position and folding level function! s:is_view_available() abort " {{{ if !&buflisted || &buftype !=# '' return 0 elseif !filewritable(expand('%:p')) return 0 endif return 1 endfunction " }}} function! s:mkview() abort " {{{ if s:is_view_available() silent! mkview endif endfunction " }}} function! s:loadview() abort " {{{ if s:is_view_available() silent! loadview endif endfunction " }}} "autocmd MyAutoCmd BufWinLeave ?* call s:mkview() "autocmd MyAutoCmd BufReadPost ?* call s:loadview() " reload vimrc with r {{{ if has('vim_starting') function s:reload_vimrc() abort execute printf('source %s', $MYVIMRC) if has('gui_running') execute printf('source %s', $MYGVIMRC) endif redraw echo printf('.vimrc/.gvimrc has reloaded (%s).', strftime('%c')) endfunction endif nmap (my-reload-vimrc) :call reload_vimrc() nmap r (my-reload-vimrc) " }}} " source/reload current vimscript file " {{{ function! s:source_current_vimscript() abort let abspath = resolve(expand('%:p')) if &filetype !=# 'vim' redraw echohl WarningMsg echo printf( \ 'The filetype of the current buffer is "%s" but it must be "vim" for source.', \ &filetype, \) echohl None return elseif abspath ==# $MYVIMRC || abspath ==# $MYGVIMRC redraw echohl WarningMsg echo 'The .vimrc/.gvimrc cannot be reloaded by this function. Use (my-reload-vimrc) instead.' echohl None return endif execute printf('source %s', expand('%')) redraw echo printf('"%s" has sourced (%s).', expand('%:t'), strftime('%c')) endfunction nmap (my-source) :call source_current_vimscript() nmap s (my-source) " }}} " automatically create missing directories {{{ autocmd MyAutoCmd BufWritePre * call s:makedirs(expand(':p:h'), v:cmdbang) function! s:makedirs(dir, force) abort if a:dir =~# '^.\{-}://' " Non local file, ignore return endif if !isdirectory(a:dir) if a:force || input(printf('"%s" does not exist. Create? [y/N]', a:dir)) =~? '^y\%[es]$' call mkdir(a:dir, 'p') endif endif endfunction " }}} " automatically change working directory on vim enter {{{ function! s:workon(dir, bang) abort let dir = (a:dir ==# '' ? expand('%') : a:dir) " convert filename to directory if required if filereadable(dir) let dir = fnamemodify(expand(dir),':p:h') else let dir = fnamemodify(dir, ':p') endif " change directory to specified directory if isdirectory(dir) silent execute 'cd ' . fnameescape(dir) if a:bang ==# '' redraw | echo 'Working on: '.dir if v:version > 703 || (v:version == 703 && has('patch438')) doautocmd MyAutoCmd User my-workon-post else doautocmd MyAutoCmd User my-workon-post endif endif endif endfunction autocmd MyAutoCmd VimEnter ?* call s:workon(expand(''), 1) command! -nargs=? -complete=dir -bang Workon call s:workon('', '') " }}} " toggle quickfix window {{{ function! s:toggle_qf() abort let nwin = winnr('$') cclose if nwin == winnr('$') cwindow endif endfunction nnoremap q :call toggle_qf() " }}} " highlight zenkaku spaces {{{ autocmd MyAutoCmd VimEnter,WinEnter * match MyZenkakuSpace / / highlight MyZenkakuSpace \ cterm=reverse ctermfg=DarkMagenta \ gui=reverse guifg=DarkMagenta " }}} " Open junk file."{{{ let s:junk_dir = rook#normpath('.junk') if !isdirectory(s:junk_dir) call mkdir(s:junk_dir, 'p') endif function! s:open_junk_file() abort let filename = strftime('%Y-%m-%d-%H-%M-%S.') let filename = input( \ 'Name of the junk file: ', \ filename, \) let path = s:junk_dir . '/' . filename redraw! if !empty(path) execute printf('edit %s', fnameescape(path)) else echo 'The operation has canceled by user.' endif endfunction command! -nargs=0 JunkFile call s:open_junk_file() " }}} function! s:screen_cast() abort set columns=80 set lines=30 endfunction command! -nargs=0 ScreenCast call s:screen_cast() function! s:clear_messages() abort for i in range(201) echomsg '' endfor endfunction command! -nargs=0 MessageClear call s:clear_messages() function! s:timeit(q_args) abort let q_args = a:q_args let q_args = substitute(q_args, '^[\t :]\+', '', '') let q_args = substitute(q_args, '\s\+$', '', '') let args = substitute(q_args, '^[ :]*!', '', '') let start = reltime() try if q_args !=# '' && q_args[0] ==# '!' echo system(args) else execute q_args endif finally echomsg printf('Timeit: %s s [%s]', reltimestr(reltime(start)), a:q_args) endtry endfunction command! -nargs=+ -bang -complete=command Timeit call s:timeit() " }}} if !has('gui_running') let g:hybrid_use_Xresources = 1 endif colorscheme hybrid call rook#source(rook#normpath('rc/plugin.vim')) call rook#source(expand('~/.vimrc.local')) " vim:set et ts=2 sts=2 sw=2 tw=0 fdm=marker: