diff --git a/.vimrc b/.vimrc index 7df5cd2..d67a6be 100644 --- a/.vimrc +++ b/.vimrc @@ -90,7 +90,7 @@ if has('autocmd') " vim-tiny does not have autocmd call mkdir(b:backup_dir, "p", 0700) endif endif - call system('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") .. '.bak') + call system('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") . '.bak') endfunction autocmd BufWritePost * call BackupFile() set swapfile @@ -368,8 +368,8 @@ if has('autocmd') " vim-tiny does not have autocmd " neovim 0.10.0 uses ripgrep as greppg by default command! -nargs=1 RgToQf call SystemToQf('rg --vimgrep ' . ) - noremap ]q :call ToggleQuickfix('c') - noremap ]l :call ToggleQuickfix('l') + nnoremap ]q :call ToggleQuickfix('c') + nnoremap ]l :call ToggleQuickfix('l') nnoremap cn :cn nnoremap cp :cp nnoremap ln :ln @@ -386,9 +386,6 @@ if has('autocmd') " vim-tiny does not have autocmd " 清空高亮 nnoremap lc :call clearmatches() - " Example: - " :e+22 ~/.vimrc - command! -nargs=0 VimExeLine exe getline(".") if v:version >= 740 || &readonly == 0 set mouse=a if !has('nvim') && has('unix') && exists('$TMUX') @@ -438,7 +435,7 @@ if has('autocmd') " vim-tiny does not have autocmd " [[palette]]git-blame :GitBlame command -range -nargs=0 GitBlame :!git blame -n -L , -- % - if has("autocmd") && exists("+omnifunc") + if exists("+omnifunc") autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif endif function! MyCppCompleteFunc(findstart, base) diff --git a/root/.vim/autoload/noplug.vim b/root/.vim/autoload/noplug.vim index 69899a4..1921696 100644 --- a/root/.vim/autoload/noplug.vim +++ b/root/.vim/autoload/noplug.vim @@ -1,3 +1,17 @@ +function noplug#GetVisualSelection() + " https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript + " Why is this not a built-in Vim script function?! + let [line_start, column_start] = getpos("'<")[1:2] + let [line_end, column_end] = getpos("'>")[1:2] + let lines = getline(line_start, line_end) + if len(lines) == 0 + return '' + endif + let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] + let lines[0] = lines[0][column_start - 1:] + return join(lines, "\n") +endfunction + function! noplug#MyRead(file) " 获取文件大小 let l:size = getfsize(a:file) @@ -88,3 +102,28 @@ function noplug#SystemToQf(args) call setqflist([], 'r', {'title': a:args}) call s:ShowQuickfixListIfNotEmpty() endfunction + +function noplug#AsyncRunOrSystem(cmd) + if exists("*asyncrun#run") + call asyncrun#run('', {'silent': 1}, a:cmd) + else + call system(a:cmd) + endif +endfunction +" NOTE: 运行 clean-vim-backup.py,仅保留最新的1个备份版本 +function noplug#BackupFile() + " 获取当前文件的大小(单位:字节) + let l:filesize = getfsize(expand('%:p')) + " 设置大小阈值为5MB + let l:maxsize = 5 * 1024 * 1024 + if l:filesize > l:maxsize + return + endif + if !exists("b:backup_dir") + let b:backup_dir = expand("~/.vim/backup") . expand("%:p:h") + if !isdirectory(b:backup_dir) + call mkdir(b:backup_dir, "p", 0700) + endif + endif + call noplug#AsyncRunOrSystem('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") . '.bak') +endfunction diff --git a/root/.vim/doc/command-line-tool.txt b/root/.vim/doc/command-line-tool.txt index 06fa97e..e72dd9f 100644 --- a/root/.vim/doc/command-line-tool.txt +++ b/root/.vim/doc/command-line-tool.txt @@ -14,6 +14,8 @@ vim:ft=help ------------------------------------------------------------------------------- +*the-art-of-command-line* https://github.com/jlevy/the-art-of-command-line/blob/master/README-zh.md + *write-maintainable-command-line-tools* https://github.com/jiangyinzuo/write-maintainable-command-line-tools diff --git a/root/.vim/pack/my_plugins/opt/gtags_asyncrun/plugin/gtags_asyncrun.vim b/root/.vim/pack/my_plugins/opt/gtags_asyncrun/plugin/gtags_asyncrun.vim index 1676127..280a2c6 100755 --- a/root/.vim/pack/my_plugins/opt/gtags_asyncrun/plugin/gtags_asyncrun.vim +++ b/root/.vim/pack/my_plugins/opt/gtags_asyncrun/plugin/gtags_asyncrun.vim @@ -550,7 +550,7 @@ function! s:GtagsAutoUpdate() if l:file == '' return endif - let l:result = AsyncRunOrSystem(s:GlobalCommand() . " -u --single-update=\"" . l:file . "\"") + let l:result = noplug#AsyncRunOrSystem(s:GlobalCommand() . " -u --single-update=\"" . l:file . "\"") endfunction " @@ -577,7 +577,7 @@ function! GtagsCandidateCore(lead, line, pos) return glob(l:pattern) else let l:cmd = s:GlobalCommand() . ' ' . '-c' . s:option . ' ' . a:lead - call AsyncRunOrSystem(l:cmd) + call noplug#AsyncRunOrSystem(l:cmd) endif endfunction diff --git a/root/.vim/vimrc b/root/.vim/vimrc index 3e83ffb..84110ad 100644 --- a/root/.vim/vimrc +++ b/root/.vim/vimrc @@ -131,31 +131,7 @@ if has('autocmd') " vim-tiny does not have autocmd set nobackup set nowritebackup - function AsyncRunOrSystem(cmd) - if exists("*asyncrun#run") - call asyncrun#run('', {'silent': 1}, a:cmd) - else - call system(a:cmd) - endif - endfunction - " NOTE: 运行 clean-vim-backup.py,仅保留最新的1个备份版本 - function BackupFile() - " 获取当前文件的大小(单位:字节) - let l:filesize = getfsize(expand('%:p')) - " 设置大小阈值为5MB - let l:maxsize = 5 * 1024 * 1024 - if l:filesize > l:maxsize - return - endif - if !exists("b:backup_dir") - let b:backup_dir = expand("~/.vim/backup") . expand("%:p:h") - if !isdirectory(b:backup_dir) - call mkdir(b:backup_dir, "p", 0700) - endif - endif - call AsyncRunOrSystem('cp ' . expand('%:p') . ' ' . b:backup_dir . '/' . expand('%:t') . strftime("~~%Y-%m%d-%X") .. '.bak') - endfunction - autocmd BufWritePost * call BackupFile() + autocmd BufWritePost * call noplug#BackupFile() set swapfile set updatecount=100 " 会话不保存options, 防止重新set background=dark后,覆盖一些highlight设置 @@ -243,7 +219,7 @@ if has('autocmd') " vim-tiny does not have autocmd set undofile endif """"""""""""""""""""""""""""""" - if has("patch-8.1.0360") " nvim also has this patch + if has("patch-8.1.0360") set diffopt=vertical,filler,context:3,indent-heuristic,algorithm:patience,internal endif command -nargs=0 DiffthisSetfiletypeText :diffthis | setf text @@ -299,30 +275,16 @@ if has('autocmd') " vim-tiny does not have autocmd augroup END endif - function GetVisualSelection() - " https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript - " Why is this not a built-in Vim script function?! - let [line_start, column_start] = getpos("'<")[1:2] - let [line_end, column_end] = getpos("'>")[1:2] - let lines = getline(line_start, line_end) - if len(lines) == 0 - return '' - endif - let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)] - let lines[0] = lines[0][column_start - 1:] - return join(lines, "\n") - endfunction - nnoremap vw :call noplug#FindWord(expand("")) - vnoremap vw :call noplug#FindWord(GetVisualSelection()) + vnoremap vw :call noplug#FindWord(noplug#GetVisualSelection()) command! -nargs=1 VimWord call noplug#FindWord() nnoremap vy :call noplug#FindType(expand("")) - vnoremap vy :call noplug#FindType(GetVisualSelection()) + vnoremap vy :call noplug#FindType(noplug#GetVisualSelection()) command! -nargs=1 VimType call noplug#FindType() nnoremap vd :call noplug#FindDefinitionFunction(expand("")) - vnoremap vd :call noplug#FindDefinitionFunction(GetVisualSelection()) + vnoremap vd :call noplug#FindDefinitionFunction(noplug#GetVisualSelection()) command! -nargs=1 VimDef call noplug#FindDefinitionFunction() " Reference: vim.fandom.com/wiki/Searching_for_files @@ -337,8 +299,8 @@ if has('autocmd') " vim-tiny does not have autocmd " neovim 0.10.0 uses ripgrep as greppg by default command! -nargs=1 RgToQf call noplug#SystemToQf('rg --vimgrep ' . ) - noremap ]q :call noplug#ToggleQuickfix('c') - noremap ]l :call noplug#ToggleQuickfix('l') + nnoremap ]q :call noplug#ToggleQuickfix('c') + nnoremap ]l :call noplug#ToggleQuickfix('l') nnoremap cn :cn nnoremap cp :cp nnoremap ln :ln @@ -352,9 +314,6 @@ if has('autocmd') " vim-tiny does not have autocmd " 清空高亮 nnoremap lc :call clearmatches() - " Example: - " :e+22 ~/.vimrc - command! -nargs=0 VimExeLine exe getline(".") if v:version >= 740 || &readonly == 0 set mouse=a if !has('nvim') && has('unix') && exists('$TMUX')