1

Suppose I have several words on multiple lines:

R            
T_std       
k_AB_IN      
k_AB_ac      
k_AB_co2     
k_La

Now I want them to have them all on one line, only seperated by one whitespace.

R T_std ... k_La

So effectively, I need to delete all line endings. Maybe this could be done by recording a macro (like 0dhj) and subsequently applying it to all lines, but there needs to a better way!?

(All I have found on the web so far was how to remove extra whitespace via regular expressions, but not line endings.)

3
  • 1
    In your example there is some trailing whitespace after the text; is it like that in the actual file or is that just in the example here? Dec 18 '20 at 10:47
  • 1
    not quite the same, but should give you a good overview of options: vi.stackexchange.com/questions/439/… Dec 18 '20 at 10:56
  • @MartinTournoij It is, because there used to be a block of = vector(1) and so on, which I deleted via Visual Block mode.
    – rotton
    Dec 18 '20 at 11:39
3

You can remove line endings by joining lines together. There are a few commands that can accomplish this, for instance the J Normal-mode command will join the current line and the next, or you can use a count to join more than the two lines, or use J in Visual mode to join all lines spanning the selection.

(The :join command that @D.BenKnoble mentioned and that takes a range is analogous to the J Normal-mode command, so that's an option too.)

The J command will add spaces between the joined lines (which, from your example, is what you want.) If you for some reason don't want to add spaces in between the joined lines, you can use gJ instead. (Or, if you're using the Ex command, you can use :join! with an extra ! at the end.)

When J is adding spaces in between joined lines, in some situations it will add two spaces, when the initial line ends in a period, question mark, exclamation mark. You can control this behavior with the 'joinspaces' option, which is on by default (the behavior of conditionally adding two spaces), but you can disable with :set nojoinspaces.


Finally, if you want to use a :substitute command to replace the newlines (instead of one of the join command variants), you can use \n to match the newline, and replacing it will also have the effect of joining lines together.

The following :substitute command is similar to :%join:

:%s/\n/ /

Except that the :s command will not respect the 'joinspaces' behavior and it will add a space to the last line in the buffer (you could use a range of :1,$-1 to avoid that last part.)

Using a :s command matching a \n can be useful when you want to replace the newline with something other than a space, or otherwise collapse whitespace preceding the newlines, or perhaps only join consecutive blank lines, situations where using regexes give you a lot more flexibility than a command such as J that can only do one thing.

3

I think there’s a substitute command that works, but the first thing that comes to mind is

:%join

Which translates to “join the whole file.”

0

After trying out several commands, I've noticed that the trailing whitespace is indeed a problem, so I also need to remove it.

When I combine your answer and the expression from this post, I arrive at:

:%s/\s\+$\n/ /

which does exactly what I want.

1
  • 1
    You don't need the $, matching on \n will take care of only matching at the end of the line. You might want to use \s* instead, to also join lines not ending in trailing whitespace. Finally, you might want to use :1,$-1 as a range to avoid adding a space to the last line (though it's easy to drop that one in a second step.)
    – filbranden
    Dec 18 '20 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.