'Vim: The "gq" Command to Hard-Wrap Comments, but Not the Code (even if no blank line in-between)
My Vim configuration includes set formatoptions=c,q,a. I am completely annoyed with the following problem (| denotes cursor position, its exact position does not matter, as you probably know only the fact of its presence in this commented line matters):
" This is a long line which we would like to wrap. However, something sick is go|ing to happen if we hit "gqip" here!
if has('win32') || has('win64')
set runtimepath^=~/.vim
set runtimepath+=~/.vim/after
endif
Now we hit gqip:
" This is a long line which we would like to wrap. However, something sick is
" go|ing to happen if we hit "gqip" here!
if has('win32') || has('win64') set runtimepath^=~/.vim set
runtimepath+=~/.vim/after endif
What it does is - it actually treats the whole thing as a single paragraph. (Yes, I know that separating with a blank line prevents this behavior, but it does not solve the problem!) What I would like it to be is indeed:
" This is a long line which we would like to wrap. However, something sick is
" go|ing to happen if we hit "gqip" here!
if has('win32') || has('win64')
set runtimepath^=~/.vim
set runtimepath+=~/.vim/after
endif
In other words, it would be great if gq could somehow forget about the code and work only with comments.
BONUS: How to do this formatting (wrapping comments only) on the whole buffer in one shot? Because, ideally I would like to move that stuff to a special formatting hook for file saving.
Solution 1:[1]
it actually treats the whole thing as a single paragraph
Well, with gqip, you told it to! You need to choose the right motion. In this case of a single line, it would be gqq. For 3 lines, that's gq2j. If it's too many lines to count, use visual mode: Vjjjjjjgq.
Solution 2:[2]
The meaning of the ip text object is rather narrow, as @IngoKarkat pointed out: it is simply any text paragraph delimited by blank lines.
I have also created a plugin that you could use to address this problem.
textobj-comment - Text objects for comments
In contrast with the SameSyntaxMotion plugin, textobj-comment relies on the filetype-specific 'comments' setting to identify comments. That means it should work even with syntax-highlighting turned off.
Use gqic to format a comment.
Pro tip: Prefer gw over gq as it preserves the cursor position.
Solution 3:[3]
Search for commented lines using :g, then wrap those lines:
:%g/^"/normal gq_
Solution 4:[4]
I know I'm a bit late to the party, but I found this question while investigating the same problem myself. How about:
set formatprg=fmt\ --prefix='\"'
With that gqip seems to behave as required. I will definitely be checking out the SameSyntaxMotion plugin though.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ingo Karkat |
| Solution 2 | glts |
| Solution 3 | Marius |
| Solution 4 | subtleseeker |
