I've remapped <cr> (enter) to za so I can quickly open/close folds.

However, now when I use the quickfix window and hit enter it no longer jumps to the line with the error (because it's trying to open/close a fold that does not exist in the quick fix window).

Is there a way to disable my mapping or re-set it locally when entering a quickfix window?

1 Answer 1


Using an expression mapping is the easiest way to do this:

nnoremap <expr> <CR> &buftype ==# 'quickfix' ? "\<CR>" : 'za'

This defines a mapping that checks whether you are in the quickfix window, and then performs either a <CR> or za accordingly.


  • :h :map-expr
  • :h 'buftype'
  • :h expr1
  • :h string

Mapping to a function

You can also define a function that carries out a regular enter when invoked from the quickfix window, and does 'za' otherwise. This isn't efficient for what you're trying to do, but I'm leaving it here because it's useful info for if you want to map to something more complicated than a normal mode command:

function! FoldingEnter()
  if &buftype ==# 'quickfix'
    execute "normal! \<CR>"
    normal! za

nnoremap <CR> :call FoldingEnter()<CR>
  • Can I ask for why we must use \< in "<CR>" here?
    – WW00WW
    Commented Feb 26, 2019 at 12:46
  • 3
    @Voldikss We want the string to contain a single carriage return character. There are two ways to achieve this: either we can press <C-V><C-M> to enter the carriage return literally, but that displays as ^M and can't be easily copy/pasted outside of Vim, or we can use Vim's string constant notation. (See :help expr-string.) If we didn't include the backslash in "<CR>" then the string would instead contain the series of four characters: less-than, uppercase C, uppercase R, greater-than.
    – Rich
    Commented Feb 26, 2019 at 13:13
  • 2
    Thanks man, I understand it now. :help expr-string is important.
    – WW00WW
    Commented Feb 26, 2019 at 14:27

Your Answer

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

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