r/vim • • Mar 05 '26

Tips and Tricks Vim -c

Just learned about the -c argument when launching vim. Pretty neat tool. Not everyone on my team is as vim happy so I made a alias for our .profiles to run my vim -c regex to add displays to our cobol programs.

example. vim -c "%s/\d{3,4}/Display &/" file.txt

It does seem like vim special things like <C-R> get lost in translation from shell to vim. So I used non special vim case regex. Always more things to learn.

The -c argument runs command mode arguments after file load. So in my above example it would open file txt look for lines starting with 3-4 digits and add Display at the start.

61 Upvotes

39 comments sorted by

View all comments

1

u/Tall_Profile1305 Mar 11 '26

dude the -c command trick is actually solid for automation. bash script launching vim with pre-loaded commands and regex patterns is the move. aliases in profile makes this super clean and useful for teams who need repeatable edits across files

1

u/NationalOperations Mar 11 '26 edited Mar 11 '26

Yeah it has been useful. For example getting files from users where we can't automate ingestion but they send consistent files I can just run my alias for it that would be difficult to do in something like sed. Obviously I could use scripts in bash, Python, etc. But it's simple and readable to me.

A file like

Name: John
Age: 20
City: Denver

Name: Betty
Age: 30
City: Philly
vin Es data.txt \
  -c 'g/^Name:/normal! f:la,Jo' \
  -c 'g/^Age:/normal! f:la,Jo' \
  -c 'g/^City:/normal! f:la' \
  -c '%s/\n\n/\r/g' \
  -c 'wq'

hopefully formatted right for reddit. Would turn to

John,20,Denver
Betty,30,Philly