32

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.

enter image description here

4
  • Where is the cursor in the game? Are you not wanting to delete the sentence around the cursor, rather than from cursor to end? Also, this belongs in vi.SE
    – Alejandro
    Commented Apr 8, 2016 at 18:41
  • @AakilFernandes Since you found an answer, I think it would be appropriate for you to delete the comment saying "none of the current answers work". The visitors could be misled into thinking that they won't find an answer in this post.
    – J...S
    Commented Apr 30, 2018 at 19:48
  • @J...S done. Thanks
    – Akhil F
    Commented May 1, 2018 at 1:10
  • You just type capital D
    – kevin
    Commented Mar 1, 2019 at 22:43

9 Answers 9

39

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.

2
  • 6
    For 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 expect ds or dS (or similar) to delete from cursor to end/beginning of sentence, one needs to use ( and ) for this.
    – mazunki
    Commented May 29, 2020 at 0:12
  • If you use the vim-sandwich plugin the das mapping won't work. Try writting da, waiting till the cursor starts blinking then typing s. See github.com/machakann/vim-sandwich/issues/62
    – user10706046
    Commented Nov 29, 2021 at 11:05
18

) 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:

7

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 }
6

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).

3

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.

1
  • And, of course, change the initial 'd' for a 'y' to yank text with the same rules. Commented Aug 30, 2022 at 8:10
1

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

1

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.

1

You can use the command: d2d, but I do not know whether it works in the game.

0

Vim grammar is [Nubmer] [Operator/ Command] [Motion or Text Object]

So in this case, you can use: 2dd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.