Vim How to Search Words When Reading Man

This commodity is part of the on-going Vi / Vim Tips and Tricks series. Vim is normally mentioned as text editor, not text creator. Why ? Because we spend lot of fourth dimension editing an existing text than creating new text.  In the text editing, text/pattern substitutions becomes a vital part.

In this article, let us review how to perform both basic and advanced text and pattern substitution features in Vi and Vim Editor. These features are explained using 12 very applied and powerful text substitution examples.

Syntax of the text substitution inside vim editor:

:[range]southward[ubstitute]/{pattern}/{string}/[flags] [count]


Following are three possible flags.

  1. [c] Confirm each substitution.
  2. [g] Replace all occurrences in the line.
  3. [i] Ignore case for the pattern.

Example ane. Substitute all occurrences of a text with some other text in the whole file

This is the basic fundamental usage of the text substitution inside Six editor. When you lot want a specific text to be replaced with another text in the unabridged file then yous tin can utilise the following sequence.

:%south/quondam-text/new-text/g
  • %s – specifies all lines. Specifying the range equally '%' ways do commutation in the entire file.
  • yard – specifies all occurrences in the line. With the 'thou' flag , y'all can make the whole line to be substituted. If this 'thousand' flag is non used then just offset occurrence in the line only will exist substituted.

Example ii. Substitution of a text with another text within a unmarried line

When yous want a specific text to be replaced with some other text inside a unmarried line in a instance insensitive style. Specifying no range means, do substitution in the current line but. With the 'i' flag, you can make the substitute search text to exist case insensitive.

:s/I/We/gi

Example iii. Substitution of a text with another text within a range of lines

With the range, yous can make only a range of line to exist affected in the exchange. Specifying 1, 10 equally range means, do substitution but in the lines one – ten.

:1,10s/helo/hello/thousand

Example iv. Exchange of a text with another text by visual selection of lines

You tin as well select a specific lines by visually selecting those lines. Press CTRL + 5 in control mode, use navigation keys to select the function of the file you want to exist substituted. Printing ':' which will automatically formed as :'<,'> Then you tin can employ the normal substitute as

:'<,'>s/helo/hello/g

Instance five. Substitution of a text with another text just the 1st 10 number of lines

Using count in substitution, If yous specify the count N in the substitution then information technology means do commutation in N lines from the current position of the cursor. practice substitution in iv lines from the current line.

:s/helo/hello/g 4

Example half dozen. Substitute only the whole word and not partial match

Permit us assume that you want to change only the whole give-and-take 'his' to 'her' in the original text mentioned beneath. If you exercise the standard substitution, apart from changing his to her, information technology will also change This to Ther as shown below.

Standard Subsitution

Original Text: This is his idea  :s/his/her/g  Translated Text: Ther is her idea

Whole Word Subsitution

Original Text: This is his thought  :s/\<his\>/her/  Translated Text: This is her idea

Note:: You should enclose the word with < and > , which will force the commutation to search only for the total word and not any partial match.

Example 7. Substitute either word1 or word2 with a new word using regular expression

In the post-obit example, information technology will translate any occurrences of either proficient or nice volition be replaced with awesome.

Original Text: Linux is practiced. Life is squeamish.  :%due south/\(good\|squeamish\)/awesome/g  Translated Text: Linux is crawly. Life is awesome.


You can also do exchange by specifying regular expression. Following instance does the substitution of hey or hi to hai. Please notation that this does not practice any substitution for the words 'they', 'this'.

:%s/\<\(hey\|hi\)\>/hai/yard
  • \< – word boundary.
  • \| – "logical or" (in this example hey or how-do-you-do)

Example 8. Interactive Detect and Supersede in Vim Editor

You can perform interactive discover and supplant using the 'c' flag in the substitute, which will ask for confirmation to do exchange or to skip information technology as explained below. In this example, Vim editor volition practice a global find the word 'awesome' and replace it with 'wonderful'. Merely it will do the replacement only based on your input as explained below.

:%s/awesome/wonderful/gc  replace with wonderful (y/n/a/q/l/^E/^Y)?
  • y – Will replace the current highlighted give-and-take. After replacing it will automatically highlight the next discussion that matched the search blueprint
  • n – Volition non supervene upon the current highlighted word. But it will automatically highlight the next word that matched the search design
  • a – Will substitute all the highlighted words that matched the search criteria automatically.
  • fifty – This will supersede only the current highlighted word and cease the discover and replace effort.

Example 9. Substituting all lines with its line number.

When the string starts with '\=', it should be evaluated every bit an expression. Using the 'line' function we tin get the electric current line number. Past combining both the functionality the exchange does the line numbering of all lines.

:%s/^/\=line(".") . ". "/one thousand


Annotation: This is different from the ":gear up number" where it will non write the line numbers into the file. But when you use this exchange y'all are making these line number available within the file permanently.

Example 10. Substituting special character with its equivalent value.

Substituting the ~ with $Abode variable value.

Original Text: Current file path is ~/test/  :%s!\~!\= expand($Dwelling)!k  Translated Text: Current file path is /domicile/ramesh/test/

You can use expand function to utilise all available predefined and user defined variables.

Instance 11. Alter sequence number in a numbered list while inserting a new item

