From fa5946b35af5a39de89505223e1d2affea4616b6 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Fri, 12 May 2023 21:16:52 +0100 Subject: Adds useful commands in vimscript --- vim/vimrc | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) (limited to 'vim/vimrc') diff --git a/vim/vimrc b/vim/vimrc index ad792b7..8202049 100644 --- a/vim/vimrc +++ b/vim/vimrc @@ -25,7 +25,88 @@ endfunction nnoremap q :call TaskWarriorAddCurrentLine() -" encryption method when using :X +" redirect any output to a scratch buffer +" from https://gist.github.com/romainl/eae0a260ab9c135390c30cd370c20cd7 +function! Redir(cmd, rng, start, end) + for win in range(1, winnr('$')) + if getwinvar(win, 'scratch') + execute win . 'windo close' + endif + endfor + if a:cmd =~ '^!' + let cmd = a:cmd =~' %' + \ ? matchstr(substitute(a:cmd, ' %', ' ' . shellescape(escape(expand('%:p'), '\')), ''), '^!\zs.*') + \ : matchstr(a:cmd, '^!\zs.*') + if a:rng == 0 + let output = systemlist(cmd) + else + let joined_lines = join(getline(a:start, a:end), '\n') + let cleaned_lines = substitute(shellescape(joined_lines), "'\\\\''", "\\\\'", 'g') + let output = systemlist(cmd . " <<< $" . cleaned_lines) + endif + else + redir => output + execute a:cmd + redir END + let output = split(output, "\n") + endif + vnew + let w:scratch = 1 + setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile + call setline(1, output) +endfunction + +" This command definition includes -bar, so that it is possible to "chain" Vim commands. +" Side effect: double quotes can't be used in external commands +command! -nargs=1 -complete=command -bar -range Redir silent call Redir(, , , ) + +" This command definition doesn't include -bar, so that it is possible to use double quotes in external commands. +" Side effect: Vim commands can't be "chained". +command! -nargs=1 -complete=command -range Redir silent call Redir(, , , ) + +" big from https://gist.github.com/romainl/047aca21e338df7ccf771f96858edb86 +nnoremap ;s :g//# +function! CCR() + let cmdline = getcmdline() + if cmdline =~ '\v\C^(ls|files|buffers)' + " like :ls but prompts for a buffer command + return "\:b" + elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number)$' + " like :g//# but prompts for a command + return "\:" + elseif cmdline =~ '\v\C^(dli|il)' + " like :dlist or :ilist but prompts for a count for :djump or :ijump + return "\:" . cmdline[0] . "j " . split(cmdline, " ")[1] . "\\" + elseif cmdline =~ '\v\C^(cli|lli)' + " like :clist or :llist but prompts for an error/location number + return "\:sil " . repeat(cmdline[0], 2) . "\" + elseif cmdline =~ '\C^old' + " like :oldfiles but prompts for an old file to edit + set nomore + return "\:sil se more|e #<" + elseif cmdline =~ '\C^changes' + " like :changes but prompts for a change to jump to + set nomore + return "\:sil se more|norm! g;\" + elseif cmdline =~ '\C^ju' + " like :jumps but prompts for a position to jump to + set nomore + return "\:sil se more|norm! \\" + elseif cmdline =~ '\C^marks' + " like :marks but prompts for a mark to jump to + return "\:norm! `" + elseif cmdline =~ '\C^undol' + " like :undolist but prompts for a change to undo + return "\:u " + else + return "\" + endif +endfunction +cnoremap CCR() + + + +" encryptio) method when using :X set cm=blowfish2 " Underline the current line, based on its length. -- cgit v1.2.3