17

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?

jinhwan
  • 1,207
  • 1
  • 13
  • 27
  • 2
    The "current" behavior you describe is pretty unusual; I'm trying to figure out how vim would be able to do that. To confirm, your current behavior is that, starting with the cursor before the "t" in line 3, if you press 'k' once, it goes up one screen line to before the "s" in line 2, but if you press it again, it goes up two screen lines to before the "f" in line 1? – David Pope Mar 15 '12 at 12:59

5 Answers5

32

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
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 7
    Just a note that you may want to consider using `:noremap` or `:nnoremap` instead; see here for more info: http://learnvimscriptthehardway.stevelosh.com/chapters/05.html – Travis Northcutt Jun 03 '14 at 00:53
5

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
Rook
  • 60,248
  • 49
  • 165
  • 242
2

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

  • This has the adverse effect of moving it on a line as you do `i`. Use [Ctrl-O](http://vim.wikia.com/wiki/Use_Ctrl-O_instead_of_Esc_in_insert_mode_mappings) – Pod Nov 14 '16 at 15:00
0

This answer is derived from @mario-rossi 's answer (Kudo to him), with minor midification.

I use the normal UP and DOWN arrow key, rather than CTRL+up and CTRL+down. And somehow I need to remove one excess space in the INSERT mode mapping to avoid an off-by-one behavior.

Put the following into your ~/.vimrc:

" When a long line is wrapped, the "gk" and "gj" allow you to move up and down
" a visual line, while normal "k" and "j" move a physical line.
" The following settings map "gk" and "gj" to cursor <up> and <down>.
map <up> gk
map <down> gj
" And the following lines enables same <up> and <down> behavior in INSERT mode
imap <up> <C-[> <up>i
imap <down> <C-[> <down>i
Community
  • 1
  • 1
RayLuo
  • 17,257
  • 6
  • 88
  • 73
0

Took this from vim.fandom.com:

There are several cases to remap up and down movements. First, you probably should remap both k/j and / arrows. Second, you should coose vim modes that requires remap. Basic modes are Normal (nmap), Insert (as after i command, imap) and Select (as after v command, vmap). To remap all three:

nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
nnoremap <Down> gj
nnoremap <Up> gk
vnoremap <Down> gj
vnoremap <Up> gk
inoremap <Down> <C-o>gj
inoremap <Up> <C-o>gk

There's also one significant Operator mode (as in dj or, to say, y4k), remapping operator (omap) breaks vim experience/habits dramatically and not recommended.

Personally I prefer to remap Insert mode only, to keep my editorial habits intact.

FrBrGeorge
  • 610
  • 7
  • 8