Windows Support Tools

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 31 August 2008

Vim tips: The basics of search and replace

Posted on 20:33 by Unknown
Vim beginners may be puzzled when it comes to doing search and replace operations in Vim. The syntax can be a bit arcane, but after you've practiced, it becomes second nature. Let's take a look at how easy it can be.

Let's start by looking at searches and doing search and replace operations within Vim. You can do a search in normal mode by using /searchstring . This will search forward through the file for searchstring . Likewise, running ?searchstring will search backwards through the file.

After running a search once, you can repeat it by using n in command mode, or N to reverse direction.

When you want to search for a string of text and replace it with another string of text, you can use the syntax :[range]s/search/replace/. The range is optional; if you just run :s/search/replace/, it will search only the current line and match only the first occurrence of a term.

Most of the time, that's not sufficient, so you can add a range like so:

:8,10 s/search/replace/g

In that example the range is from line 8 to line 10. I've also added the "global" option, which tells Vim to replace every occurrence on a line, and not just the first occurrence. Without adding g, your search will match only the first instance of a string in any given line.

Another way to specify the range is to enter visual mode and select the lines that you want to search, and then press : to enter command mode. To enter visual mode from normal mode, press v to select regular visual mode, or V for line selection, or Ctrl-v for block selection. Then select the range in visual mode and press :, followed by the search command you wish to use.

If you want to search an entire file, you can use % to indicate that as the range:

:%s/search/replace/g

You may also wish to be asked for confirmation before Vim makes a substitution. To do this, add the confirm (c) option to the end of the search and replace command: :%s/search/replace/gc. When you run this search, Vim will give you a prompt that looks something like this:

replace with foo (y/n/a/q/l/^E/^Y)?

The "y" and "n" are self-explanatory, but what about the rest? To tell Vim to go ahead and replace all instances of the matched string, answer with a. If you realize that you don't really want to make the changes, you can tell Vim to quit the operation using q. To tell Vim to make the current change and then stop, use l, for last.

^E and ^Y allow you to scroll the text using Ctrl-e and Ctrl-y.

Where you land

Searches in Vim put the cursor on the first character of the matched string by default; if you search for Debian, it would put the cursor on the D. That's usually fine, but Vim has a few options for offsetting the cursor placement from the beginning of the matched string.

To land on the last character in the matched string, rather than the beginning, add an /e to your search:

/Debian/e

That will place the cursor on the n rather than the D. Vim also allows you to specify a cursor offset by line, or from the beginning or end of the string. To have the cursor land two lines above a matched string, for example, use /string/-2. To place the cursor two lines below the string, use /string/+2.

To offset from the beginning of the string, add a /b or /s with the offset that you want. For example, to move three characters from the beginning of the search, you'd use /string/s+3 or /string/b+3 -- "s" for "start" or "b" for "begin." To count from the end of the string, use /e instead, so /string/e-3 will place the cursor on the third character from the last character of the matched string.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • What is Cloud Computing ?
  • Setting up a DHCP server in Windows 2003
    The DHCP server assigns a client an IP address taken from a predefined scope for a given amount of time. If an IP address is required for lo...
  • Cartoons of the Week
  • Manage your home network better with Network Magic 5.0
    Cisco announced on Thursday Network Magic 5.0, a suite of network management software. This is is the first product released by Cisco since ...
  • Google Sinks Cash into Undersea Cable
    Google has joined the Unity consortium, which will build a $300 million fiber optic cable linking the US and Japan. The 7.68 Tbps, 10,000 ki...
  • Battle Progress Map
  • Exchange Server 2010 Beta available
    Microsoft Exchange Server 2010 brings a new and rich set of technologies, features, and services to the Exchange Server product line. This t...
  • Nambara Sanda Wage
  • Complete Wireless Design (McGraw-Hill, 2008, English)
    ON THE CD-ROM *PUFF RF/Microwave circuit simulation software *Sonnet Lite electromagnetic simulation software *National's PLL Design Pro...
  • Keyshia Cole - Just Like You (2007)
    1- Let It Go feat Missy Elliot and Lil Kim [03:58] 02- Didn't I Tell You feat Too Short [03:52] 03- Fallin Out [04:27] 04- Give Me More ...

Categories

  • ISA Firewall
  • ISA Server 2006
  • Quick Tips
  • Reports
  • Tips

Blog Archive

  • ►  2013 (7)
    • ►  October (6)
    • ►  February (1)
  • ►  2012 (10)
    • ►  September (3)
    • ►  August (3)
    • ►  July (3)
    • ►  June (1)
  • ►  2011 (16)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (1)
    • ►  March (4)
    • ►  January (1)
  • ►  2010 (12)
    • ►  December (3)
    • ►  October (4)
    • ►  March (1)
    • ►  January (4)
  • ►  2009 (67)
    • ►  August (2)
    • ►  July (18)
    • ►  June (23)
    • ►  April (3)
    • ►  March (6)
    • ►  February (7)
    • ►  January (8)
  • ▼  2008 (319)
    • ►  December (30)
    • ►  November (43)
    • ►  October (45)
    • ►  September (12)
    • ▼  August (18)
      • Vim tips: The basics of search and replace
      • Setting up DNS
      • Setting your ubuntu stytem hostname
      • Configuring Static IP address for your network card
      • RT - Installation Guides
      • How To Install And Use gDesklets On Ubuntu 8.04
      • User Authentication HOWTO
      • Kaspersky Internet Security 2009
      • Save Electricity
      • Active FTP vs. Passive FTP, a Definitive Explanation
      • Kodak introduces HD media hub
      • BitNami Alfresco Stack 2.1.0.7
      • User Management
      • How do I unzip tarballs with the 'bz2' extension?
      • DELON - Unstoppable
      • DIACAP
      • TechnoSNACK: a new aggregator for visibility of yo...
      • Learn about GanttProject
    • ►  July (27)
    • ►  June (15)
    • ►  May (7)
    • ►  April (55)
    • ►  March (65)
    • ►  January (2)
  • ►  2007 (10)
    • ►  December (2)
    • ►  November (8)
Powered by Blogger.

About Me

Unknown
View my complete profile