First, I'm not sure just mapping j
to gj
always is a good idea, because, for instance, I don't want j
to behave as gj
when wrap
is off and the line is longer than the screen.
How can I make 0, j, k, ^, $ behave like g0, gj, gk, g^, g$ when wrap
is set? What is the proper way to do so?
I thought a good starting point could be
function! s:setDisplayLinesMovementWhenWrapOn()
if &wrap
nnoremap <buffer> j gj
nnoremap <buffer> k gk
nnoremap <buffer> 0 g0
nnoremap <buffer> ^ g^
nnoremap <buffer> $ g$
else
nunmap <buffer> j
nunmap <buffer> k
nunmap <buffer> 0
nunmap <buffer> ^
nunmap <buffer> $
endif
endfunction
augroup DisplayLinesMovementWhenWrapOn
autocmd!
autocmd OptionSet wrap call s:setDisplayLinesMovementWhenWrapOn()
augroup END
But probably I also need another autocmd
to call s:setDisplayLinesMovementWhenWrapOn
when a buffer is read? Or a file? Or what?