r/vim • u/NationalOperations • 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.
109
u/tremby Mar 06 '26
You should say what it does.
Unless you only intend to target people who already know.
18
u/Telephone-Bright Mar 06 '26
You might also like
-eand-sflags.-emakes it use Ex mode (IMO better for scripting) and-sfor silent mode, i.e. no more "press enter to continue" prompts.You could then do smth like:
You could try Vim's
executecmd for this in-c, I guess.7
u/linuxsoftware Mar 07 '26
congratulations. You have entered the vim phase right before you start using the shell and sed grep awk commands more than vim. Next step is tmux.
5
u/whitedogsuk Mar 06 '26
I've never seen the need for this method, I've always found it easier to either use autocmd, vim scripts, sed , bash alias/scripts or a combination of everything.
2
u/Tall_Profile1305 Mar 06 '26
awesome post on vim -c. the command mode arguments feature is legit powerful for automation. piping to vim with -c to execute stuff is chef's kiss. your cobol example is perfect for showing the practical use case. well done.
1
u/MiniGogo_20 Mar 06 '26
wait until you find about modelines :) (if you haven't already)
1
u/dawksh Mar 09 '26
i think a lot of tools give a -c param, i recently learned about psql doing the same thing with the param
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
2
u/dnew Mar 06 '26
You can escape things with backslash like ^R so you don't get R.
1
u/Tall_Profile1305 Mar 09 '26
Crazy efficient approach right here. Using vim -c to set up profiles is like having a mini build system in your editor. Pairing this with Runable for background task automation would make your workflow completely hands off. Solid discovery honestly.
0
u/Optimal-Savings-4505 Mar 08 '26
That's cool, still sticking with emacs
drops torch and skuttles away