Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim moving with hjkl in long lines (screen lines)

Tags:

vim

enter image description here

The highlighted text is the array in which I want to move. I have to press g before pressing j to move a line down. Is there some mapping for my .vimrc that I can just use hjkl to move in screen lines without pressing g every time.

Thanks for your help Matthias

like image 654
Matthias Guenther Avatar asked Feb 09 '11 14:02

Matthias Guenther


3 Answers

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 :

like image 119
Andrea Spadaccini Avatar answered Oct 06 '22 00:10

Andrea Spadaccini


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("$")
like image 33
Rick Avatar answered Oct 05 '22 23:10

Rick


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
like image 26
Ned Batchelder Avatar answered Oct 05 '22 23:10

Ned Batchelder