我发现Vim快捷方式nmap <enter> o<esc>或者nmap <enter> O<esc>用enter键插入一个空行非常有用.但是,它们会对插件造成严重破坏; 例如,ag.vim它使用要跳转到的文件名填充quickfix列表.在此窗口中按Enter键(应该跳转到文件)会给出错误E21: Cannot make changes; modifiable is off.
为了避免在quickfix缓冲区中应用映射,我可以这样做:
" insert blank lines with <enter>
function! NewlineWithEnter()
if &buftype ==# 'quickfix'
execute "normal! \<CR>"
else
execute "normal! O\<esc>"
endif
endfunction
nnoremap <CR> :call NewlineWithEnter()<CR>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我真正想要的是避免任何不可修改的缓冲区中的映射,而不仅仅是在quickfix窗口中.例如,映射在位置列表中也没有意义(并且可能会破坏使用它的其他一些插件).如何检查我是否在可修改的缓冲区中?
你可以检查你的映射中的选项modifiable(ma).
但是,您不必创建函数并在映射中调用它.该<expr>映射是专为那些使用案例:
nnoremap <expr> <Enter> &ma?"O\<esc>":"\<cr>"
Run Code Online (Sandbox Code Playgroud)
(上面一行没有经过测试,但我认为应该去.)
有关的详细信息<expr> mapping,请执行:h <expr>
用途'modifiable':
" insert blank lines with <enter>
function! NewlineWithEnter()
if !&modifiable
execute "normal! \<CR>"
else
execute "normal! O\<esc>"
endif
endfunction
nnoremap <CR> :call NewlineWithEnter()<CR>
Run Code Online (Sandbox Code Playgroud)