:g/^/gqq
How would I reverse this operation? That is, an existing file
with lines 65 characters long, each ending in a carriage return,
with a blank line between paragraphs. I want to join each
paragraph into one long line, to export to a word processor.
Thanks in advance.
This works in Vim.
:s/\n\([^\n]\)/ \1/
--
Andreas Kähäri
--------------------------------------------------------------
Use the gq command as you did before, but slightly differently. First
:set tw=9999
(some value longer than the longest paragraph) then
1GgqG
Gary
To break long lines to length defined by textwidth:
:g/^/normal gqq
To join short lines to maximum length defined by textwidth:
:g/^/normal 1GgqG
In the end I mapped both commands to F2 and F3 in .vimrc:
map <F2> :set tw=65 <ESC> :g/^/normal gqq<CR>
map <F3> :set tw=10000 <ESC> :g/^/normal 1GgqG<CR>
What a great editor!
Thanks for the help.
In article <X0aq8.254481$kb.14...@news1.calgary.shaw.ca>, "Steve
George" <st...@glort.com> wrote:
> To break long lines to length defined by textwidth:
> :g/^/normal gqq
On a per line basis, I guess this is the best and easiest. From a purist
point of view, I'd probably use "/./" to specify lines with characters
on it only.
> To join short lines to maximum length defined by textwidth:
> :g/^/normal 1GgqG
That could possibly be very expensive as your format the whole buffer.
Actually, I can't see that it is any different from a single "1GgqG".
If you want to do it for all paragraphs (starting) in a specific range,
I'd much rather do
:'a,'bg/\S/normal gq}
Peppe
--
"Before you criticize someone, walk
Preben "Peppe" Guldberg __/-\__ a mile in his shoes. That way, if
c92...@student.dtu.dk (o o) he gets angry, he'll be a mile away
----------------------oOOo (_) oOOo-- - and barefoot." --Sarah Jackson
For example,
:'a,'bg/\S/normal gq}
1GvGgq
1GgqG
:g/^/normal gqq
:g/$/normal gqq
all seem to do roughly the same thing, as far as I can tell, on the files
I've used them on. Being a Vim newbie, figuring out what they mean is the
challenge for me.
1GgqG is the intuitive first answer, and it seems to work for both
breaking the lines and joining them again, based on the textwidth, but I'm
not sure why it doesn't merge blank lines with lines containing
non-whitespace characters. Formatting the entire buffer was my intention,
by the way.
If I understand correctly, your suggestion would format lines in the range
of the marks 'a and 'b, only if there's a non-whitespace character
on those lines, then format to the end of the paragraphs those lines are
in.
It does sound safer to me, but the end result seems to be exactly the
same as the other examples. Correct me if I'm wrong.
In article <slrnaaml9v....@daddy.guldberg.net.invalid>, "Peppe"
The power and beauty of it.
> For example,
> :'a,'bg/\S/normal gq}
> 1GvGgq
> 1GgqG
> :g/^/normal gqq
> :g/$/normal gqq
> all seem to do roughly the same thing, as far as I can tell, on the files
> I've used them on. Being a Vim newbie, figuring out what they mean is the
> challenge for me.
There are many subtleties involved. A quick run down of the commands
above. Take |link| to mean 'more info at ":help link"'. Bear with me
if I am a bit verbose.
1) 1GgqG
"1G" takes you to the first line of the buffer.
"gq" is an |operator| which can be followed by a |motion| or
|text-object|. It will format the lines covered by the
motion/text-object.
"G" is a motion taking you to the end of the buffer.
All pretty straight forward. It will reformat the entire buffer.
2) 1GvGgq
"1G" we know.
"v" enters |visual-mode| allowing you to select, in a more
interactive way, a portion of the buffer for an operator to work
on. "v" is characterwise visual, so your selection will be all
characters from start to end point, even if they are in the middle
of lines.
"G" selects until the first character of the last line. Note that
the remainder of the lines is not selected (due to "v").
"gq" then formats, linewise, the selection. As "gq" exclusively
deals with whole lines, it overrides the characterwise nature of
the "v" selection.
Visual mode is nice and can be very handy. In this case it is
completely superfluous, though. Actually, I find it expensive as
the screen has to be refreshed after "vG". (Too many slow links or
machines).
I guess a lot of newcomers to vim use visual mode a lot as you can
see excactly what chunk of the text an operator will work on
(barring carater/line/blockwise issues) -- and it sticks with
them.
3) :g/^/normal gqq
:g/$/normal gqq
":g/.../" perfomes a set of ex commands on the matching lines. All
matching lines are first flagged internally and then processed one
by one. A check is made whether the line was deleted while
processing one of the previous lines.
"normal ..." temporarily switches to |normal-mode| and executes
the specified commands. In vim 6, :normal will be completed with
<Esc> if it is not complete eg. a "d" without a motion).
"gqq" formats the current line. It will never join lines, only
break them if they are too long.
Note that these commands are synonymous as every line in the
buffer will match either of "^" (start of line) or "$" (end of
line).
4) :'a,'bg/\S/normal gq}
":'a,'b" sets a range for the following command based on marks.
"g/\S/" will perform the following commands on lines with at least
one non-whitespace character on it (in the range). |/\S|
"normal gq}" formats until the end of the paragraph. Lines between
the current one and end of paragraph are replaced (implying prior
deletion) and hence the :normal command is actually only applied
to the first line in each paragraph.
Note that "gq}" may reformat beyond the last line in the range.
> 1GgqG is the intuitive first answer, and it seems to work for both
> breaking the lines and joining them again, based on the textwidth, but I'm
> not sure why it doesn't merge blank lines with lines containing
> non-whitespace characters.
Blank lines are concidered paragraph seperators. Anything else does
not really makes sense to me.
It is documented under ":help fo-table" where the "q" is explained.
> Formatting the entire buffer was my intention,
> by the way.
In that case, just delete all blank lines first:
:g/^\s*$/d
> If I understand correctly, your suggestion would format lines in the range
> of the marks 'a and 'b, only if there's a non-whitespace character
> on those lines, then format to the end of the paragraphs those lines are
> in.
Yes.
> It does sound safer to me, but the end result seems to be exactly the
> same as the other examples. Correct me if I'm wrong.
For one, it uses a range. If you want the whole buffer, there is not
much point going this route.
As for only matching lines with a non-whitespace character on it, I
used it mostly out of habit. When I, with very little effort, can make
my commands more precise in scope, I tend to do so.
In this case, I don't know if has any advantages. You could have a
trade off between having to check all lines for non-whitespace for the
benefit of not having to execute a very simple command on empty lines.
Still, it is just a simple example of more complicated stuff like
:0/^Thus wrote/+,/^-- $/?.?-v/^>/norm gq}
which would normally be able to reformat only the new text I write in
an email or newsgroup post after the attribution and before the
signature. In this one it would fail on the indented explanations
above as there are unmarked subpoints.
> In article <slrnaaml9v....@daddy.guldberg.net.invalid>, "Peppe"
> <c92...@student.dtu.dk> wrote:
[please concider removing quoted material when top posting or
altogether switch away from top posting.]