Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:50:27+00:00 2026-06-04T22:50:27+00:00

I find my myself frequently with a problems that need to combine regular expressions,

  • 0

I find my myself frequently with a problems that need to combine regular expressions, some kind of movement, and copying and pasting. I’m sure there is some way to do this in vim but I haven’t seen anything bringing it all together. As an example right now the problem I am trying to solve is like this.

  1. Find a line that is /^Description “(.*)”/
  2. Save the captured text
  3. Go to the line that is /^TEXT “(.*)-/
  4. Replace the captured text above with the captured text from step 2
  5. Repeat as necessary moving forward through the file

Does anyone know of a way to quickly automate this type of task in VIM?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:50:30+00:00Added an answer on June 4, 2026 at 10:50 pm

    I haven’t learned all the new-fangled vim-only features; I learned vi before vim and this solution will work in any vi-ish editor. There might be an even better solution using vim features, perhaps.

    What I would usually do in this case is use the :map command to bind several keys, each one doing part of the above. You need to pick some keys you can live without for a while; I often use g and v for this. @Neil Forrester suggested using function keys, which is a great idea.

    Now, you showed regular expression patterns with parens indicating a match group. (In vi or vim, you actually need to put a backslash before each paren to make it “magic”; see the documentation.) For this solution, however, I am instead going to use the f command, which (f)inds a specified character; and/or the t command, which jumps un(t)il a character. f jumps up to a character, where t jumps just before a character. So, with f" we can jump to the first double-quote of a string, and then using t" we can jump until just before the second double quote. So, the sequence f"lyt" would find the first double-quote, move one char to the right, then yank everything until the next double quote. But, let’s store the yanked text into one of the 26 named buffers; let’s just use buffer “a”: f"l"ayt" This is a little bit confusing, because we must use "a to refer to named buffer “a” but we have lots of other " characters that we are looking for.

    Also, within a “map” you may need to record a keystroke for the Enter key. The way you do that is to hit Ctrl+V, then hit the Enter key. This will display as ^M. In my code below, if you see ^M it is not intended to mean an actual ^ followed by an actual M but rather a single key that represents the Enter key.

    So now, we can make our two key mappings. Let’s bind v to do steps 1 and 2, and g to do steps 3 and 4.

    :map v /^Description "/^Mf"l"ayt"
    
    :map g /^TEXT "/^Mf"ldt""aP
    

    Don’t forget, use Ctrl+V and Enter rather than actually typing ^M, so you can’t just copy/paste the above without editing it.

    Now, use the v key to do steps 1 and 2, and the g key to do steps 3 and 4. By alternately hitting the two keys you can do a lot pretty quick.

    There might also be a way to do this by using scripting in vim, but on the other hand, you might just want to write a short Python script (or your favorite language) if you want to script this. The two key macros, above, really do provide a fast way to do this sort of thing in vim.

    vim has some sort of feature for recording keys as you type them, which I think can be used to quickly create this sort of macro.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing SQL code (queries or stored procs) to solve problems that
I frequently find myself writing utility classes that can be re-used throughout my projects.
With some frequency I find myself writing APIs that offer an Iterator<Foo> which is
I frequently find myself writing max value functions that search through an array of
I frequently find myself having a need to create a class as a container
I frequently find myself doing some analysis in R and then wanting to make
I find myself needing to use Perl more and more since PHP's regular expressions
When creating reports I find myself frequently joining to ad-hock sub queries like this:
I find myself very frequently wanting to write reusable strings with parameter placeholders in
I frequently find myself creating classes which use this form (A): abstract class Animal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.