Why does Vim add spaces when joining lines?
Asked Answered
T

5

37

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

Why is that?

Tiro answered 16/10, 2009 at 16:0 Comment(0)
P
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

Pamella answered 16/10, 2009 at 16:4 Comment(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.Tiro
Then the answer to your question is simply to unset joinspaces: :set nojoinspacesPamella
Most definitely, thanks! I was unfamiliar with this 2 spaces after a sentence thing.Tiro
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.Pamella
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.Lermontov
Since I found this by search, future people also might. I just wanted to join two lines with J, but this always gives you the space, even with set nojoinspaces. I remapped the command with nnoremap J :j!<return>.Mortenson
S
60

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.

Stillman answered 17/9, 2012 at 20:47 Comment(2)
This is the correct answer. The accepted answer did not work for me.Mamelon
Same. nojoinspaces did not have any effect. I had to macro Jx to delete each space.Endocrine
P
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

Pamella answered 16/10, 2009 at 16:4 Comment(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.Tiro
Then the answer to your question is simply to unset joinspaces: :set nojoinspacesPamella
Most definitely, thanks! I was unfamiliar with this 2 spaces after a sentence thing.Tiro
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.Pamella
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.Lermontov
Since I found this by search, future people also might. I just wanted to join two lines with J, but this always gives you the space, even with set nojoinspaces. I remapped the command with nnoremap J :j!<return>.Mortenson
R
11

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.

Ribbon answered 29/5, 2015 at 22:10 Comment(0)
C
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.

Cootie answered 20/3, 2013 at 19:20 Comment(0)
E
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!

Epicotyl answered 8/7, 2015 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.