I want to unwrap text in Vim. When I join lines I get an additional space between sentences.
Why is that?
I want to unwrap text in Vim. When I join lines I get an additional space between sentences.
Why is that?
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
:set nojoinspaces
–
Pamella :help J
). Vim's internal documentation is generally quite good, especially if you have a jumping-off point like J
. –
Pamella :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 J
, but this always gives you the space, even with set nojoinspaces
. I remapped the command with nnoremap J :j!<return>
. –
Mortenson 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.
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
:set nojoinspaces
–
Pamella :help J
). Vim's internal documentation is generally quite good, especially if you have a jumping-off point like J
. –
Pamella :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 J
, but this always gives you the space, even with set nojoinspaces
. I remapped the command with nnoremap J :j!<return>
. –
Mortenson 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
.
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.
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!
© 2022 - 2024 — McMap. All rights reserved.