How to check if Vim buffer can be modified?

I found Vim shortcuts nmap <enter> o<esc>or nmap <enter> o<esc>that insert an empty line with an input key are very useful. However, they damage the plugins; for example, ag.vimwhich populates the quickfix list with filenames for the jump. Pressing the input in this window (which should go to the file) gives me an error E21: Cannot make changes; modifiable is off.

To avoid applying matching in the quick fix buffer, I can do this:

" 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>

This works, but I really want to avoid displaying in any unmodifiable buffer, and not just in the quickfix window. For example, displaying does not make sense in the list of locations (and may violate another plugin that uses it). How can I check if I am in a mutable buffer?

+4
2

'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>
+5

modifiable (ma) .

. <expr> :

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

( , , .)

<expr> mapping :h <expr>

+6

All Articles