gJ mapping does this; from
Join [count] lines, with a minimum of two lines. Don't insert or remove any spaces.
You could rebind it to
J, if you want to save a keystroke:
:nnoremap J gJ
Note that this doesn't remove any spaces, so if either the current line ends with a space or next line starts with one or more spaces they will be left as is; so this:
We could use
Jx in this case, then it will be
Helloworld, but that won't work in all cases; from the help:
Join the highlighted lines, with a minimum of two lines. Remove the indent and insert up to two
These commands, except "gJ", insert one space in place of the unless
there is trailing white space or the next line starts with a ')'.
So in some cases more than one space or no space is inserted. As far as I can
see, there is no easy way to change this behaviour; I created a function to
gJ to always join without spaces:
" Like gJ, but always remove spaces
execute 'normal! gJ'
" Remove character under the cursor if it's whitespace.
if matchstr(getline('.'), '\%' . col('.') . 'c.') =~ '\s'
execute 'normal! dw'
" Map it to a key
nnoremap <Leader>J :call <SID>join_spaceless()<CR>
If you want, you can add
let save = winsaveview() at the start of the function, and
call winrestview(save) at the end to prevent the cursor from moving (but the current behaviour is the same as