Assume that you lot accept a numbered list similar the post-obit inside a text file. In this instance, let us presume that you want to add a new line after Article 2. For this, you should change the number of all other articles accordingly.

vi / vim tips & tricks serial Article i: Vi and Vim Editor: iii Steps To Enable Thesaurus Selection Article 2: Vim Autocommand: 3 Steps to Add Custom Header To Your File Article iii: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Article 4: Half dozen and Vim Macro Tutorial: How To Record and Play Article v: Tutorial: Brand Vim as Your C/C++ IDE Using c.vim Plugin Commodity half dozen: How To Add together Bookmarks Within Vim Editor Article 7: Make Vim as Your Bash-IDE Using bash-support Plugin Article 8: 3 Powerful Musketeers Of Vim Editor ? Macro, Mark and Map Commodity 9: viii Essential Vim Editor Navigation Fundamentals Article 10: Vim Editor: How to Correct Spelling Mistakes Automatically Article 11: Transfer the Power of Vim Editor to Thunderbird for Email Article 12: Catechumen Vim Editor to Beautiful Source Code Browser


3rd Commodity "Brand Vim every bit Your Perl IDE Using perl-support.vim Plugin" got missed. Then when you desire
to add it, then you desire to change "Article 3" to "Article iv", "Article 4" to "Article five", upto "Commodity 12" to "Article 13".

This can exist achieved by the following vim substitution command.

:4,$south/\d\+/\=submatch(0) + one/
  • Range: 4,$ – 4th line to last line.
  • Pattern to Search – \d\+ – digits sequence
  • Blueprint to Supplant – \=submatch(0) + one – gets the matched pattern and adds 1 to it.
  • Flag – as there is no flag, by default it substitutes only the showtime occurrence.


Subsequently executing the substitute statement the file volition become like this, where you can
add the 3rd Article.

vi / vim tips & tricks series Article 1: Vi and Vim Editor: iii Steps To Enable Thesaurus Option Commodity 2: Vim Autocommand: 3 Steps to Add Custom Header To Your File Article iv: 5 Awesome Examples For Automatic Word Completion Using Ctrl-X Article five: 6 and Vim Macro Tutorial: How To Record and Play Article 6: Tutorial: Brand Vim every bit Your C/C++ IDE Using c.vim Plugin Commodity 7: How To Add Bookmarks Inside Vim Editor Article 8: Make Vim as Your Fustigate-IDE Using fustigate-back up Plugin Article nine: three Powerful Musketeers Of Vim Editor ? Macro, Mark and Map Commodity 10: viii Essential Vim Editor Navigation Fundamentals Article 11: Vim Editor: How to Right Spelling Mistakes Automatically Article 12: Transfer the Power of Vim Editor to Thunderbird for Email Article thirteen: Convert Vim Editor to Cute Source Code Browser

Note: Cheque the commutation changed the 3 to 4, 4 to five and and then on. At present we can add together a new line mentioning information technology as Commodity 3, and no need to do whatever manual changes.

Instance 12. Substituting the sentence ancestry with upper example. ( i.due east title instance the unabridged document ).

While formatting a document, making the title case is also an important thing. It can be done hands with exchange.

:%s/\.\s*\due west/\=toupper(submatch(0))/1000
  • \.\s*\westward – Search Blueprint – literal . ( dot ) followed past Naught or more space, and a discussion character.
  • toupper – converts the given text to upper example.
  • submatch(0) – returns the matched blueprint.

Text before substitution:

Lot of vi/vim tips and tricks are available at thegeekstuff.com.          reading          these articles will make you very productive.          following          activities can be done very easily using vim editor.         a.          source          code walk through,         b.          record          and play command executions,         c.          making          the vim editor as ide for several languages,         d.          and          several other @ vi/vim tips & tricks.

Text afterwards commutation (Changes are in bold)

Lot of vi/vim tips and tricks are available at thegeekstuff.com.          Reading          these articles volition make you very productive.          Following          activities tin can be washed very easily using vim editor.         a.          Source          code walk through,         b.          Record          and play control executions,         c.          Making          the vim editor as ide for several languages,         d.          And          several other @ vi/vim tips & tricks.

Recommended Reading

Vim 101 Hacks, by Ramesh Natarajan. I'chiliad a command-line junkie. So, naturally I'g a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor feel, I've written Vim 101 Hacks eBook that contains 101 practical examples on various avant-garde Vim features that will make you fast and productive in the Vim editor. Fifty-fifty if you've been using Vi and Vim Editors for several years and take not read this volume, please practise yourself a favor and read this book. You'll exist amazed with the capabilities of Vim editor.

Awesome Vim Editor Articles

Following are few awesome Vi / Vim editor tutorials that you lot might find helpful.

  • Half dozen and Vim Macro Tutorial: How To Tape and Play
  • Turbocharge Firefox Browser With Vim Editor Functionality Using Vimperator Add-on
  • Tutorial: Brand Vim as Your C/C++ IDE Using c.vim Plugin
  • Convert Vim Editor to Beautiful Source Code Browser for Any Programming Language

Notation: Please subscribe to The Geek Stuff and don't miss any hereafter Six and Vim editor tips and tricks.

martinezthattery.blogspot.com

Source: https://www.thegeekstuff.com/2009/04/vi-vim-editor-search-and-replace-examples/

0 Response to "Vim How to Search Words When Reading Man"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel