In Vim, what is the simplest way to join all lines in a file into a single line?

后端 未结 6 1416
庸人自扰
庸人自扰 2020-12-07 16:32

I want to join all lines in a file into a single line. What is the simplest way of doing this? I\'ve had poor luck trying to use substitution (\\r\\n or

相关标签:
6条回答
  • 2020-12-07 16:55

    Another way:

    ggVGJ
    

    "ggVG" visually selects all lines, and "J" joins them.

    0 讨论(0)
  • 2020-12-07 16:56

    You can do it with 3 keystrokes starting from normal mode:

    :%j
    
    • : enters command mode
    • % refers to all lines in the file
    • j executes the join command

    Now it seems that this adds a space between the lines. I am not sure if you want this.

    0 讨论(0)
  • 2020-12-07 17:06

    Ah, I found the answer.

    :1,$join
    

    Works like a charm.

    EDIT: As pointed out in the comment:

    :%join   -or-    :%j
    

    ...removes the range.

    0 讨论(0)
  • 2020-12-07 17:12

    Cryptic way:

    qqqqqJ@qq@q
    

    (the first three q's clear the q register, the qqJ@qq records a macro to the q register that performs a Join, then calls q, and the last @q runs it.

    0 讨论(0)
  • 2020-12-07 17:18

    You can do it in three fewer keystrokes:

    :1,$j
    

    isn't ed grand?

    0 讨论(0)
  • 2020-12-07 17:18

    I’m surprised no one even mentioned the other way:

    :%s/\n/ /
    

    I am equally surprised that no one pointed out that the range 1,$ has a shorthand that’s written %.

    (This doesn’t do the same thing as joining the lines, but depending on circumstances that may in fact be more appropriate.)

    0 讨论(0)
提交回复
热议问题