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 8808811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:42:53+00:00 2026-06-14T02:42:53+00:00

I would like to find a good way in Vim to do this search

  • 0

I would like to find a good way in Vim to do this search and replace for the following –

name_1
end
name_2
end

name_3
end

where I take name_”x” and append it to it’s following “end” statement. So it would look like this –

name_1
end_name_1
name_2
end_name_2
name_3

end_name_3

Note – the empty line before name_3 ruins a constant line spacing between “name_x” and “end”. So I was thinking of something like

:g/\(name_x\)/,/end/ s/end/end_\1/

Now that doesn’t work trying to store the first pattern like that but hopefully you get the idea. Any tricks here?

Thanks

  • 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-14T02:42:54+00:00Added an answer on June 14, 2026 at 2:42 am

    The issue you are having is that \1 refers to the current substitution’s match group not the :g command’s pattern. Sadly you can not access previous match groups from previous searches. There are however ways to accomplish your goal. The first I would suggest is a macro.

    First off start on a a non matching line. This can be done by inserting a blank line at the top of the file if needed (just remove it afterwards).

    1. start the macro search for your pattern: /name_\d\+
    2. yank until the end of your pattern: y//e
    3. find the end pattern: /end/e
    4. insert a underscore a_
    5. paste with p
    6. end your macro q
    7. execute the macro with a large number. e.g. 999@q

    Here is the macro I used:

    :let @q = "/name_\\d\\+\<cr>y//e\<cr>/end/e\<cr>a_\<esc>p"
    

    The other way is to use the :global command in a similar fashion to your attempt.

    :g/name_\d\+/let @@=matchstr(getline('.'),'name_\d\+')|/end/s//\=submatch(0).'_'.@@/
    

    I personally this is much too complicated, but it is nice to have options. The command can be broken down into the following parts:

    • :g/name_\d\+ execute a command on each line matching name_ and some number
    • getline('.') will return a string of the current line
    • matchstr(getline('.'), 'name_\d\+') will return only the matching part of the current line
    • let @@ = matchstr(...) will set the matched portion to the default register
    • /end/s/ will execute a :s on the following line that matches end
    • s//\=.../ matches the last used pattern and replaces it with an express.
    • submatch(0).'_'.@@ create a string with the whole matched pattern followed by an underscore and the contents of the unnamed register.

    For more information on the following topics see:

    :h q
    :h search-offset
    :h :g
    :h :let-@
    :h registers
    :h matchstr(
    :h getline(
    :h range
    :h submatch(
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to find a way to store multiple addresses for one subject
I would like to find a way to see what happens while my XAML
I would like to find a way to compile and package our iPhone application
I would like to find a way to use custom User provider within a
This has probably been asked before, but I can't find a good way of
I would like to do something like this: jqueryElement.find('divIdWithIterator**').each(...); where 'divIdWithIterator**' matches all elements
I would like to find a good (up-to-date) Java Trail type beginning tutorial on
I would like to find the distance of every pixel coordinate in an image
I would like to find out which version of an executable the CMD shell
I would like to find out is as3 is doing is doing anything in

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.