aboutsummaryrefslogtreecommitdiffstats
path: root/vim/vimrc
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2023-05-12 21:16:52 +0100
committerMatthew Lemon <y@yulqen.org>2023-05-12 21:16:52 +0100
commitfa5946b35af5a39de89505223e1d2affea4616b6 (patch)
tree8542691bff7bd1d4e3f99eda16d647289a37b549 /vim/vimrc
parent14b1fd422b7f5c1b5784388c9049c1295ba73b31 (diff)
Adds useful commands in vimscript
Diffstat (limited to '')
-rw-r--r--vim/vimrc83
1 files changed, 82 insertions, 1 deletions
diff --git a/vim/vimrc b/vim/vimrc
index ad792b7..8202049 100644
--- a/vim/vimrc
+++ b/vim/vimrc
@@ -25,7 +25,88 @@ endfunction
nnoremap <leader>q :call TaskWarriorAddCurrentLine()<CR>
-" 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(<q-args>, <range>, <line1>, <line2>)
+
+" 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(<q-args>, <range>, <line1>, <line2>)
+
+" big from https://gist.github.com/romainl/047aca21e338df7ccf771f96858edb86
+nnoremap ;s :g//#<Left><Left>
+function! CCR()
+ let cmdline = getcmdline()
+ if cmdline =~ '\v\C^(ls|files|buffers)'
+ " like :ls but prompts for a buffer command
+ return "\<CR>:b"
+ elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number)$'
+ " like :g//# but prompts for a command
+ return "\<CR>:"
+ elseif cmdline =~ '\v\C^(dli|il)'
+ " like :dlist or :ilist but prompts for a count for :djump or :ijump
+ return "\<CR>:" . cmdline[0] . "j " . split(cmdline, " ")[1] . "\<S-Left>\<Left>"
+ elseif cmdline =~ '\v\C^(cli|lli)'
+ " like :clist or :llist but prompts for an error/location number
+ return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
+ elseif cmdline =~ '\C^old'
+ " like :oldfiles but prompts for an old file to edit
+ set nomore
+ return "\<CR>:sil se more|e #<"
+ elseif cmdline =~ '\C^changes'
+ " like :changes but prompts for a change to jump to
+ set nomore
+ return "\<CR>:sil se more|norm! g;\<S-Left>"
+ elseif cmdline =~ '\C^ju'
+ " like :jumps but prompts for a position to jump to
+ set nomore
+ return "\<CR>:sil se more|norm! \<C-o>\<S-Left>"
+ elseif cmdline =~ '\C^marks'
+ " like :marks but prompts for a mark to jump to
+ return "\<CR>:norm! `"
+ elseif cmdline =~ '\C^undol'
+ " like :undolist but prompts for a change to undo
+ return "\<CR>:u "
+ else
+ return "\<CR>"
+ endif
+endfunction
+cnoremap <expr> <CR> CCR()
+
+
+
+" encryptio) method when using :X
set cm=blowfish2
" Underline the current line, based on its length.