简体   繁体   中英

how to remove new lines in vi editor linux?

I am using Linux (centos flavor) and created a file with the following text:

" 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). 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!

You can use dd to delete any lines in vi editor.

Example: You have a file having 6 lines and you want to delete all 6 lines:

  1. Open the file using 'vi` editor
  2. Go to first line
  3. use 6dd

:g (for global) could help you here.

:g/^$/d basically says that "globally find any pattern matching ^$ and delete those".

If you think that you might have blanks in those lines, you could say ^\ *$

  1. open txt with vi

  2. :1 << move cursor to first of file

  3. d << enter delete mode

  4. G << move cursor to end of file

It will remove all from cursor( in this case, at first of file ) to end of file

or

  1. open txt with vi

  2. d

  3. N (Number, As many as you want to delete lines)

  4. Enter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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