I have a list of regular expressions I would like to run on my C code files. They are simple formatting stuff and would save me trouble while my code is reviewed.
Here they are
this removes 2 or more blank lines in a single blank line
:%s/\n\{3,}/\r\r/e
this add missing space at the end of the comment eg /* blah blah*/ to /* blah blah */
:%s/\([^ *]\)\*\//\1 \*\//gc
this add missing space at the start of the comment eg /blah blah/ to /* blah blah*/ note that it ignores /**
:%s/\/\*\([^ *]\)/\/\* \1/gc
removes blank lines after opening brace {
:%s/{\s*$\n\{2,}/{\r/gc
removes blank lines before closing brace }
:%s/\n\{2,}\(\s*\)}/\r\1}/gc
in comments adds a space after comma if missing TODO throws and error E16 if no patterns match
:g/\/\*/ ,/\*\// s/,\([^ ]\)/, \1/gc
I have saved these in a file called fix.txt. Is there a way I can run them from within VI one after the other? something like
:run fix.txt ?
Note that regular expressions can be used with the vi search commands / and ? as well as in the ex :g and :s commands. [19]Much more information on regular expressions can be found in the two O'Reilly books sed & awk, by Dale Dougherty and Arnold Robbins, and Mastering Regular Expressions, by Jeffrey E.F. Friedl.
Using Regex in VimMany of Vim's search features allow you to use regular expressions. Some of them are: The search commands ( / and ? ) The global and substitute command-line (ex) commands ( :g and :s )
In normal mode, press / to start a search, then type the pattern ( \<i\> ), then press Enter. If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
You have to execute:
:source fix.txt
See :help :source
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With