2016-05-13 74 views
4

我发现Vim的快捷键nmap <enter> o<esc>nmap <enter> O<esc>,它用enter键插入一个空行,非常有用。然而,他们对插件造成严重破坏;例如,ag.vim,它使用要跳转到的文件名填充quickfix列表。在这个窗口中按回车(应该跳转到文件)给我错误E21: Cannot make changes; modifiable is off如何检查Vim缓冲区是否可修改?

为了避免在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> 

这工作,但我真正想要的是避免映射任何不可更改缓冲,不只是在quickfix窗口。例如,映射在位置列表中也没有意义(也可能会破坏使用它的其他插件)。如何检查我是否处于可修改的缓冲区中?

回答

5

使用'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> 
6

您可以检查在映射选项modifiablema)。

但是,您不必创建函数并在映射中调用它。该<expr>映射是专为那些使用案例:

nnoremap <expr> <Enter> &ma?"O\<esc>":"\<cr>" 

(以上线并没有进行测试,但我认为它应该去。)

有关详细信息有关<expr> mapping,做:h <expr>

+0

感谢@肯特 - 我接受了其他答案,因为它正好回答了我的问题,但我在.vimrc中使用了你的映射:) – Sasgorilla