Vim: move around quickly inside of long line
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
- You can use
$
,0
, and^
to move to line endpoints and then usew
andb
. Also, adding a numeric argument tow
andb
can accelerate the process, so using6w
instead of justw
can put you about to where ou need to be. - Using
f
andt
to move to individual characters will help also. (I use this typically with punctuation. If, for example, I have four sentences on one long line2f.
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 withJ
. - Something I just discovered, you can move up and down one displayed line by using
gj
andgk
. 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.
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.
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
. :)
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.
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^
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.
You can also move around with W
B
that will skip to the next space :)
G
moves to the end of the document
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
精彩评论