2016-05-13 87 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