strip spaces on paragraph formatting

1 view
Skip to first unread message

Michael Iatrou

unread,
Nov 21, 2008, 4:33:25 AM11/21/08
to vim...@googlegroups.com
Hi,

Formatting the following two lines:

fooSPACESPACE
bar

using gqap (or gq on a visual selection of them) will give:

foo bar

I there a way to customize its behavior, to merge arbitrary number of white
space at the end of the line to a single space character, so that in the
example above I get:

foo bar

TIA

--
Michael Iatrou (iycz)

Tony Mechelynck

unread,
Nov 21, 2008, 5:49:18 AM11/21/08
to vim...@googlegroups.com

Maybe by first using the following to suppress all whitespace at ends of
lines? (Note: In "mail" files, the single space at the end of the
dash-dash-space sig delimiter should be left intact, hence the TRUE path
of the :if.)

mark '
1
if &ft == 'mail' && search('^-- $', 'cn')
1,/^-- $/-1s/\s\+$//e
else
1,$s/\s\+//e
endif
''

Note: The second and last lines in the (untested) snippet above are
"naked ranges", they move the cursor to the corresponding line.


Best regards,
Tony.
--
The gods gave man fire and he invented fire engines. They gave him
love and he invented marriage.

Teemu Likonen

unread,
Nov 21, 2008, 6:01:46 AM11/21/08
to vim...@googlegroups.com
Michael Iatrou (2008-11-21 11:33 +0200) wrote:

> I there a way to customize its behavior, to merge arbitrary number of
> white space at the end of the line to a single space character, so
> that in the example above I get:
>
> foo bar

There's no easy customization so you have to combine several commands.

TextFormat plugin adds a bit more automatic and intelligent
text-formatting commands. It's suitable for human languages and it does
what you asked for (removes extra spaces).

http://www.vim.org/scripts/script.php?script_id=2324

fritzophrenic

unread,
Nov 21, 2008, 10:00:38 AM11/21/08
to vim_use


> There's no easy customization so you have to combine several commands.
>

It is also possible to write a custom formatexpr function. See :help
'formatexpr'. I don't know if this is at all recommended, but I have
found that within this expression it is possible to use the 'gw'
command (:help gw) in order to fall back to the basic behavior.

A full formatexpr is pretty involved though...not at all easy.

StarWing

unread,
Nov 21, 2008, 10:21:20 AM11/21/08
to vim_use
i think you can simply use :'<,'>s/\s*$//g, and use J or gq to format
your text.
Reply all
Reply to author
Forward
0 new messages