簡體   English   中英

如何檢查Vim緩沖區是否可修改?

[英]How can I check if a Vim buffer is modifiable?

我發現Vim快捷方式nmap <enter> o<esc>nmap <enter> O<esc> ,它使用回車鍵插入一個空行,非常有用。 但是,它們會對插件造成嚴重破壞; 例如, ag.vim ,它使用要跳轉到的文件名填充quickfix列表。 在此窗口中按Enter鍵(應該跳轉到文件)會給出錯誤E21: Cannot make changes; modifiable is off 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>

這是有效的,但我真正想要的是避免任何不可修改的緩沖區中的映射,而不僅僅是在quickfix窗口中。 例如,映射在位置列表中也沒有意義(並且可能會破壞使用它的其他一些插件)。 如何檢查我是否在可修改的緩沖區中?

你可以在你的映射中檢查選項modifiablema )。

但是,您不必創建函數並在映射中調用它。 <expr>映射是為這些用例設計的:

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

(上面一行沒有經過測試,但我認為應該去。)

有關<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM