19

For some text formats, line breaks matter.

For example, I often use Vim to write MediaWiki documents, where you can't just put a linebreak anywhere (unlike Markdown). So I'm forced to write lines with a lot of characters.

This can lead to long (200+ chars) or even very long (1000+ chars) lines. I find this very awkward to use in Vim. One example is if I want to edit something in the middle of the 1000 character-line, I need to move my cursor 500 times.

This can be made a bit faster with w or /, but it's still awkward.

Are there better ways to deal with this? Better movement keys? Some sort of "fake wrapping" (text behaves as if tw=80, but no \n in the file), or something else?

20

You can use g series of commands to move to the boundaries of the visible screen area. For example, g$ moves to the right edge of the screen (which is not necessarily the end of the line). gj moves the cursor down one line as it appears on your screen (which is not necessarily one logical line down).

Perhaps you could rebind the arrow keys:

nnoremap <Up> gk
nnoremap <Down> gj

Or some people also directly rebind k and j:

nnoremap k gk
nnoremap j gj

For insert mode, you could use:

inoremap <C-k> <C-o>gk
inoremap <C-j> <C-o>gj

Or:

inoremap <Up> <C-o>gk
inoremap <Down> <C-o>gj

In addition, if you use :set wrap, Vim will wrap the lines, so you can see all of the line. You can also use set showsbreak=+ to show a + to indicate that Vim is doing wrapping.


To jump to specific column positions, you can use the | command. For example, 200| will go to column position 200.

  • 1
    ... and g5j to jump 5 down, as usual. – yo' Feb 3 '15 at 18:53
  • 1
    @yo' I think you mean 5gj – Martin Tournoij Feb 5 '15 at 17:23
  • 1
    @Carpetsmoker Strange; with most commands it doesn't matter in which order you give the prefixes and the numbers, as long as the command itself is last. Here it does not :-/ – yo' Feb 6 '15 at 9:50
  • @yo', well, in g5j, g5 is seen first, which just jumps to the next 5 before you apply the j. That's why it can't do what 5gj does. – Shahbaz Mar 2 '15 at 14:17
3

Vim has support for soft wrapping, just set wrap to enable it and it will wrap long lines going beyond the visible screen. You can use gj & gk to move up/down respectively over such wrapped lines.

2

When I'm dealing with very long lines, the biggest pain point has to do with differing line lengths. That is, moving up or down near the ends of the lines scrolls the window very far left or right.

This can be avoided by setting virtualedit=all, which allows you to move the cursor into the space long after the line endings.

1

As others have said, you can use g + something to move around. However, when you have extremely long lines, pressing gj multiple times to move downward is not ergonomic. I have the following in my .vimrc to make that much easier:

vmap <D-j> gj
vmap <D-k> gk
vmap <D-4> g$
vmap <D-6> g^
vmap <D-0> g^
nmap <D-j> gj
nmap <D-k> gk
nmap <D-4> g$
nmap <D-6> g^
nmap <D-0> g^

On a Mac, this allows me to use j, k, $, 0 and ^ on display lines by holding down the command key. On other systems, instead of D you could try M or A for Alt, or C for Control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.