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!
3
Answers
You can use
dd
to delete any lines invi
editor.Example:
You have a file having 6 lines and you want to delete all 6 lines:
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
^ *$
open txt with vi
:1
<< move cursor to first of filed
<< enter delete modeG
<< move cursor to end of fileIt will remove all from cursor( in this case, at first of file ) to end of file
or
open txt with vi
d
N
(Number, As many as you want to delete lines)Enter