Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a set of regular expressions in VI

Tags:

vim

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 ?
like image 288
user636273 Avatar asked Jun 05 '11 16:06

user636273


People also ask

Can we use regular expressions in vi editor?

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.

Does vim use regex?

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 )

How do I match a pattern in vim?

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.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.


1 Answers

You have to execute:

:source fix.txt

See :help :source.

like image 114
Susam Pal Avatar answered Oct 25 '22 12:10

Susam Pal