强悍的 vim —— 删除空行、删除注释以及加注释解注释

来源:互联网 发布:java实现bt下载 编辑:程序博客网 时间:2024/04/29 17:57

1. 删除空行

空行的构成比较复杂

  • (1)删除没有内容的空白行

    :g/^$/d
  • (2)删除包含空格(%s)的空白行

    :g/^%s*$/d

2. 删除注释

:%s/^#.*$//g

如果某些行以若干空格开始,并以换行结束:

:%s/^[ ]*#.*\n//g

3. 删除以//开头的注释

$ cat test.txt | grep -v '//' >> test2.txt

4. 加注释

其实就是替换:

:s/^/#/:s/^/#/g

末尾的g可加可不加

  • :s/<from>/<to> = substitude across entire document replacing <from> with <to>(只对每一行的第一个进行修改)

  • :s/<from>/<to>/g = substitute every occurence on line rather than just first

5. 解注释

:s/^#//g

^#:表示开头为 # 号;

0 0
原创粉丝点击