Vim plugin to add mappings only when shell prompt is empty
- Vim 8.2 or higher
After installing this plugin, write this to your vimrc.
function! s:empty_prompt_mappings() abort
" If current line is empty prompt ...
" : works as <C-w>:
call empty_prompt#map(#{lhs: ':', rhs: '<C-w>:'})
" <Esc> works as <C-w>N
call empty_prompt#map(#{lhs: '<Esc>', rhs: '<C-w>N'})
" ... Add more mappings you like
endfunction
autocmd VimEnter * ++once call s:empty_prompt_mappings()
g:empty_prompt#pattern
(default: &shell =~# 'sh$' ? '\$ $' : '>\s*$'
)
If current line on terminal window matches this pattern, fire given mapping.
Default is:
- current line ends with
$
(&shell
ends withsh
(bash/zsh/...) like Unix environment) - current line ends with
>
(otherwise, cmd.exe/powershell like Windows)
If the default value does not fit your environment, please consider customize the value.
call empty_prompt#map(#{lhs: '<Esc>', rhs: "<C-w>N"})
This will be expanded to:
tnoremap <expr> {lhs} empty_prompt#is_empty() ? {rhs} : {fallback}
{fallback}
is optional.
Default {fallback}
is same as {lhs}