>
> At the bash prompt, I often use the [Alt+.]¹ keyboard action to retrieve
> the argument of a prior command from the bash history list.
>
> To illustrate:
>
> | $ mkdir -p long/directory/name/I/would/rather/not/type/again
> | $ cd [Alt+.]
>
> Bash expands the [Alt+.] giving me:
>
> | $ cd long/directory/name/I/would/rather/not/type/again
>
> So I just have to hit enter.
>
> Naturally, this is not limited to the last command: I can hit [Alt+.]
> twice to retrieve the argument of the last-but-one command.. etc.
>
> I'm used to doing this in bash and I find it a lot more effective than
> retrieving entire commands and editing them (in this case, retrieving
> the mkdir command & replacing 'mkdir -p' by 'cd').
>
> Is there any way I could do this in Vim's ex-mode?
This might be an overkill, and perhaps dangerous, but it seems to
work:
let s:CmdHistory = []
let s:CmdCount = -1
function! ResetCmd()
let s:CmdCount = len(s:CmdHistory) - 1
return ""
endfunction
function! StoreCmd()
let cmd = matchstr(getcmdline(), '\s.\+')
call add(s:CmdHistory, cmd)
let s:CmdCount = len(s:CmdHistory) - 1
return getcmdline()
endfunction
function! GetCmd()
if s:CmdCount >= 0
let cmd = matchstr(getcmdline(), '^\S*') . s:CmdHistory[s:CmdCount]
let s:CmdCount = s:CmdCount - 1
return cmd
else
return getcmdline()
endif
endfunction
cnoremap <buffer> <CR> <C-\>eStoreCmd()<CR><CR>
cnoremap <buffer> <Esc> <C-\>eResetCmd()<CR><Esc>
cnoremap <buffer> <A-.> <C-\>eGetCmd()<CR>
Also, I'm not very fluent in Vimscript (I tend to script in Lua with Vim
functions simply as wrappers), so this might be improvable.
Best,
Paul
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
No comments:
Post a Comment