Vim: move around quickly inside of long line

Vim

Vim Problem Overview


I have word-wrap enabled and tend to have quite long lines. But moving around inside a line that's actually 4 lines high with "w" is cumbersome. I keep using / to jump to the word I'm looking for, but that seems overdoing it a bit.

Any hints on how to move more quickly inside of a line?

Thanks,

MrB

Vim Solutions


Solution 1 - Vim

  • You can use $, 0, and ^ to move to line endpoints and then use w and b. Also, adding a numeric argument to w and b can accelerate the process, so using 6w instead of just w can put you about to where ou need to be.
  • Using f and t to move to individual characters will help also. (I use this typically with punctuation. If, for example, I have four sentences on one long line 2f. will go to the end of the second sentence)
  • Using the ( and ) keys are an alternative way to navigate entire sentences.
  • Splitting out long lines into multiple lines (manually, or with set tw=72 [or 80]) can make editing them simpler. You can always join them later with J.
  • Something I just discovered, you can move up and down one displayed line by using gj and gk. That way, you can treat your one wrapped line as multiple lines.

If you comment on the type of data you're editing, it might make it easier for us to make suggestions.

Solution 2 - Vim

I think you can benefit from gk and gj instead of just k and j.

Also look at 'virtualedit' for some options that allow you to cursor through 'void' areas without flicking the cursor to the next best physical character.

You might want to (temporarily)

nnoremap <buffer> k gk
nnoremap <buffer> j gj

Leave out the <buffer> part to apply this globally.

Solution 3 - Vim

You can use ( and ) to navigate by sentence; it just looks for ., but that can be immensely helpful, especially if you don't like the sentence and want to change it: (c) will jump to the beginning of the current sentence, then change the entire sentence.

You can also use w and e, with count modifiers, to move words. 3w will move three words at a time.

You can also use f and F to search forward and backwards for a specific character. This is much more useful if you're looking for the word quite or syzygy than the. :)

Solution 4 - Vim

My preferred strategy while jumping around long lines is to use f F and t T to zero in on the character. What makes this family of motions supercharged is that you can utilize the ; and , motions, so you don't have to count the position of character relative to cursor, but just step through them (extremely useful with ' " . etc)

Let's say we have a line:

reallyLongObjectName.longMethod().prettyPrettyLongMethod().burp();

If we need to jump to, say, the third dot from the beginning of the line, we can use either 3f. or f.;; visiting two dots and landing on third.

While the ; , style can use more keystrokes, I found it more agile and fun overall.

Solution 5 - Vim

If you choose to go the route of remapping these:

nnoremap k gk
nnoremap j gj

here are a couple others along the same lines:

nnoremap 0 g0
nnoremap $ g$
nnoremap ^ g^

Solution 6 - Vim

I recently started using a plugin that I find really nice to move very quickly inside a line (or the hole file).

The plugin's name is PreciseJump and you can find it here.

When you use this plugin it defines to mappings _f and _F.

if you type _f followed by x it will highlight all x characters and will replace temporarily with other characters that you can press to jump to that location. Check the script page for an illustration.

Solution 7 - Vim

You can also move around with W B that will skip to the next space :) G moves to the end of the document

Solution 8 - Vim

Please notice that using "g" followed by Up or Down arrows indeed works fine, but if you have long lines and move quickly you may enter "gg" by mistake and end-up at the top of the text...! (Undo will not bring you back, and AFAIK there is not one-key-pressed way to go back to where you were.)

It happened to me too many times.

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. Will make mistyping "gg" impossible and is perfectly 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 in insert mode (!)

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

(VIM is great !)

Greg Ruo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMrBubblesView Question on Stackoverflow
Solution 1 - VimyanView Answer on Stackoverflow
Solution 2 - VimseheView Answer on Stackoverflow
Solution 3 - VimsarnoldView Answer on Stackoverflow
Solution 4 - VimdnetsView Answer on Stackoverflow
Solution 5 - VimNick KnowlsonView Answer on Stackoverflow
Solution 6 - VimskeeptView Answer on Stackoverflow
Solution 7 - VimMichael BallentView Answer on Stackoverflow
Solution 8 - VimMario RossiView Answer on Stackoverflow