How to join two lines and remove unnecessary spaces in VIM
In source view, when two strings are concatenated, extra spaces will be compressed. For example:
This is line one,<space><space>
<space><space>and this is line two
will connect to:
This is line one,<space>and this is line two
but in VIM the connect command will create:
This is line one,<space><space>and this is line two
How do I get the same result as the original information?
+3
source to share
1 answer
Unfortunately, you cannot tweak this using the options. It is hard-coded that lines with trailing spaces will be detected this way. In general, finite space is undesirable. You might consider Vim's idea: "If there are spaces at the end, it might be important to keep it. Otherwise it won't be there." So the following line has spaces removed and concatenated:
hello```
`there
" When joined:
hello```there
hello```
``there
" joined:
hello```there
hello````
`there
" joined
hello````there
You can change this behavior on the map. This will rewrite yours
J
to remove the trailing whitespace first and then join the lines:
nnoremap J :s/\s*$//<cr>J
vnoremap J :s/\s*$//<cr>gvJ
+5
source to share