GVIM or Vim or VI -
Regular expressions and short- cuts to do complex tasks.
Its a text editor with several built-in short-cuts to simplify day to day operations of file editing. These short-cuts are usaully invoked with : operator. Whenever you type : on your keyboard a section is opened at bottom of the file. This is area where you type further commands to continue execution of expression. Lets discuss one GVIM command line expression below.
GVIM Regular expression tricks are discussed on this page. This editor is very popular for its robustness to do complex tasks without customized programs. For new users, suggested approach is to review our introduction page first and then try examples listed on this page. This page has RegEx on left side and simple tricks on right side. Note: The tricks are tested on UNIX and may not work on other O/S.
Advance gvim commands using RegEX:
How to remove empty/blank lines from text file using regular expressions ?Following is the vi/vim/gvim command to delete or remove empty blank lines::g/^$/dWhere :g executes a command in the generic operation :g/regex/command.
/^$ will match any empty line and its the regex portion in the generic operation.
/d is the command to delete the regex.
Alternate method,:v/./dNote: The alternate method will highlight the entire text file. Use following command to remove highlights.:nohls
How to merge multiple blank lines into one blank line without touching the single blank line separating text?GVIM regular expression to merge multiple blank lines into a single blank line is shown below, no effect on stand alone empty line::g/^\_$\n\_^$/d
How to drop blank characters at end of the line? Do it for only a range of lines text file?
Most common expression used to remove blank characters at end of line in GVIM is shown below:
Explanation of above expression
will do global search in generic expression
is the RegEX from generic expression to find lines with blank characters. This falls in category of gvim regular expressions.In our example Replace_Value in none. Confirm is also none which is equivalent to not ask before executing the global search.
The above command can be modified to add the capability to add check before execute.:%s:\s*$::gc
How to drop blank characters in a range of lines?
The command discussed above will do global search and delete in the file. We can limit the command to a specific region of file by following approach.
First do esc on the the keyboard to go into Visual mode. While in Visual mode select the line to edit using V (capital v) and up/down arrow keys. Once the lines are highlighted and then on keyboard type :
Next , following command will show-
up automatically for the range of files.
Append the command to drop blank characters to above prompt:
How to search numbers in text file?The number can have any number of digits within range 0 to 9. We can use simple gvim Regex below to find all numbers./s/[0-
9]Lets discuss it with an example replace “ace with face” without effecting the words like “replace”, “faced”
We can not achieve the above requirement by just one command. We will need to execute a series of commands using gvim regular expressions below.(1) Remove at word boundary:s\<ace\>:face:g\< \> creates word boundary. Note: the above expression will not replace the words as first in the line or last word in the line.
(2) Remove the first word of any matching line.
Try following, to replace first words oat beginning of any line.:s:^ace\>:face:
(3) To replace last words at end of line.:s:\<ace$:face:
(4) To replace only word in a line.:s:^ace$:face:
This command executes line by line and the explanation of the regular expression above is discussed below for unix.
do a global search in the file opened in gvim.
matches everything in a line
& & -
places every match twice in the same line.
You can simply type.
:+10 to jump down by 10 lines
23 to go up by 23 lines
Goto first line in gvim
Goto last line in gvim
Revert changes in gvim based on time? Go to older changes in time? How to revert changes in vim based on time?Its simple do
:earlier 10m or :earlier 2hTo go back 10 minutes or 2 hours respectively.
commandWhere unix- command is any command that can be executed on unix shell.
How to compile your code from within the program? How to use make in GVIM?How to compile your code from within the program?
Do following set of commands to compile C code.
:set makeprg=gcc\ -
This command needs to be set only once.
Then repeat following command whenever you need to compile the source code.
From the above command gcc can be replaced with different compiler commands for perl, python, verilog rtl etc.
How to create markers in gvim editor? Easy to navigate while editing a file.How to create markers in gvim editor? to easily navigate while editing a file.
Create markers to easily navigate in editor.m<character>creates a marker with short-
Example to go to line 7 of the text file. First hit <esc> key on keyboard and then type ma on keyboard.ma will create first marker.
after this you want to go to line 30 to edit. then on line 30 create another marker mbmb will create second marker.
Now from any line in Visual Mode you can type `a or `b to goto there respective markers.
The markers can be deleted withd`a or d`bYou can create multiple markers to avoid the trouble of remembering the line numbers in huge text files.
List all the markers with command:marks
How to enable spell check in gvim ?:set spell
If it's source code, gvim is smart enough to only spell check comments and string literals.
On gvim, put the cursor on a misspelled word and right-
click. A list of suggestions will be shown at the context menu.
Also see:help spelllang
for information on changing your dictionary to include other regions, languages, or word sets (for example, medical jargon).
It will set dictionary to Latin.
How to set syntax in gvim for filetypes like perl, python, verilog?How to set syntax in gvim for filetypes like perl, python, verilog?
For no syntax do
How to do and and or operations with text in GVIM editor?
/fred\|joe : Search for FRED OR JOE
/.*fred\&.*joe : Search for FRED AND JOE in any ORDER!
How to reverse the order of lines in entire file and also show the command for just a block of text?GVIM regular expression to reverse the order of lines in any block of text in a file is shown below.
Command to reverse order in entire file or global operation.:g/^/m 0
Let’s discuss it with an example of block of text starting at line 10 and going till line 20 end.
Command,:10,20g/^/ mo 10
The operation is simply carried by moving every line in text file before the first line in the block.
In the command above
:g stands for :global -
the commands to executed globally
/^ stands to match beginning of any line.
/m stands for move in gvim