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

  • Receiving Error 'ORA-01041: internal error. hostdef extension doesn't exist.' on re-establishing a connection to Oracle.
    You can receive the error message 'Error ORA-01041: internal error. hostdef extension doesn't exist' when attempting to re-esta...
  • 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...
  • Troubleshooting the Microsoft Exchange Transport Service When It Fails to Start on an Exchange 2007 Hub Transport Server with Event ID 1032 or 1036
    Event ID: 1032 Socket Access Denied. Binding: 0.0.0.0:25. Event ID: 1036 Failed to open one or more bindings. The service will be stopped....
  • C Traps and Pitfalls (Addison Wesley, 1989, English)
    C Traps and Pitfalls teaches how the peculiarities of the C language make it easy for the intended behavior of a program and the actual beha...
  • SoftMaker Office 2008 Free Full Version Download With Genuine License Key
    SoftMaker Office 2008 is reliable office productivity software that is designed for beginners and professional users. SoftMaker Office Suite...
  • Amazon Takes the Humans Out of Fulfillment With New API
    keeps adding to its Web services. Today, it is opening up an API for its Fulfillment by Amazon service, which allows online merchants to out...
  • AP Microsoft backs Icahn's bid to oust Yahoo board
    SAN FRANCISCO - Microsoft Corp. threw its weight behind investor Carl Icahn's effort to dump Yahoo Inc.'s board, saying Monday that...
  • Suba Vesak Mangallayak Weewa !!!!
    Keep your heart free from hate your mind from worry, Live simply expect little, give much Fill your life with love forget self, think of ot...
  • How to Remove Dosearches.com homepage (Virus Removal Guide)
    Dosearches.com  is a browser hijacker, which is promoted via other free downloads, and once installed it will change your browser homepage t...
  • Alcohol 120% v5 Blu-ray Burner Free Download with Registration Unlock Code
    Alcohol 120% is a CD and DVD burning, copying, backup, recording, duplication, emulation and creation software for Windows. In addition to c...

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