Navigate Inside Files with Motions

This post extracts some knowledge from Practical Vim Chapter 8 -- Navigate Inside Files with Motions.

[!TIP] Distinguish Between Real Lines and Display Lines

Command Move Cursor
j Down one real line
gj Down one display line
k Up one real line
gk Up one display line
0 To first character of real line
g0 To first character of display line
^ To first nonblank character of real line
g^ To first nonblank character of display line
$ To end of real line
g$ To end of display line

[!TIP] Move Word-Wise

Command Move Cursor
w Forward to start of next word
b Backward to start of current/previous word
e Forward to end of current/next word
ge Backward to end of previous word

W B E gE for WORDS.

A word consists of a sequence of letters, digits, and underscores, or as a sequence of other nonblank characters separated with whitespace.

The definition of a WORD is simpler: it consists of a sequence of nonblank characters separated with whitespace.

Use WORD-wise motions if you want to move faster, and use word-wise motions if you want a more fine-grained traversal.

[!TIP] Find by Character

Command Effect
f{char} Forward to the next occurrence of {char}
F{char} Backward to the previous occurrence of {char}
t{char} Forward to the character before the next occurrence of {char}
T{char} Backward to the character after the previous occurrence of {char}
; Repeat the last character-search command
, Reverse the last character-search command

use f and F in Normal mode, and t and T in Operator-Pending mode.

[!TIP] Search to Navigate

/pattern<CR>

[!TIP] Trace Your Selection with Precision Text Objects

Text Object Selection Text Object Selection
a) or ab A pair of (parentheses) i) or ib Inside of (parentheses)
a} or aB A pair of {braces} i} or iB Inside of {braces}
a] A pair of [brackets] i] Inside of [brackets]
a> A pair of <angle brackets> i> Inside of <angle brackets>
a' A pair of 'single quotes' i' Inside of 'single quotes'
a" A pair of "double quotes" i" Inside of "double quotes"
a` A pair of `backticks` i` Inside of `backticks`
at A pair of <xml>tags</xml> it Inside of <xml>tags</xml>

[!TIP] Delete Around, or Change Inside

很多内置插件都采用 package 机制管理,matchit 通常被放在 opt 目录下,因此默认不会自动加载,需要手动:

vim  | Keystrokes | Current content | Keystrokes | Current content | |------------|-----------------|------------|-----------------| | iw | word | aw | word plus space(s) | | iW | WORD | aW | WORD plus space(s) | | is | sentence | as | sentence plus space(s) | | ip | paragraph | ap | paragraph plus blank line(s) |

As a general rule, we could say that the d{motion} command tends to work well with aw, as, and ap, whereas the c{motion} command works better with iw and similar.

[!TIP] Mark Your Palce and Snap Back to It

The m{a-zA-Z} command marks the current cursor location with the designated letter.

'{mark} moves to the line where a mark was set, positioning the cursor on the first non-whitespace cahracter.

`{mark} command moves the cursor to the exact position where a mark was set, restoring the line and the column at once.

Automatic Marks as follows: | Keystrokes | Buffer Contents | |------------|-----------------| | `` | Position before the last jump within current file | | . `` | Location of last change | | ``^ | Location of last insertion | | [ `` | Start of last change or yank | | ``] | End of last change or yank | | < `` | Start of last visual selection | | ``> `` | End of last visual selection |


原文链接:https://zhuang.dev/reading/practical-vim/08-navigate-inside-files-with-motions/

posted @ 2026-07-12 22:50  neozhuang  阅读(3)  评论(0)    收藏  举报