Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vi/vim command line options using -c

Tags:

vi

objective:

/^Mar find a pattern, (line starts with Mar) GN find last occurrence of this pattern dgg delete from current position to top of file :wq write and quit

I've tried:

vi -c /^Mar -c GN -c dgg -c wq testfile.log

it complains GN is not an editor command dgg is not an editor command

yet when I issue the same within vi, they work just fine. I'm hoping I'm just missing something simple, but I've been searching on Google for ages, and just can't ask the right question, apparently.

Thanks!

like image 869
david barber Avatar asked Mar 18 '26 22:03

david barber


2 Answers

The commands you provide after the -c switch are interpreted as Ex commands, which means e.g. -c GN will behave as if running :GN inside vim. Prepend "normal" to the command in order to interpret it in normal mode:

vi -c "/^Mar" -c "normal GN" -c "normal dgg" -c wq testfile.log

like image 106
helios35 Avatar answered Mar 21 '26 08:03

helios35


The commands written in the command line are ex commands. They are commands that are prefixed by ":" in when working with vim. You can rewrite your command line to use ex commands:

vi -c '/^Mar' -c '$' -c '?' -c '1,.d' -c 'wq' testfile.log

That should does bas-ically the same as reuired - find the pattern, goto last line, find the pattern backward, delete everything from begin to current line, write and quit.

like image 40
Zaboj Campula Avatar answered Mar 21 '26 08:03

Zaboj Campula



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!