If you simply want to recall the last text given as an argument to input()
, you could try the following mapping:
cnoremap <expr> <Up> getcmdtype() ==# '@' ? '<C-E><C-U>' . histget('@') : '<Up>'
Which can be broken down like this:
<expr>
: type the evaluation of an expression
getcmdtype() ==# '@'
: check whether you are on the input line
'<C-E><C-U>' . histget('@')
: if the previous test succeeded, hit <C-E><C-U>
to clear the input line, then type the last entry from the input history (histget('@')
)
'<Up>'
: otherwise, if you are somewhere else (command-line, expression register, search ...), simply hit <Up>
If you want to navigate across all the entries of the input history, you could try the following code:
let s:input_hist_index=0
function! s:InputHistory(dir) abort
let s:input_hist_index += a:dir
return "\<C-E>\<C-U>" . histget('@', s:input_hist_index)
endfunction
cnoremap <expr> <Up> getcmdtype() ==# '@' ? <SID>InputHistory(-1) : '<Up>'
cnoremap <expr> <Down> getcmdtype() ==# '@' ? <SID>InputHistory(1) : '<Down>'
It's similar to the previous mapping, except it must track where you are in the input history, hence the script-local variable s:input_hist_index
. There may be another way of doing it, maybe a native function, but I didn't find one in :h history-functions
.
You can get more information with the following help topics:
:h <expr>
:h getcmdtype()
:h histget()
:h c_ctrl-e
:h c_ctrl-u
:h c_CTRL-F