views:

329

answers:

2

I want to unwrap text in Vim. When I join lines I get an additional space between sentences.

Why is that?

+9  A: 

Formatting destroys information. There are many different blocks of text which will result in the same one once formatted. Therefore, there's no way to reverse the operation without prior knowledge (i.e. undo).

Unformatted:

Unformatted text could start out as either all one line, or several, yet look the same when formatted.

Unformatted text could start out as either all one line, or several, 
yet look the same when formatted.

Formatted:

Unformatted text could start out as 
either all one line, or several, 
yet look the same when formatted.

If you want your paragraph all on one line, or if you're okay with a little manual fiddling, you can use J to join lines back together. You can use visual mode to apply the J command to several lines at once, perhaps combined with ap or ip to select a paragraph, e.g. vipJ. Again, you'll still lose some information - multiple spaces at line breaks before formatting will end up collapsed to single spaces. (You can actually join without modifying spaces by using gJ instead of J, but you'll already have lost them when you formatted)

If you're bothered by the extra spaces after sentences (lines ending in !, ?, or .), turn off joinspaces: set nojoinspaces

Jefromi
Thank you very much. I already tried joining lines but what annoys me is that suddenly multiple spaces occur between sentences after joining lines.
binOr
Then the answer to your question is simply to unset joinspaces: `:set nojoinspaces`
Jefromi
Most definitely, thanks! I was unfamiliar with this 2 spaces after a sentence thing.
binOr
I found this in the description below the list of join commands (`:help J`). Vim's internal documentation is generally quite good, especially if you have a jumping-off point like `J`.
Jefromi
A: 

How do you "unformat" something?

I mean ..

Given this formatted code:

if(expr)
{
   statement;
   statement;
}

How do you want it to look like after you "unformat it"?

hasen j
I just wanted to have some text not wrapped at column 79. Joining lines added additional spaces, which got me confused. Sorry that I didn't make that clear in the first place.
binOr
alright, no problem
hasen j
you should edit your question so all of us don't have to read all the comments to figure out what you want.
glenn jackman
@glenn: you should add that comment to the question, so he gets notified of it :P
hasen j