Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i intuitively move cursor in vim?(not by line)

Tags:

vim

if some lines are too long, it will be forced to be newlined.

for example, normally a long line will looks like this

1 first line
2 this is the long second line of the file
3 third line.

but, if the window of a vim are too narrow, it will looks like this

1 first line
2 this is the long
  second line of the file
3 third line

the problem arise from this.

let's assume the vim cursor are located at before 't' in 'third line'. if i type 'k', cursor will move to before 's' in 'second line of the file'. after that, if i type 'k' again, cursor will move to 'f' in 'first line'!, not 't' in 'this is the long'. what i want is that the cursor move to 't' in 'this is the long', it is more intuitive process for me. how can set my vim to works like this?

like image 802
jinhwan Avatar asked Mar 15 '12 04:03

jinhwan


People also ask

How do I move my cursor to a specific line?

Jump to a specific line number ↩Press Ctrl+G, or select Goto > Goto Line, to open the Goto Line palette. Type in the number to which you want to jump. Press Enter, & your cursor will now be on that line.

How do you move the cursor by sentences and paragraphs in vim editor?

l. Right, one space. You can also use the cursor arrow keys (←, ↓, ↑, →), + and - to go up and down, or the ENTER and BACKSPACE keys, but they are out of the way. At first, it may seem awkward to use letter keys instead of arrows for cursor movement.

How do I jump to a specific line in vim?

If we are in the vim editor, then simply do this, “Press the ENTER key, write the Line number, and press Shift+ g”: Again the output is the same.


3 Answers

In Vim, the gj and gk commands move by line on the screen rather than by line in the file. This sounds like it probably matches your description.

You can modify your keys like this:

:map j gj
:map k gk
like image 120
Greg Hewgill Avatar answered Oct 04 '22 10:10

Greg Hewgill


No, if some lines are too long and you have set wrap on they will be shown on "two lines", so to say, but there won't be a newline character between them. If you turn off wrap with set nowrap you'll see the effect.

Normally, k and j move you up and down. If you want to navigate wrapped lines use gk or gj, or just as some like it, map it to for example, the cursor keys.

nmap <up> gk
nmap <down> gj
like image 25
Rook Avatar answered Oct 04 '22 10:10

Rook


To move in vim in a natural way is possible.

What I did was, and I suggest you, to modify (or create) your "~/.vimrc" and add these two lines:

map <C-Up> g<Up>
map <C-Down> g<Down>

This will map you control-up and control-down to the movements commands (this is coherent with control-right and control-left to move around long lines)

If you add these other two lines, you can use the same command to move in insertmode:

imap <C-Up> <C-[> g<Up> i
imap <C-Down> <C-[> g<Down> i

(VIM is great !)

Greg Ruo

like image 2
Mario Rossi Avatar answered Oct 04 '22 08:10

Mario Rossi