<number><command><text object or motion>The number is used to perform the command over multiple text objects or motions, e.g., backward three words, forward two paragraphs. The number is optional and can appear either before or after the command. The command is an operation, e.g., change, delete (cut), or yank (copy). The command is also optional; but without it, you only have a motion command, not an edit command The text object or motion can either be a text construct, e.g., a word, a sentence, a paragraph, or a motion, e.g., forward a line, back one page, end of the line. An editing command is a command plus a text object or motion, e.g., delete this word, change the next sentence, copy this paragraph.
Lorem ipsum dolor sit amet...
daw
Lorem dolor sit amet...
Text objects beginning with a
include the surrounding white space in the text object, those starting with i
do not. This convention is followed by all text objects.
The motion w
may seem similar to the text object aw
. The difference is in the allowed cursor position. For example, to delete a word using dw
the cursor must be at the start of the word, any other position would delete only part of the word; however, daw
allows the cursor to be at any position in the word.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
cis
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
Notice how the “inner” text object does not include the trailing white space.
Like aw
, as
offers the same cursor position advantage over its motion counterparts (
)
, forward and backward a sentence. To operate on the entire previous sentence (
requires the cursor to be at the end of the sentence; to operate on the entire next sentence )
requires your cursor to be at the start of the sentence.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
dap
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
Again, ap
and ip
provide the same cursor position advantage that Vim’s sentence and word text objects provide: your cursor can be anywhere within the paragraph in order to operate on it.
cw
, operates from the current cursor position. A command using a text-object, e.g., ciw
operates on the whole object regardless of the cursor position. We saw this behavior in each of the various plaintext text objects. Although this requires one more character, it saves you the time and effort of moving the cursor into the “right” position.
puts 'Hello "world"'
ci"
puts 'Hello ""'
Notice that the cursor was not even within the double-quoted phrase (“world”); the command defaulted to changing the first double-quoted phrase in the line.
puts 'Hello "world"'
ci'
puts ''
Current line searches offer an alternative way to delete a quoted phrase. Continuing with the previous example, placing the cursor on the first '
and executing ct'
would delete the contents of the single quoted string and place us in insert mode. However, this is less flexible than using a text object because it requires the cursor to be on the opening '
.
A search pattern /'
could also be used, but it too requires the cursor to be on the opening '
. It also deletes the closing '
.
It’s best to use search commands for searching and not editing.
Project.all(:conditions => { :published => true })
da)
Project.all
Both of these text objects are also available as ab
and ib
, however I find these less intuitive than using the version that includes a parenthesis character.
The %
motion is another way to match a pair of parentheses. Entering %
on an opening parenthesis will move the cursor to the closing parenthesis. Combined with a command, this can provide the samea)
, e.g., c%
is equivalent to ca)
. However, the disadvantage to using %
is that the cursor must be on the opening or closing parenthesis; with a)
the cursor can be anywhere on or the parenthesized phrase. There is also no way to replicate i)
using %
.
(defn sum [x y]
(+ x y))
di]
(defn sum []
(+ x y))
The %
movement can also be with []
. However, it has the same limited flexibility when using it with ()
.
puts "Name: #{user.name}"
ci}
puts "Name: #{}"
Both of these text objects are also available as aB
and iB
, however, I find these less intuitive than using the version that includes a brace character.
Again, the %
movement can also be with {}
. However, it has the same limited flexibility when using it with ()
or []
.
<h2>Sample Title</h2>
cit
<h2></h2>
Notice that the cursor was not even within the <h2>
. This is a very efficient way to quickly replace tag content.
<div id="content"></div>
di>
<></div>
This text object can be used to quickly operate on a single tag and its attributes.
BeanFactoryTransactionAttributeSourceAdvisor
ci,w
FactoryTransactionAttributeSourceAdvisor
foo(42, bar(5), 'hello');
cia
foo(42, , 'hello');
def foo():
if 3 > 5:
return True
return "foo"
dai
def foo():
return "foo"
end
keyword.
hash.each do |key, value|
puts key
puts value
end
cir
hash.each do |key, value|
end
set -o vi
in bash and bindkey -v
in zsh, Vim’s text objects are not available. Text objects were introduced by Vim but shell command line editing is based on Vi.
Interested in a Career at Carbon Five? Check out our job openings.