Want to see the full-length video right now for free?
Sign In with GitHub for Free AccessWe recommend going through our Onramp to Vim course on Vim's command language before this video, so that you're comfortable with the topics discussed in this video.
Now let's build on the ideas from Onramp to Vim to use them to even better effect.
The Vim language is the unique structure of key mappings in Vim such that we can
almost treat it like a language. For example, vip
breaks down into:
v
: visually selecti
: innerp
: paragraph (Vim calls this a "text object")So vip
is "visually select inner paragraph". Since we know ip
, we can use it with
other verbs like d
("delete"), and we know what e.g. dip
will do: delete inner
paragraph. We can expand this to other verbs and nouns, like ciw
:
c
: changei
: innerw
: wordWe can use .
to repeat this action as well, and Vim will "change a word" on
any word the cursor is on. It works with any word, not just the original word we
typed ciw
on. We can compose our changes almost like a sentence.
Sometimes we find that we don't have the right verb or noun for a change that we want to make. Luckily, Vim has a plugin system!
The Commentary Vim plugin gives us a new verb that comments things out. Chris
has it mapped to cm
, so we can do things like cmip
to comment out a
paragraph. Commentary is also filetype-aware, so it will use the correct comment
character for the current file you're editing.
Exchange is for exchanging words, or any other text object. Its mapping is
cx
, so cxiw
will exchange a word, cxip
will exchange a paragraph, and so
on. Exchange gives us a new verb that works with all of our adjectives (like
"inner") and nouns (like "word" or "paragraph").
ReplaceWithRegister gives us the ability to replace the text under the
cursor with yanked text. If we yank "before_save", then put our cursor on
another word and type griw
("go replace inner word"), this plugin will replace
the word under the cursor with "before_save". Just like all of our other
operators, we can repeat this using .
.
Even better, we can use this with any register, and there's a special register that points to the system clipboard. Using this knowledge and ReplaceWithRegister, we can paste over words (or sentences, or paragraphs) with the contents of the system clipboard using this mapping:
" Note, this must be nmap, not nnoremap
nmap <leader>gr "*gr
(You can install this plugin with the vim-scripts GitHub mirror.)
We covered Surround in our video on Vim plugins, but we use
it so much that we're talking about it again. surround.vim gives us some very
handy operators for changing what surrounds a text object. For example, given
name
, we can change it to "name"
by putting our cursor on the word and
typing ysiw"
, which means "surround inner word with double quotes".
We can change the surrounding punctuation by putting our cursor on "name"
and
typing csiw'
, which means "change surroundings of inner word to double quotes".
sort-motion gives you a verb that you can use to sort a range of lines
easily with any line-based text object or motion. Its verb is gs
("go sort"),
so gs2j
will sort the next two lines.
titlecase title-cases a sentence. It's quite handy in prose documents (like
Markdown). Its mapping is gt
("go titlecase"), so gti"
will titlecase words
inside double quotes.
system-copy gives you mappings for copying to and pasting from the system clipboard. Some people connect their Vim clipboard to the system clipboard directly, but that means copying any text outside of Vim overwrites any text you've yanked in Vim, and vice versa. Instead of each one overwriting the other, system-copy makes the system clipboard readily available to Vim while keeping them separate.
text-obj-user is foundational; end-users don't use it directly, but many other plugins require it and you'll need to have it installed to use the plugins below.
entire allows us to operate on the entire document. Its text object is
called ae
, so we could do yae
to yank the entire document, or cpae
(combined with system-copy, above) to copy the entire document.
indent text object lets us operate on text that's at the same indentation
level. We can do gsii
to go sort (gs
) inner (i
) indent (i
).
line lets us operate on a line, ignoring leading whitespace. yil
will yank
inner line (only the part that has the dashed line over it):
---------
gem "hey"
We can also use gril
to replace the current line with the text we've yanked.
For more text objects you can check out the text-object wiki. The key is to embrace the idea that Vim has a language of verbs and nouns. You should learn the built-in ones first, but you'll hit a wall where Vim doesn't understand, or can't do, things that you want it to do. Fortunately with these plugins, we can tell Vim about totally new ways to select and operate on text.