Thinking in VIM
Braincite
- 4 minutes read - 768 wordsThis is a post, and we’re going to add some text. I’m writing in nvim, which I’m working to get fluent and fast in.
We’re still working in nvim. I have my setup improved and I’m still learning the keyboard shortcuts and vim motions. I’m also thinking about the vim “grammar”, such as [count] <operator> [count] <motion_or_text_object>.
The “Vim grammar” is a powerful concept that allows you to combine simple commands into complex and precise operations. It’s often described as a verb-noun or operator-motion syntax, with an optional count that acts as an adjective.
Here’s the general syntax, broken down by your analogy:
General Syntax:
[count] <operator> [count] <motion_or_text_object>
Let’s break down each part:
Subject (Implicit: “Vim” or “the cursor”): In most Vim commands, the “subject” is implicitly Vim itself, or more specifically, the current cursor position. You’re telling Vim to perform an action from the cursor’s current location on a specific piece of text.
Verb (The
<operator>): This is the action you want to perform. It’s the “doing” word.- Common Operators:
d: deletec: change (delete and then enter Insert mode)y: yank (copy)gU: make uppercasegu: make lowercase>: indent (shift right)<: unindent (shift left)=: format (auto-indent)~: swap case (for a single character or a visual selection)
- Common Operators:
Object (The
<motion_or_text_object>): This defines what the operator acts upon. It specifies the “chunk” of text. This is where Vim’s precision comes into play.- Motions: These specify a direction or destination for the operation. The operation will apply from the current cursor position up to the point specified by the motion.
w: to the beginning of the next worde: to the end of the current word$: to the end of the current line0: to the beginning of the current line}: to the next paragraphG: to the end of the filegg: to the beginning of the filef<char>: find the next occurrence of<char>on the current line (inclusive)t<char>: til the next occurrence of<char>on the current line (exclusive)j: down one linek: up one line
- Text Objects: These define a self-contained unit of text, regardless of the cursor’s exact position within that unit. They are incredibly powerful for structural editing. Text objects always start with
i(for “inner”) ora(for “around”).iw: inner word (the word under or around the cursor, without leading/trailing whitespace)aw: around word (the word under or around the cursor, including leading/trailing whitespace)i(orib: inner bracket/parenthesis (content inside())a(orab: around bracket/parenthesis (content inside()including the())i{oriB: inner Brace (content inside{})a{oraB: around Brace (content inside{}including the{})i": inner quote (content inside"")a": around quote (content inside""including the"")ip: inner paragraphap: around paragraphis: inner sentenceas: around sentenceit: inner tag (for HTML/XML tags)at: around tag
- Motions: These specify a direction or destination for the operation. The operation will apply from the current cursor position up to the point specified by the motion.
Adjective/Qualifier (The
[count]): This is an optional number that can precede either the operator or the motion/text object. It tells Vim to repeat the action or motion that many times.- If it precedes the operator, the entire operation is repeated.
- If it precedes the motion, the motion itself is repeated.
Examples of Vim Grammar in Action:
dw: delete word (from cursor to the end of the word)d2w: delete 2 words (from cursor to the end of the second word)3dw: Repeat “delete word” 3 times (effectively deleting 3 words, identical tod3win this case, but conceptually important for other commands)c$: change to end of line (delete from cursor to end of line, then enter Insert mode)yy: yank yank (a special shorthand fory_orY, which yanks the entire current line)dd: delete delete (a special shorthand ford_orD, which deletes the entire current line)ciw: change inner word (deletes the entire word under the cursor, then enters Insert mode)da}: delete around paragraph (deletes the entire paragraph including surrounding blank lines)yit: yank inner tag (copies the content inside an HTML/XML tag)d/foo: delete until the next occurrence of “foo” (the/initiates a search, which acts as a motion)5gg: Go to line 5. (Here,Gis a motion, and5is a count applied to it.ggis also a motion to go to the first line).
This “grammar” is what makes Vim so efficient and expressive. Instead of memorizing countless specific keybindings for every task, you learn a relatively small set of verbs, nouns (motions and text objects), and a single adjective (count), and then combine them creatively to perform almost any editing operation.