Vim delete blank lines

后端 未结 14 2464
情歌与酒
情歌与酒 2020-12-02 03:33

What command can I run to remove blank lines in Vim?

相关标签:
14条回答
  • 2020-12-02 03:44

    Found it, it's:

    g/^\s*$/d
    

    Source: Power of g at vim wikia

    Brief explanation of :g

    :[range]g/pattern/cmd
    

    This acts on the specified [range] (default whole file), by executing the Ex command cmd for each line matching pattern (an Ex command is one starting with a colon such as :d for delete). Before executing cmd, "." is set to the current line.

    0 讨论(0)
  • 2020-12-02 03:46
    :g/^$/d
    

    :g will execute a command on lines which match a regex. The regex is 'blank line' and the command is :d (delete)

    0 讨论(0)
  • 2020-12-02 03:46
    :g/^\s*$/d
    ^ begin of a line
    \s* at least 0 spaces and as many as possible (greedy)
    $ end of a line
    

    paste

    :command -range=% DBL :<line1>,<line2>g/^\s*$/d
    

    in your .vimrc,then restart your vim. if you use command :5,12DBL it will delete all blank lines between 5th row and 12th row. I think my answer is the best answer!

    0 讨论(0)
  • 2020-12-02 03:48

    How about:

    :g/^[ \t]*$/d
    
    0 讨论(0)
  • 2020-12-02 03:49
    1. how to remove all the blanks lines

      :%s,\n\n,^M,g
      

      (do this multiple times util all the empty lines went gone)

    2. how to remove all the blanks lines leaving SINGLE empty line

      :%s,\n\n\n,^M^M,g
      

      (do this multiple times)

    3. how to remove all the blanks lines leaving TWO empty lines AT MAXIMUM,

      :%s,\n\n\n\n,^M^M^M,g
      

      (do this multiple times)

    in order to input ^M, I have to control-Q and control-M in windows

    0 讨论(0)
  • 2020-12-02 03:50

    Press delete key in insert mode to remove blank lines.

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