CAVP33
CAVP33

Reputation: 85

Vim: Make cursor move up/down but stay in the same line

Is there a way to make the vim cursor move in lines vertically? Here's an example:

enter image description here

So the cursor is at the word nisi, and I want to move to Pellentesque. I came from Sublime, so my instinct is to press down (in this case j) but it jumps to line 2. Is there way to configure that by pressing j I move down but got to Pellentesque and not line 2? I can only move to it the normal way by moving horizontally (l,w,e etc...).

Upvotes: 3

Views: 1371

Answers (2)

Hi computer
Hi computer

Reputation: 1121

gj is the command. You can remap j to gj to have its behavior by default.

here is some of the mapppings that I use and find useful(including the answer one), and they are simple, not depend on any functions or packages

map j gj
map k gk
map Y y$ # Yank to the end of line
nnoremap L Lzz<CR> # go to bottom of screen and center on that line
nnoremap H Hzz<CR> # go to top of screen and center on that line
inoremap jk <esc> # just type jk to exit inserting mode
inoremap <esc> <nop> # don`t do anything in insert mode
vnoremap v <Esc> #  Quit visual mode
nnoremap U <C-r> # Redo
nnoremap n nzz # search and center
nnoremap N Nzz # search and center
nnoremap * *zz # search and center
nnoremap # #zz # search and center
nnoremap g* g*zz # search and center

Upvotes: 5

Cemal Okten
Cemal Okten

Reputation: 829

VIM makes a distinction between 'real lines' and 'display lines'.

By default the j and k commands move up and down 'real lines'.

The line numbers in your screenshot demonstrate that VIM count's 3 'real lines'.

To switch to using 'display lines' instead, which will enable you to move up and down each individual line you can remap the j and k keys to gj and gk which is the default way of moving up and down 'display lines'.

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

Upvotes: 2

Related Questions