6

I like to replace the default enter/return key functionality with actually inserting a new line, and the same with space (but inserting a line above), e.g. here is my map:

nnoremap <CR> O<ESC>
nnoremap <space> o<ESC>

But the problem I have here is that it breaks selecting functionality. For example Omnisharp and Flake8 both bring up a new buffer which allows you to select errors, or browse found items. For example Flake8 lists any errors that it finds with the file, then you can select an item and press enter to navigate to that error. But if you do this with the enter/return key remapped then instead it will give the error:

E21: Cannot make changes, 'modifiable' is off

Unfortunately I can't find the default mapping for enter/return. Is there any other way to handle this? Or is there a way to change mapping based on what buffer you're currently in, as unmapping the enter/return key once mapped will restore this functionality. But any autocmd solutions with buffers I have came up with are poor and not very reliable.

1 Answer 1

8

I guess those plugins are using the quickfix-window; if there were using scratch buffers, they should set up buffer-local mappings that would override your global ones.

As Vim treats the quickfix buffer just like any other, your global mappings apply there as well. But fortunately, there's an easy fix: You just need to provide :help :map-<buffer> mappings that restore the original commands. As Vim automatically sets a filetype (cp. :help ft-qf-plugin), you can place those into ~/.vim/ftplugin/qf.vim (as long as you have :filetype plugin on, which most people have), or prepend :autocmd FileType qf to each line and put them into your ~/.vimrc:

" Restore original selection functionality in quickfix windows.
nnoremap <buffer> <CR> <CR>
nnoremap <buffer> <Space> <Space>
3
  • In my situation, I use <ENTER> in normal-mode to move the line under the cursor down. This is a generic/global functionality. Is there a way to specify bindings for the quickfix window?... something like a ~filetype that tags quickfix? Thanks in advance. - E Nov 17, 2020 at 16:50
  • @Edmund'sEcho: Sure, the approach in my answer does not only apply to the special case of clearing a global mapping; you can use :nmap <buffer> ... in a ftplugin/qf.vim script to define mappings that are local to a quickfix window as well. Nov 18, 2020 at 17:15
  • Thank you for your response. I recently figured out that the quickfix buffer has its own filetype qf. With that, I can easily apply your solution. Thank you! - E Nov 18, 2020 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.