38

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

Why is that?

0

5 Answers 5

61

I have a feeling this is what you really want: gJ

From :h gJ:

gJ          Join [count] lines, with a minimum of two lines.
            Don't insert or remove any spaces.  {not in Vi}

This is handy if you've copied something from a terminal and it's pasted it as a big rectangular block into vim, rather than a single line.

I usually use it in visual mode. Hilight stuff, gJ.

2
  • 6
    This is the correct answer. The accepted answer did not work for me.
    – Jonah
    Commented Jan 8, 2013 at 20:35
  • 1
    Same. nojoinspaces did not have any effect. I had to macro Jx to delete each space. Commented Nov 12, 2013 at 13:46
33

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

6
  • 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
    Commented Oct 16, 2009 at 16:24
  • 4
    Then the answer to your question is simply to unset joinspaces: :set nojoinspaces
    – Cascabel
    Commented Oct 16, 2009 at 16:29
  • Most definitely, thanks! I was unfamiliar with this 2 spaces after a sentence thing.
    – binOr
    Commented Oct 16, 2009 at 16:38
  • 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.
    – Cascabel
    Commented Oct 16, 2009 at 16:46
  • 2
    With :set nojoinspaces, I still get one space at the end of every line. Whether it's an "extra" space or not is arguable -- in defense of the Unquestionable Gods of Vim, vi is probably just replacing the newline char with a space so, technically, that space "doesn't count." If you really want NO spaces at all between the joined lines, try ggVGgJ as pointed out by @Chris (not me :-) below.
    – Chris B
    Commented Apr 19, 2013 at 19:09
13

I guess the simple solution to join the lines without spaces between is:

:j!

With ! the join does not insert or delete any spaces. For the whole file, use :%j!.

See: :help :join.

6

This is the answer that ended up working for me, none of the above worked in my use case.

Essentially, use gJ like multiple others have said, but highlight all of file, so in command mode typing ggVGgJ.

2

I still got the extra one space after join, if the line we work on does not end with space. Usually this is the desired behaviour. Example

first line without space
second line

after joining with J, become

first line without space second line

Although in some case, we do not wish to apply it,

myInstance->methodA()
          ->methodB()

And we would want the join to become myInstance->methodA()->methodB() without any space in between!

Here the helpers mapping i use

nmap <leader>jj Jx

<leader> key can be checked with :let mapleader, default to key \ i believe.

so in normal mode, just \jj to perform join without any extra space!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.