The highlighted text is the array in which I want to move. I have to press g b
This is a great idea for when editing long text files, like latex documents. But it's not such a good idea when editing code, as it can screw up some macros (as jelera notes on Ned's answer). My compromise is to only enable it when in insert (and replace) modes, and not for anything else (I'm not aware of any macros that I use in insert mode that use navigation keys):
" move on soft lines in insert mode
inoremap <Down> <C-o>g<Down>
inoremap <Up> <C-o>g<Up>
(adapted from this answer)
You can simply remap j and k (for example) to gj and gk:
" map j to gj and k to gk, so line navigation ignores line wrap
nmap j gj
nmap k gk
You can remap the j
and k
keys (not sure if you really need h
and l
..)
:map j gj
:map k gk
Once you tried and liked them, add them to your .vimrc
without the leading :
I use the following snippet that helps with all forms of navigating, including things like $ to end of line and such.
" mapping to make movements operate on 1 screen line in wrap mode
function! ScreenMovement(movement)
if &wrap
return "g" . a:movement
else
return a:movement
endif
endfunction
onoremap <silent> <expr> j ScreenMovement("j")
onoremap <silent> <expr> k ScreenMovement("k")
onoremap <silent> <expr> 0 ScreenMovement("0")
onoremap <silent> <expr> ^ ScreenMovement("^")
onoremap <silent> <expr> $ ScreenMovement("$")
nnoremap <silent> <expr> j ScreenMovement("j")
nnoremap <silent> <expr> k ScreenMovement("k")
nnoremap <silent> <expr> 0 ScreenMovement("0")
nnoremap <silent> <expr> ^ ScreenMovement("^")
nnoremap <silent> <expr> $ ScreenMovement("$")