1

I'm using the following key binding to visually select the current paragraph.

nnoremap <leader>v }kV{j

Is works as follows:

  1. } Move below the current paragraph.
  2. k Move one line up.
  3. V Start line-based visual selection.
  4. { Move above the current paragraph.
  5. j Move one line down.

This works well except for the last paragraph in the file. If there is no empty line below the current paragraph } stops at the last line inside the paragraph. The following k results in the last line of the paragraph not being included in the selection. The same problem occurs with the very first paragraph in the file.

How can I make this key binding to also work for paragraphs at the top of bottom of the document?

Is there a text object for this?

6

See:

You are looking for the built in vip.

Also, :h text-objects is a good start to find the text objects you are looking for.

Edit To address your comment: to move the cursor to one end or the other end of the current visual selection you can simply use o in visual mode.

:h v_o

  • Awesome, I tried vi{ but not vip. Can you think of a way to make the cursor end up at the start of the paragraph as in my key binding, rather than at the end? – danijar Feb 22 '18 at 12:20
  • 2
    @danijar o in visual mode moves the cursor to the other end of the current selection, so vipo – statox Feb 22 '18 at 12:22
  • 1
    Learned a lot, thanks. May not even need a macro for that. – danijar Feb 22 '18 at 12:25
  • @danijar Indeed that is clearly not worth a macro nor a custom mapping :) – statox Feb 22 '18 at 12:26

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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