8

When I'm editing English text, I need to work with full sentences instead of lines of text in the file. Using the default Vim commands for this kind of text manipulation is very awkward, because I may have to join and split lines redundantly in order to copy a given sentence in the file. As an example, I might want to move sentence two before sentence one in the text below.

This is sentence
one. This is sentence
two. This is sentence
three.

Are there any settings or plugins that will allow me to do this more easily?

2
  • Is there a reason why you would have hard linebreaks as opposed to being on one long line (or more reasonable, one line per paragraph, as one might do in a TeX document). Commented Feb 4, 2015 at 8:21
  • @mixedmath I like to set my textwidth to 80 and use gqap after I finish writing a paragraph. I would imagine that the document would become difficult to read if I used the one sentence per paragraph approach. Commented Feb 4, 2015 at 8:25

2 Answers 2

10

You already seem to be aware of the 'ap' text-object that represents a paragraph, similar to that there exist 2 text-objects to deal with sentences, 'is' & 'as' for inner sentence & a (or around) sentence. You should use these along with sentence motions '(', ')' for moving them around if need be.

eg.) For moving sentence two before sentence one. I would use the following key combinations in vim : )das(P

Explanation :

  • ): helps you move to the beginning of the second sentence.
  • das: deletes the sentence.
  • (: Moves back to the beginning of the first sentence (previous sentence to be precise)
  • P: Pastes the previously deleted sentence before the current one.
1
  • Thanks for the explanation! It looks like I need to read up on text objects. Commented Feb 4, 2015 at 23:42
1

Here's one possibility. Let's say we have textwidth=40 in mind, and we want to play with Lorem:

Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo
dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est
Lorem ipsum dolor sit amet.

Let's say we want to move the At vero... sentence after the Stet clita... sentence. We might navigate to the At vero... sentence, use da s to delete the sentence. Navigate after the Stet... sentence, p the At vero sentence after it (perhaps inserting a space if necessary).

This yields

Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua.
Stet clita kasd
gubergren, no sea takimata sanctus est
Lorem ipsum dolor sit amet. At vero eos et accusam et justo duo
dolores et ea rebum. 

but we can gqap to reformat the paragraph to the textwidth, yielding

Lorem ipsum dolor sit amet, consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore
magna aliquyam erat, sed diam voluptua.
Stet clita kasd gubergren, no sea
takimata sanctus est Lorem ipsum dolor
sit amet. At vero eos et accusam et
justo duo dolores et ea rebum. 

In other words, vim's gq is sufficiently capable of doing this with repeated application of gq.

To a certain extent, vim will automatically attempt to format things on the fly if you :set fo=aw2tq. This is explained in :h formatoptions and more relevantly :h fo-table. But in my (limited) experience, it does a mediocre job with the above Lorem ipsum test. But it does very well in handling typing on the fly.

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.