简体   繁体   English

如何在 vi 编辑器 linux 中删除新行?

[英]how to remove new lines in vi editor linux?

I am using Linux (centos flavor) and created a file with the following text:我正在使用 Linux(centos 风格)并创建了一个包含以下文本的文件:

" hello 



  world 
"

Question:题:

Now, I opened the file in vi editor mode and am able to remove all non blank characters(backspace or delete keys do nothing).现在,我在 vi 编辑器模式下打开了文件,并且能够删除所有非空白字符(退格键或删除键什么都不做)。 But newline characters persist and I get error saying "no previous regular expression".但是换行符仍然存在,我收到错误消息“没有以前的正则表达式”。

What should I do to remove all the new lines so that my file is just empty??我应该怎么做才能删除所有新行,以便我的文件只是空的? I have tried backspace key many times but no effect and I do not want to use cat > filename to just overwrite the file to make it empty!我已经多次尝试退格键但没有效果,我不想使用cat > filename来覆盖文件以使其为空!

You can use dd to delete any lines in vi editor.您可以使用dd删除vi编辑器中的任何行。

Example: You have a file having 6 lines and you want to delete all 6 lines:示例:您有一个包含 6 行的文件并且您想要删除所有 6 行:

  1. Open the file using 'vi` editor使用“vi”编辑器打开文件
  2. Go to first line Go转一线
  3. use 6dd使用6dd

:g (for global) could help you here. :g (全球)可以在这里帮助你。

:g/^$/d basically says that "globally find any pattern matching ^$ and delete those". :g/^$/d基本上是说“全局找到任何匹配^$的模式并删除它们”。

If you think that you might have blanks in those lines, you could say ^\ *$如果您认为这些行中可能有空格,您可以说^\ *$

  1. open txt with vi用vi打开txt

  2. :1 << move cursor to first of file :1 << 将 cursor 移动到文件开头

  3. d << enter delete mode d << 进入删除模式

  4. G << move cursor to end of file G << 将 cursor 移动到文件末尾

It will remove all from cursor( in this case, at first of file ) to end of file它将从光标(在这种情况下,在文件的开头)到文件末尾删除所有内容

or要么

  1. open txt with vi用vi打开txt

  2. d

  3. N (Number, As many as you want to delete lines) N (数字,要删除多少行)

  4. Enter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM