6

How can I set up a keybinding to delete up to the end of a sentence that ends with a period, but keep the period?

The commands df., d), and D all truncate the sentence but don't keep the period. Manually, I can type d/\.<ENTER>:noh<ENTER>.

For example: I have this sentence and the cursor is at the position marked by the caret:

Scholar X's monograph is a groundbreaking study but I don't like it.
                                               ^

I want to type one command (say, Ds) and get this:

Scholar X's monograph is a groundbreaking study.
0

5 Answers 5

3

With no extra configuration involved, I would just use:

c).<Esc>

Bonus: It's repeatable with the dot command.

Also bonus: It works on any POSIX vi and is not Vim-specific.

And actually, I would use <C-[> (or "Ctrl+[" if you speak Windows) rather than actually pressing the "Escape" key.

2
  • 2
    Nice try, but this fails if the document doesn’t begin each sentence on a new line; e.g., Scholar X’s monograph is a groundbreaking study.  I like it.” (all on one line). Your command will delete the spaces before “I like it.” Commented Jan 28, 2017 at 16:18
  • I start every sentence on a new line, so this does work for me. Commented Feb 4, 2017 at 4:38
8

t and f are nice when we are on the same line. If the period is on another line, we're back to /\.. If you want to type ds, or cs, or ys, ... it can be done thanks to an operator-pending mode mapping:

:onoremap s /\./<cr>
" and for visual mode, with e-1 to exclude the period.
:vnoremap s /\./e-1<cr>

But honestly, I'll bind this to . instead of s as s already does something in visual mode.

7

You had the right idea with df.. Of course, f goes up to the period like you noticed. If you use t (mnemonic until) instead, it will go forward until the period, but not to it. From :h t

                            *t*
t{char}         Till before [count]'th occurrence of {char} to the
            right.  The cursor is placed on the character left of
            {char} |inclusive|.
            {char} can be entered like with the |f| command.

So if your buffer is like this:

Scholar X's monograph is a groundbreaking study but I don't like it.
                                               ^

and you type t., you will be here:

Scholar X's monograph is a groundbreaking study but I don't like it.
                                                                  ^

The f and t commands mirror eachother very nicely. In fact they're pretty much identical other than f moving one further.

  • They can both be repeated with ; and ,

  • They both have uppercase variants that move backwards instead of forwards

2
  • I accepted this before realizing it doesn't work if the sentence ends on another line. Is that possible with these commands? Commented Jan 27, 2017 at 19:12
  • 1
    I personally think of the mnemonic for t as up to
    – Paul Evans
    Commented Jan 28, 2017 at 5:20
0

In your case I would do the following:

ma)bd`a
^^^^^^^
||||||+ mark a
|||||+  line position
||||+   delete
|||+    go to end of last word
||+     start of next sentence
|+      mark a
+       set a mark

Although not one command as you ask, this does if you're used to using marks roll off the fingers with reasonable ease.

1
  • 1
    This fails if the last character before the period is also a punctuation character; e.g., I have a fox named "Edward".  He jumped over a dog (which was lazy). Commented Jan 28, 2017 at 16:21
0

After reviewing the other answers I thought of this:

map ds v)2hx

Enter visual mode, go to the end of the sentence (even if on subsequent line), back up two characters to exclude the period, and delete.

1
  • 1
    This doesn’t work for me. (1) I can’t get h to move back up to a previous line. (2) Have you considered the case where the document doesn’t begin each sentence on a new line? My version of vim doesn’t recognize (period)(space) as the end of a sentence; it has to be (period)(space)(space). In that case, the 2h just backs up over the two spaces, and the period still gets deleted. Changing 2h to 3h would be a quick fix, but that would fail if there were ever more than two spaces after the period. Commented Jan 28, 2017 at 16:20

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.