ex - Differences in running vim via command line vs. running it in the vim editor -


i trying process series of files. have noticed there discrepancies in running particular command command line (i.e. ex mode). e.g.

$cat poo.txt big red dog small black cat  $vim -c "2,$g/^dog/d|wq" poo.txt  $cat poo.txt big small black cat 

it appears 2,$g/^dog/d|wq has deleted lines red , dog. confuses me because command should : start on line 2 (going eof) , delete lines beginning dog. in instance, i'd expect output be:

$ cat poo.txt  big red small black cat 

in fact, if try in vim editor exact behavior observed.

question: cause of discrepancy between vim -c version , vim version of running command?

i think need replace double quotes single quotes prevent shell expanding $g. man bash:

enclosing characters in double quotes preserves literal value of characters within  quotes,   exception  of  $,  `, \, and,  when history expansion enabled, !. 

currently, shell expands $g inside string, if environment variable. it's not defined, expands empty string. so, though you've typed:

vim -c "2,$g/^dog/d|wq" poo.txt 

vim doesn't receive command:

2,$g/^dog/d|wq 

... but:

2,/^dog/d|wq 

this command deletes lines 1 address 2, next 1 starts dog (in case it's 3rd line). then, saves , quit.

but if replace quotes, there's still problem in command. :h :bar:

these commands see '|' argument, , can therefore not followed vim command:  ... :global ... 

the bar interpreted :g part of argument, not command termination. in case, means whenever finds line starting dog, delete it, save , quit. so, if there several dog lines, first 1 deleted, because :g have saved , quit after processing 1st one.

you need hide |wq :g, either wrapping global command inside string , executing :execute, or moving wq in -c {cmd}. in all, try:

vim -c 'exe "2,\$g/^dog/d" | wq' poo.txt 

or

vim -c '2,$g/^dog/d' -c 'wq' poo.txt 

or

vim -c '2,$g/^dog/d' -cx poo.txt 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -