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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:55:31+00:00 2026-06-16T23:55:31+00:00

I am writing a vimscript function to wrap the currently selected lines (visual mode)

  • 0

I am writing a vimscript function to wrap the currently selected lines (visual mode) in a Ruby begin-rescue block. I am getting some strange behavior. Here is my function:

function! BeginRescueWrap()
  execute "normal! gvd"
  let head = "begin\<CR>"
  let body = @"
  let tail = "rescue StandardError =>e\<CR>binding.pry\<CR>end\<CR>"
  execute "normal! i" . head . "\<CR>" . body . "\<CR>" . tail
endfunction

It almost works– it just produces two copies of the head and tail for some reason. For example, running the function when this text is selected:

Oh lord won't you buy me a Mercedes Benz
My friends all drive Porsches, I must make amends

Produces this output:

begin

  begin

  rescue StandardError =>e
  binding.pry
  end

  Oh lord won't you buy me a Mercedes Benz
  My friends all drive Porsches, I must make amends

  rescue StandardError =>e
  binding.pry
  end

I don’t care about the indentation (can fix that later). Just notice that there is an outer wrapping that includes an empty inner wrapping followed by the target lines. What am I doing wrong?

  • 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-16T23:55:33+00:00Added an answer on June 16, 2026 at 11:55 pm

    The problem is that Vim will execute your method twice, because you selected 2 lines. If you add the range parameter to the method it will only be executed once, and you will have your desired effect:

    function! BeginRescueWrap() range
      execute "normal! gvd"
      let head = "begin\<CR>"
      let body = @"
      let tail = "rescue StandardError =>e\<CR>binding.pry\<CR>end\<CR>"
      execute "normal! i" . head . "\<CR>" . body . "\<CR>" . tail
    endfunction
    

    Note though that for this specific purpose, a mapping like this would probably be more concise:

    vnoremap ,q sbegin<cr><C-R>1rescue StandardError =>e<cr>binding.pry<cr>end<cr>
    

    This mapping for ,q removes the currently selected text, writes the begin statement, then puts what was just deleted, then writes your ending ending.

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

Sidebar

Related Questions

I'm trying to learn vimscript by writing a small plugin to display marks using
I am writing a vim plugin in vimscript where I need to search another
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
Writing documentation in html requires some code examples. What to do with characters that
I'm writing a vbscript to pull some data from a webpage, strip out a
I am currently writing a JavaScript script for the Microsoft JScript Runtime Environment. It's
I've been trying to write a bit of Vimscript to call a function when
I'm writing a piece of vimscript to manage sessions. I would like to let
I am writing a VBScript in VB.Net to automate some operation in SAP GUI.
Writing a templated function, I declared: template <typename T> T invertible(T const& container, T::size_type

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.