As the title stated, remap ':' to maintain last command, and erase all char if any key other than <CR>
was pressed.
possible use cases:
- I have typed a long command, and I may or may not modified it, then re-run it.
- I save a lot, so I want type
:
, then visually verify:w
is there, then type<CR>
to proceed. -- I have remapped:saveas<CR>
to<Leader>s
.
things that are not what I pursue:
- I know about
q:
or<C-f>
in cmdline mode to open the mini buffer to edit command. - I know type
@:
then@@
to repeat command. - I know type
<C-p>
or<Up>
in cmdline mode.
a little more on motivation:
Repeat with register/macro
is great and is really handy for some task.
But here I want a more consistent and safer (visually verification/feedback) way to re-run last command, with fewer key stroke.
Is that possible? or not trivially implementable at all??
======== EDIT ========
Based on @Karl's comment, I think the problem could be implemented via a function definition and a nnoremap
. The function consist three parts:
echo last command
getreg(':')
, then getchar
withgetchar
if
char == '^M'
, re-execute last command.otherwise, open command line and act accordingly.
My first attempt is as below: -- (based on @grodzik's answer and @Karl's idea in the comment)
func! Show_last_command_and_exec()
echo ':'.getreg(':')
try
let char = getchar()
catch /^Vim:Interrupt$/
let char = "\<Esc>"
endtry
if char == "^M"
exec getreg(':')
" not work, don't know why, tried with previous comand be `:pwd`
else
" not work, don't know which function or command should be used.
" below are failed attempt.
" normal! :
" call feedkeys(char)
" exec ':'.char
endif
endfunc
nnore : :call Show_last_command_and_exec()<CR>
But unfortunately, except the trivial first echo part, neither work yet. I post my attempt (though failed currently) here, hope someone interested may implement a better one that work nicely.
P.S. as @grodzik said in his answer, the "^M" should be replaced with real <CR>
sign.
evil-mode
ofemacs
, and thought, cool, how can I re-implement it withvimscript
so I can use it invim
.nnoremap : :<C-p>
. Not sure about erasing it when pressing something else than<CR>
<C-u>
to erase whole line in Ex-mode? I know it's extra key press, but auto erasing seems impossible.<C-p>
,<C-u>
and<C-f>
in cmdline mode.