how to remove new lines in vi editor linux? - 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:
Open the file using 'vi` editor
Go to first line
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 ^\ *$

open txt with vi
:1 << move cursor to first of file
d << enter delete mode
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
open txt with vi
d
N (Number, As many as you want to delete lines)
Enter

Related

how to visualise and delete trailing newline at the end of file in vim\nvim

Sometimes I need to edit files which should not end with a newline.
However vim\nvim by default do not visualise in any way the newline character at the end of file. Therefore I am not able to:
visually confirm if the file has a newline character at the end or not
remove that character
Are there any setting which would allow me to see the tailing newline character and edit it in the same way as any other characters?
For example, after create 2 files as follows:
echo test > file-with-newline
echo -n test > file-without-newline
opening first one with nvim file-with-newline shows:
test
~
~
file-with-newline
opening second one with nvim file-without-newline shows:
test
~
~
file-without-newline
Navigating with the cursor to the end of line in either case yields the same result (the cursor stops after last visible character: t). There is no way to tell if the newline is there or not, let alone remove it using familiar commands used to remove ordinary characters (or newlines within the file).
You can enable the option :help 'list':
:set list
to show that "newline character" as a $ at the end of the line (among other things):
Note, however, that the option doesn't make the character "editable" in any way.
if the file has a newline character at the end or not
:set eol?
endofline
remove that character
:set noeol nofixeol
:update

why is vim's delete command so slow

I have a file that contains about 5000 lines and I want to delete all lines that have 'some_string' so I first search for /some_string then I execute :g//d. This takes over 5 minutes to delete ~90% of the lines. What gives?
In comparison, if I run sed -i '/some_string/d' some_file it takes 46ms.
Add an underscore to your command.
I experienced a similar problem and it turned out to be each line being copied to my system clipboard. By adding a _, you tell vim to use the blackhole register.
:g//d_
The help gives the following syntax for :d
:[range]d[elete] [x] Delete [range] lines (default: current line) [into register x].

How to write a string in a file using vim editor from command line

I want to create a new file using vi editor from command line and add a string to it multiple times say 100. Using vi -S command.script file.txt is supposed to do the trick where a new file file.txt will be created and the commands given in command.script file can write to this file. My command.script contains
:%100a hello world
:wq
But its's not working, what I am doing wrong?
If you interactively execute :%100a hello world in a Vim session, you'll get E488: Trailing characters. Looking up :help :a:
:{range}a[ppend][!] Insert several lines of text below the specified
line. If the {range} is missing, the text will be
inserted after the current line. [...]
These two commands will keep on asking for lines, until you type a line
containing only a ".".
tells you that the text has to be put in following lines (and concluded by a line with only a . character).
Or did you mean to use the normal mode a command? (That one takes a [count] to multiply; your %100 range is wrong, too!)
You can also use the low-level function append(), repeating the string with repeat().
summary
$append
hello world
[...]
hello world
.
execute "$normal! 100ahello world\<CR>"
" Easier with o instead of a:
$normal! 100ohello world
call append('$', repeat(['hello world'], 100))
non-Vim alternatives
But honestly, if that is your real use case (and not just a simplified toy example), you don't need Vim at all for this. Here's one example for the Bash shell:
$ for i in $(seq 100); do echo "hello world" >> file.txt; done

Copy and pasted file names from finder on mac and vim showing ^M after each filename, how to fix?

I copy and pasted files from finder, into a text file to get the names of the files in a certain directory to a text file. When I open the file the files from each directory are all the same line...
For example: I copy and pasted from the "Hats Directory":
Line1 is:
hats1.jpg^Mhats2.jpg^Mhats3.jpg^M...
In that same text file I copy and pasted files from the "Shoes" directory
Line 2 is:
shoes1.jpg^Mshoes2.jpg^Mshoes3.jpg^M...
I have tried
:%s/^M//
dos2unix
CTRL-V CTRL-M
Neither of them work, am I going about this process the wrong one? Is there a more efficient way to do what I'm trying to do?
A more simple way is using bash truncate tr command.
Just do, tr -d '\r' < inputFile > outputFile to remove all the carriage return characters.
Try this command:
:%s/^M//g
The ^M that you are looking for is ctrl+V Enter
These are old style mac line breaks (a single \r).
Try mac2unix
You could get that list without involving the Finder and without fishy formatting:
:r!ls path/to/dir
See :help :r and :help :!.

Vim - how to start inserting at the end of the file in one step

Is there a single shortcut to start inserting in the new line at end of the file?
I'm aware of G + o combo.
There's also the command line option "+":
vim + myfile.txt
Will open myfile.txt and do an automatic G for you.
Not that I know of - G+o is what I would have suggested too, but that is 2 steps :)
You could always create a macro which does G+o, and then you can invoke the macro which will be 1 step.
Adding the following into ~/.vimrc will create one for you:
:nmap ^A Go
To type the "^A" first press Ctrl-V, then press Ctrl-A. You can then use Ctrl-A to append at the end of the file when not in insert or visual mode.
echo >> myfile.txt && vim -c 'startinsert' + myfile.txt
You can also save the above command in a script and then use $1 instead of myfile.txt, name your script myvim ( or whatever you like ) and always open your files and start writing away instantly.
myvim myfile.txt
You could stick the map definition in your .vimrc and then invoke it when the you open the file.
Or, if you only want to do this for a particular file, you could create an autocmd for that file type that does it automatically. See autocommand in the vim doc's.

Resources