5

In my vimrc I have the following:

vmap Q gw
nmap Q gwap
set formatoptions+=cj
set formatoptions-=t

This disables automatic text wrapping, and allows me to correctly wrap the paragraph my cursor is on right now by pressing Shift+Q.

Normally, formatting a paragraph is exactly what I want to do, but here's an example of where it's wrong:

# This is a long comment in a Python file. It is longer than 80 characters, and should get wrapped.
def a_function():
    """This is still in the same paragraph."""

Now, when I put my cursor on the comment and press Shift+Q, this happens:

# This is a long comment in a Python file. It is longer than 80 characters, and
# should get wrapped.
def a_function(): """This is still in the same paragraph."""

I want the following to happen:

# This is a long comment in a Python file. It is longer than 80 characters, and
# should get wrapped.
def a_function():
    """This is still in the same paragraph."""

Is there a combination of commands or a plugin that does this? I want to use the same key to wrap regular text and comments, and use paragraphs for text, but when my cursor is on a comment I want the wrapping to be limited to the comment.

  • 2
    I use gqq there or possibly some variation of gqj or gq2j – sehe Dec 17 '15 at 17:01
4

The problem is, your motion "ap" moves to the end of a paragraph. And a paragraph is defined as:

A paragraph begins after each empty line, and also at each of a set of
paragraph macros, specified by the pairs of characters in the 'paragraphs'
option.  The default is "IPLPPPQPP TPHPLIPpLpItpplpipbp", which corresponds to
the macros ".IP", ".LP", etc.  (These are nroff macros, so the dot must be in
the first column).  A section boundary is also a paragraph boundary.
Note that a blank line (only containing white space) is NOT a paragraph
boundary.
Also note that this does not include a '{' or '}' in the first column.  When
the '{' flag is in 'cpoptions' then '{' in the first column is used as a
paragraph boundary posix.

                                                        section
A section begins after a form-feed (<C-L>) in the first column and at each of
a set of section macros, specified by the pairs of characters in the
'sections' option.  The default is "SHNHH HUnhsh", which defines a section to
start at the nroff macros ".SH", ".NH", ".H", ".HU", ".nh" and ".sh".

Which means, Vim does as you told and formats the paragraph. You could either add an empty line before your function definition or use a different motion, (since essentialy, this is what they are for ;)) like e.g. the suggested gqj or alternatively use visual mode like this Vq

| improve this answer | |
  • 2
    I'm already perfectly aware of what the problem is - I'm looking for a solution, not an explanation. – orlp Dec 18 '15 at 14:00
  • 3
    The solution is to use the correct motion – Christian Brabandt Dec 18 '15 at 16:59
  • Let me quote from the question: "Is there a combination of commands or a plugin that does this?". Your answer doesn't answer this. I'm sure there must be some combination of commands or a plugin that provides a text object that is the current comment or paragraph. – orlp Dec 18 '15 at 21:14
  • Yes it does. Let me rephrase: if you know the problem, than you should know, that ap already does select the current paragraph. If your and Vims definition of a paragraph differ, than either put an empty line there or use a different motion. That answers exact your question. – Christian Brabandt Dec 19 '15 at 7:24
  • 1
    @ChristianBrabandt my comment of yesterday already shows more constructive information of what motions could be interesting. Well. In less words ar least – sehe Dec 19 '15 at 15:19
0

Looks like this is the only solution so far is vim-textobj-comment

It conflicts with TComment's plugin which also defines ic map for something useless, so I had to disable that mapping with

let g:tcommentTextObjectInlineComment = ''

to stop getting mysterious errors.

But if you want to reformat only part of comment (from cursor position to the end of comment or paragraph inside comment - like Q} does for normal text) you need a "motion" instead of "text object" provided by this plugin.

I've created the custom mapping for this. (It contains hardcoded comment marker // and thus you may need several similar mappings - one per each comment marker you've in your file types.)

autocmd FileType go nnoremap <buffer> Q}
    \ gq/\m\%#.*\(^\s*\/\/.*\)\@<=\(\n\s*\/\/.*\S.*\)*\(\n\s*\)*\ze/e+1<CR>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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