[英]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 行:
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 ^\ *$
如果您认为这些行中可能有空格,您可以说^\ *$
open txt with vi用vi打开txt
:1
<< move cursor to first of file :1
<< 将 cursor 移动到文件开头
d
<< enter delete mode d
<< 进入删除模式
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要么
open txt with vi用vi打开txt
d
N
(Number, As many as you want to delete lines) N
(数字,要删除多少行)
Enter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.