Wrap long lines in Vim?
Asked Answered
T

2

6

I've noticed that gq does not work when I paste in a long line. For example, with a textwidth=72 and formatoptions=tcroqbnl, gq refuses to wrap this (in insert mode, I pasted the entire label contents, and then exited insert mode with ESC):

<label for="contact_reason_1">To get assistance with or to confirm a tire replacement recommendation</label>

If I add a line break in (after "to", for example), it'll wrap then. The funny thing is if I join the line back together, it'll happily wrap it again. So VIM seems to somehow be remembering "oh, this is one paste, don't wrap it".

How do I turn that feature off? I'd like gq in command mode to always work. Taking l out of formatoptions did not seem to help (and it shouldn't, this isn't insert mode).


clarification

Yes, I'm using a motion command, in particular, gq<Right>. formatexpr and formatprog are both unset. If it matters, this is in gvim on Debian GNU/Linux, vim version 7.2p284.

steps to reproduce

  1. Pop up gvim on an open file.
  2. Press i to get into insert mode, then type This is a long line. A long line. But not wrappable yet. Or yet. Soon.
  3. Press ESC, then I. Type Now putting text in front of the long line. note: there is a space after the final period, can't get SO to show it, except when this note is here. FUN.
  4. Press ESC, then A. Type And some after. note: space before the And, same SO problem.
  5. Press ESC one last time. Now try gq<Left>, note it only wraps And some after.; I can't get vim to wrap the rest of the line (without going into insert mode and doing a line break by hand, then it works).

Fixing this state is doable; putting a newline after "now" and then hitting undo makes line wrap work again. WTF.

Tearing answered 9/12, 2009 at 13:50 Comment(2)
Reproduced and got a "good" result, sorry. Details under my answer.Piselli
Related: Vim: The "gq" Command to Hard-Wrap Comments, but Not the Code (even if no blank line in-between).Tormoria
I
9

gq isn't enough to wrap the text. You have to give it a motion over which to wrap (like gqj) or tell it to wrap the current line with gqq. Are you sure you're not just mistyping it?

If you aren't, what are the formatexpr and formatprg options set to, if anything?

Update
The problem is the b setting in formatoptions. That's telling Vim to only wrap the text added during the last insertion.

Installation answered 9/12, 2009 at 13:59 Comment(6)
Exactly what I was thinking, +1Piselli
Yes, I'm giving it a motion. And it works on other lines, so I'm pretty sure it works. I've been trying gq<Right>, which normally works. I'll update the question.Tearing
The only situations where gq shouldn't be wrapping a line are 1) the line has no whitespace, 2) the line is shorter than 'textwidth', 3) 'formatexpr'/'formatprg' is set and the function/program that is invoked doesn't wrap the text. Since none of these cases seem to be in play here, I'm not sure why you're seeing this behavior.Installation
I've added some steps to reproduce a similar problem. Does this only happen for me? I tried it with gvim -u NONE, so it's not my vimrc.Tearing
Thanks for the clarification. The problem is the b setting in formatoptions. That's telling Vim to only wrap the text added during the last insertion.Installation
Thanks! It is indeed the b in formatoptions. Please edit this in to your answer so I can accept it!Tearing
P
1

I find that if I select the line before doing the gq, it works fine. Doesn't gq want to be combined with some text selection operation to work?

UPDATE

I confirm the bug. Running vim -u NONE, my formatoptions are vt.

Maybe Bram Molenar or at least the vim community would be interested?

Piselli answered 9/12, 2009 at 14:2 Comment(6)
Please see my clarification & steps to reproduce.Tearing
I wasn't aware that a small motion like <kbd>left</kbd> would be enough to cause wrap, but sure enough! I faithfully followed your steps to reproduce and achieved a wrap just before "But," which looks entirely correct to me. I'm using vim 7.2.245 out of the box on Ubuntu 9.10 . I can do more testing if you tell me what.Piselli
"out of the box" was a lie. my .vimrc does: " Carl's vim settings syn on set sts=3 set ai set sw=3Piselli
@Carl: Your distro (Ubuntu) probably has some customization in /etc/vim/vimrc and /etc/vim/gvimrcTearing
@Carl: That -u NONE will skip system initialization in /etc as well; see :help initialization inside vim.Tearing
Re-tested with -u NONE and now it's behaving "badly" for me too. See update of my answer.Piselli

© 2022 - 2024 — McMap. All rights reserved.