If you want to actually modify the buffer, J
/ :join
do that. If you just want to join the lines that are sent to the interpreter (but keep them split in the buffer), you can retrieve the selected lines with getline()
, and then join()
them. Here's an example command:
:command! -range Invoke echomsg join(getline(<line1>,<line2>), '')
Edit
Based on that, you can "massage" the List of lines returned by getline()
. E.g. to ignore the commented lines:
:command! -range Invoke echomsg join(filter(getline(<line1>,<line2>), 'v:val !~# "^\\s*//"'), '')
Additionally strip leading whitespace (this becomes unwieldy in a single line; better use a function now):
:command! -range Invoke echomsg join(map(filter(getline(<line1>,<line2>), 'v:val !~# "^\\s*//"'), 'substitute(v:val, "^\\s\\+", " ", "g")'), '')