Last active
October 29, 2022 19:26
-
-
Save romainl/5b2cfb2b81f02d44e1d90b74ef555e31 to your computer and use it in GitHub Desktop.
Make various list-like commands more intuitive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Background here: https://gist.github.com/romainl/047aca21e338df7ccf771f96858edb86 | |
" with help from https://github.com/teoljungberg | |
function! CCR() | |
let cmdline = getcmdline() | |
let filter_stub = '\v\C^((filt|filte|filter) .+ )*' | |
command! -bar Z silent set more|delcommand Z | |
if getcmdtype() !~ ':' | |
return "\<CR>" | |
endif | |
if cmdline =~ filter_stub . '(ls|files|buffers)$' | |
" like :ls but prompts for a buffer command | |
return "\<CR>:b" | |
elseif cmdline =~ '\v\C/(#|nu|num|numb|numbe|number|l|li|lis|list)$' | |
" like :g//# but prompts for a command | |
return "\<CR>:" | |
elseif cmdline =~ filter_stub . '(\%)*(#|nu|num|numb|numbe|number|l|li|lis|list)$' | |
" 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 =~ filter_stub . '(cli)' | |
" like :clist or :llist but prompts for an error/location number | |
return "\<CR>:sil cc\<Space>" | |
elseif cmdline =~ filter_stub . '(lli)' | |
" like :clist or :llist but prompts for an error/location number | |
return "\<CR>:sil ll\<Space>" | |
elseif cmdline =~ filter_stub . 'old' | |
" like :oldfiles but prompts for an old file to edit | |
set nomore | |
return "\<CR>:Z|e #<" | |
elseif cmdline =~ filter_stub . 'changes' | |
" like :changes but prompts for a change to jump to | |
set nomore | |
return "\<CR>:Z|norm! g;\<S-Left>" | |
elseif cmdline =~ filter_stub . 'ju' | |
" like :jumps but prompts for a position to jump to | |
set nomore | |
return "\<CR>:Z|norm! \<C-o>\<S-Left>" | |
elseif cmdline =~ filter_stub . 'marks' | |
" like :marks but prompts for a mark to jump to | |
return "\<CR>:norm! `" | |
elseif cmdline =~ '\v\C^undol' | |
" like :undolist but prompts for a change to undo | |
return "\<CR>:u " | |
elseif cmdline =~ '\v\C^tabs' | |
set nomore | |
return "\<CR>:Z| tabnext\<S-Left>" | |
elseif cmdline =~ '^\k\+$' | |
" handle cabbrevs gracefully | |
" https://www.reddit.com/r/vim/comments/jgyqhl/nightly_hack_for_vimmers/ | |
return "\<C-]>\<CR>" | |
else | |
return "\<CR>" | |
endif | |
endfunction | |
cnoremap <expr> <CR> CCR() |
@craigmac :#
is a synonym of :number
so it has to be taken into account. As of now, line 14 is triggered by commands like :g/foo/#
, which I use quite often.
The pattern could definitely be improved or I could !!
a second pattern.
@craigmac looking at it again, :buffer #<CR>
and :edit #<CR>
seem to work fine on my end.
- The pattern on line 14 has a
/
before the#
so it shouldn't match against:<command> #
. - The pattern on line 17 starts with
\v\C^((filt|filte|filter) .+ )*
so it shouldn't match either.
Do you use the latest version of this gist?
@romainl I re-copied and tried it out, only to realize I had my own version of the command overriding this one (which had a syntax error causing my bug). Apologies, it works fine as is!
@craigmac No problem!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another little annoyance: if you use alternate file
#
in, for example,:buffer #<CR>
it switches the buffer then puts:
on the command-line, which is not what we want. Same thing happens with:edit #<CR>
. I see that the issue is one condition which checks for#|num|...
and returns"\<CR>:"
. I'm trying to think why the#
is checked for there, and if it breaks any cases if I remove it. On further examination, is that line missing a^
so that it wouldn't pick up a#
in the case of:buffer #<CR>
?