Currently, my vimrc contains the lines

nnoremap j gj
xnoremap j gj
nnoremap gj j
xnoremap gj j
nnoremap k gk
xnoremap k gk
nnoremap gk k
xnoremap gk k

This way, the j key in both visual and normal mode will go to the next displayed line, not the next actual line. However, this is rather silly in visual line mode, since, well, it only operates on actual lines. It doesn't make much sense in visual block mode either.

How can I make a mapping apply only in visual mode, not visual line mode or visual block mode?

1 Answer 1


You could rewrite your mappings as "expression mappings":

xnoremap <expr> j  mode() ==# "v" ? "gj" : "j"
xnoremap <expr> gj mode() ==# "v" ? "j"  : "j"
xnoremap <expr> k  mode() ==# "v" ? "gk" : "k"
xnoremap <expr> gk mode() ==# "v" ? "k"  : "gk"

See :help <expr> and :help mode().

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.