So I'm playing vim adventures and I got stuck. I need a Vim command that will delete the keys in red. I thought dd
would do it, but that only deletes the current line.
9 Answers
Use das or dis to delete a sentence. Use dap or dip to delete a paragraph. See :help text-objects
for details. Unrelated to your question, see this wiki page for plugins that provide other, highly useful text objects.
-
6For reference:
d
deletes the next object.s
for sentence.a
means "around",i
means "inner". The difference between them is the whitespace. While one would expectds
ordS
(or similar) to delete from cursor to end/beginning of sentence, one needs to use(
and)
for this.– mazunkiCommented May 29, 2020 at 0:12 -
If you use the
vim-sandwich
plugin thedas
mapping won't work. Try writtingda
, waiting till the cursor starts blinking then typings
. See github.com/machakann/vim-sandwich/issues/62– user10706046Commented Nov 29, 2021 at 11:05
)
jumps to the beginning of next sentence, so d)
will delete (from the cursor) till the beginning of the next sentence. Vim detects sentences using .
, meaning period + space. This does mean that d)
will have some problems if your cursor is on either the period or space delimiting two sentences, and will only delete until the first character of the next sentence (meaning it deletes either a space or the period and space, which is almost never what is desired). das
will work as you probably expect, deleting the sentence and the delimiter (period + space).
If you specifically want to move (and delete to) the last character in a sentence it is more complicated according to this vi.SE answer:
To delete to the end of the sentence, from where your cursor is, use the letters, use "d)". the "d" is the delete command object, followed by a motion object ")" which advances the cursor (and the deletion process) to the end of the sentence.
To delete "around" a sentence, including all the extra whitespace, use "das" (delete around sentence). Or to delete inside the sentence, and not all the whitespace, then use "dis" (delete inside sentence).
Once you understand the VIM language, then you can easily memorize a plethora of operations. Use this table to understand VIM's vocabulary:
COUNT NUMERAL + TEXT OBJECT COMMAND + MOTION (or OPERATORS) "3das" will perform "delete around sentence 3 times"
So, if practical, you could place a numeral followed by... a command:
d=delete
y=yank (into memory buffer to "put" later)
c=change (delete then insert new text)
and then a motion:
) = move cursor to end of sentence
( = move cursor to beginning of prior sentence
} = move cursor to the next paragraph
{ = move cursor to the beginning of the prior paragraph
w = move cursor to next word
b = move cursor back a word
$ = move cursor to the end of the logical line
0 = (zero) move cursor to the beginning of the logical line
G = move cursor to the end of the file
gg = move cursor to the beginning of the file
h, j, k, or l (you might have to look those up)
OR instead of a Motion, define a field of area using:
a = around
i = inside
followed by the name of the area around the cursor:
s = sentence
p = paragraph
w = word
t = xml-tag <tag example> lots of text between tags </tag example>
< or > = inside or around a tag, frequently found in xml documents
{ [ ( ) ] } = inside or around any type of bracket ...
... {a large area [some more (a little stuff) things] of a great many things }
The solution was either dk
which deletes the line and the line above it or dj
which deletes the line and the line below it.
My original question was actually not the right question (there are multiple sentences).
I actually find this table from the help file the best overview for block commands:
"dl" delete character (alias: "x")
"diw" delete inner word
"daw" delete a word
"diW" delete inner WORD (see |WORD|)
"daW" delete a WORD (see |WORD|)
"dgn" delete the next search pattern match
"dd" delete one line
"dis" delete inner sentence
"das" delete a sentence
"dib" delete inner '(' ')' block
"dab" delete a '(' ')' block
"dip" delete inner paragraph
"dap" delete a paragraph
"diB" delete inner '{' '}' block
"daB" delete a '{' '}' block
So deleting a sentence is das
or deleting a paragraph is dap
.
-
And, of course, change the initial 'd' for a 'y' to yank text with the same rules. Commented Aug 30, 2022 at 8:10
If you want to delete from J
up to and including the .
start at J
and use df.
If you want to delete both lines then 2dd
Another option (not sure if it works in the game) is to delete up to and including the period:
d/\./e
You have to escape the period when using a search pattern like this after the delete command.
If you were limited to a single line, it is much simpler:
df.
Vim grammar is [Nubmer] [Operator/ Command] [Motion or Text Object]
So in this case, you can use: 2dd
D