" vim: set sw=2 ts=2 sts=2 et tw=78 foldmarker={{,}} foldmethod=marker foldlevel=0: let g:mapleader = ',' " basic {{ " Edit file in current file folder nnoremap n :Lexplore nnoremap q :silent! Bdelete! nnoremap e :e =substitute(expand('%:p:h').'/', getcwd().'/', '', '') nnoremap v :vs =substitute(expand('%:p:h').'/', getcwd().'/', '', '') nnoremap t :tabe =substitute(expand('%:p:h').'/', getcwd().'/', '', '') nnoremap rm :Rm =expand('%:p:h').'/' nnoremap mk :Mkdir =expand('%:p:h').'/' " Replace all of current word nnoremap s :%s/\<\>//g " Reload vimrc file nnoremap rl :source ~/.vimrc " Search with grep nnoremap / :Rg " generate doc nnoremap d :call GenDoc() " clean some dirty charactors nnoremap cl :call Clean() " show vim highlight group under cursor nnoremap hi :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' \ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" \ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">" nnoremap pp "0p nnoremap o :call Open() " }} " setting switch {{ nnoremap sc :setl spell! nnoremap pt :set paste! nnoremap nu :call NumberToggle() nnoremap bg :call ToggleBackground() " }} " plugin {{ "session helper nmap ss :call SessionSave() nmap sl :SessionLoad nmap sr :call SessionReload() " svg.vim not used very often nmap se SvgEdit " rename.nvim grep and replace nmap rs (rename-search-replace) " coc.nvim nmap x (coc-cursors-operator) nmap rn (coc-rename) nmap rf (coc-refactor) nmap ca (coc-codelens-action) xmap f (coc-format-selected) nmap f (coc-format-selected) xmap a (coc-codeaction-selected) nmap a (coc-codeaction-selected) nmap ac (coc-codeaction) nmap di (coc-diagnostic-info) nmap qf (coc-fix-current) nmap te :call CocAction('runCommand', 'jest.singleTest') nmap dr (coc-diagnostic-related) nmap tr (coc-translator-p) " }} " grep by motion {{ vnoremap g :call GrepFromSelected(visualmode()) nnoremap g :set operatorfunc=GrepFromSelectedg@ function! s:GrepFromSelected(type) let saved_unnamed_register = @@ if a:type ==# 'v' normal! `y elseif a:type ==# 'char' normal! `[v`]y else return endif let word = substitute(@@, '\n$', '', 'g') let word = escape(word, '| ') let @@ = saved_unnamed_register execute 'CocList grep '.word endfunction " }} " functions {{ function! s:SessionSave() if !empty(v:this_session) execute 'SessionSave' else call feedkeys(':SessionSave ') endif endfunction function! s:ToggleBackground() if &background ==# 'light' set background=dark else set background=light endif call SetStatusLine() endfunction function! s:NumberToggle() if(&number == 1) | set nu! | set rnu! | else | set rnu | set nu | endif endfunction function! s:SessionReload() execute 'wa' execute 'RestartVim' endfunction " Simple clean utility function! s:Clean() let view = winsaveview() let ft = &filetype " replace tab with 2 space if index(['javascript', 'html', 'css', 'vim', 'php'], ft) != -1 silent! execute "%s/\/ /g" endif " replace tailing comma if ft ==# 'javascript' || ft ==# 'typescript' silent! execute '%s/;$//' endif " remove tailing white space silent! execute '%s/\s\+$//' " remove windows `\r` call winrestview(view) endfunction function! s:GenDoc() if &ft ==# 'javascript' || &ft ==# 'typescript' exe "JsDoc" elseif &ft ==# 'css' let lines = ['/*', ' * ', ' */'] exe "normal! j?{$\:noh\" let lnum = getpos('.')[1] call append(lnum - 1, lines) exe "normal! kk$" startinsert! elseif &ft ==# 'html' let lnum = getpos('.')[1] let ind = matchstr(getline('.'), '\v\s*') call append(lnum - 1, ind . '') exe "normal! k^Ell" startinsert elseif &filetype ==# 'vim' let lnum = getpos('.')[1] let ind = matchstr(getline('.'), '\v\s*') call append(lnum - 1, ind . '" ') exe "normal! k$" startinsert! else let lnum = getpos('.')[1] let ind = matchstr(getline('.'), '\v\s*') call append(lnum - 1, ind. '# ') exe "normal! k$" startinsert! endif endfunction function! s:Open() let res = CocAction('openLink') if res | return | endif let line = getline('.') " match url let url = matchstr(line, '\vhttps?:\/\/[^)\]''" ]+') if !empty(url) let output = system('open '. url) else let mail = matchstr(line, '\v([A-Za-z0-9_\.-]+)\@([A-Za-z0-9_\.-]+)\.([a-z\.]+)') if !empty(mail) let output = system('open mailto:' . mail) else let output = system('open ' . expand('%:p:h')) endif endif if v:shell_error && output !=# "" echoerr output endif endfunction " }}