0

I usually have j and k bound to gj and gk, respectively for navigating wrapped lines. But this becomes annoying when I have to use the quickfix window, so I thought of using something like :

augroup QuickFix
     au FileType qf unmap j
     au FileType qf unmap k
augroup END

But this undoes the mapping for the main buffer as well, so I tried unmap <buffer> j and unmap <buffer> k instead but this produces an E31: No such mapping error and the gk, gj bindings still seem to persist. The command that I am testing this using is :vimgrep "TODO" %.

0

1 Answer 1

5

Instead of unmap I simply did a buffer-local (:h :map-local) mapping back to the original values...

augroup QuickFix
     au FileType qf map <buffer> j j
     au FileType qf map <buffer> k k
augroup END

Ran vimgrep per your example and in the quickfix window I have j/k behavior while still maintaining gj/gk function in the main window.

Update: Interesting (and somewhat lengthy) side bar regarding the mapping commands used above. @LucHermite asked a valid question in the comments: "Shouldn't [the auto commands use] noremap?" He asked because what happens with map and the other non-noremap mapping commands is that keys on the RHS of a defined mapping retain any mappings they already have. This can actually result in a kind of "infinite loop" where a LHS key triggers a RHS key that is mapped to the LHS key which triggers the mapped keys again and repeat, ad infinitum.

It seems pretty clear then that map j j should result in just such an endless cycle. But it works fine. How? Well, there is a specific exception to the behavior I described which is noted in :h recursive_mapping:

If the {rhs} starts with {lhs}, the first character is not mapped again. For example:

:map ab abcd

will execute the "a" command and insert "bcd" in the text. The "ab" in the {rhs} will not be mapped again.

That being said, I'm normally a stickler for using the noremap commands. I just kind of locked in on the fact that map works correctly and answers OP's question but noremap is better because, at the very least, it's a good idea to get in the habit of using it always unless you know explicitly that you need map.

TL;DR: Though both are valid I recommend using noremap instead of map in the auto commands above.

4
  • Shouldn't it be noremap? Nov 19, 2020 at 10:34
  • 1
    @LucHermitte Check out my (long) update.
    – B Layer
    Nov 19, 2020 at 13:08
  • 1
    Thanks for the update! Nov 19, 2020 at 13:17
  • 1
    @LucHermitte Thanks for motivating me to address it. :)
    – B Layer
    Nov 19, 2020 at 13:49

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.