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:

  1. 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/^$/d
    Where :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/./d
    Note: The alternate method will highlight the entire text file. Use following command to remove highlights.
    :nohls
  2. 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

    Where :g executes a command in the generic operation below.
    :g/regex/command

    ^\_$\n\_^$ - Its the regex to grep all multiple blank lines excluding the blank line before any newline text.

    /d is the command to delete the text captured by regular expression.
  3. 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:
    :%s:\s*$::

    Explanation of above expression
    :%s - will do global search in generic expression

    :%s:RegEx:Replace_value:confirm?

    \s*$ - 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
    With this command, gvim will ask for permission to execute for every finds in the file.

    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:

    :`<,`.>s:\s*$::

  4. 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]
    How to do search and replace exact word in a file? Also exclude words containing the search patterns as a part of its characters.
    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:
  5. How to do search and replace exact word in text file excluding includes? Try details from above:

  6. How to duplicate columns in GVIM? Repeat the same text twice on every line? 

    This command executes line by line and the explanation of the regular expression above is discussed below for unix.

    :%s/.*/& &/

    Explanation:-

    :%s - do a global search in the file opened in gvim.

    .* - matches everything in a line

    & & - places every match twice in the same line.

  7. Move up and down from current line by a fixed number?

    GVIM has some short-cuts to move/jump inside files in increment/decrement of numbers.
    You can simply type.
    :+10 to jump down by 10 lines
    :-23 to go up by 23 lines
    Goto first line in gvim
    :1
    Goto last line in gvim
    :$
  8. 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.
  9. How to run shell commands from gvim in unix

    :! unix-command
    Where unix-command is any command that can be executed on unix shell.
  10. 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\ -g\ %
    This command needs to be set only once.
    Then repeat following command whenever you need to compile the source code.
    :make
    From the above command gcc can be replaced with different compiler commands for perl, python, verilog rtl etc.
  11. 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-cut <character>

    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 mb
    mb 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 with

    d`a or d`b
    You can create multiple markers to avoid the trouble of remembering the line numbers in huge text files.

    List all the markers with command
    :marks
  12. How to enable spell check & command to import language specific dictionary?

    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).

    Example
    :set spelllang=ru

    It will set dictionary to Latin.
  13. How to set syntax in gvim for filetypes like perl, python, verilog?

    How to set syntax in gvim for filetypes like perl, python, verilog?

    :set filetype=verilog
    :set filetype=perl
    :set filetype=python
    :set filetype=c
    :set filetype=vhdl
    For no syntax do
  14. 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!
  15. 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

LTE - 4G Wireless Technology

Digital fundamentals.

Interview Questions.

GVIM simple commands.

How to Jump to first or last line of text file?
:1
for first line.
:$
for last line.
How to goto to a particular line in a text file opened in VI/Vim/GVIM?

Simply type following command
:line
Where line is line number.

To go to line 50 you do :50
How to move cursor to Home, Middle, or Last line in a window?
H or M or L
(Note capital letters)
It moves cursor to Home, Middle, or Last respectively in the current view.
How to display and hide line numbers in files?

You can display line number by following command
:set nu

line numbers can be removed by following command.
:set nonu

Tutorials @fullchipdesign.com

Verilog Tutorial.

LTE Tutorial.

Memory Tutorial.

Hope you liked! this page. Don't forgot to access relevant previous and next sections with links below.