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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:06:56+00:00 2026-05-28T16:06:56+00:00

I have been looking to map a new operator in Vim that takes an

  • 0

I have been looking to map a new operator in Vim that takes an extra parameter.

For example, we know that ciw will “cut inside word” and will put you into Insert mode. What I am looking for is having a custom action to replace c (for example, s) that takes movements like iw, but requires an extra parameter.

A trivial example would be:

Given a line in a text file

Execute siw* in Normal mode (assuming the cursor is on the first column) for it to surround the first word with * like so:

*Given* a line in a text file

I know, this is what the most excellent surround.vim plugin does. But I am just giving an example here, and looking for an answer as to how to get the mappings so that the above work.

I tried playing with onoremap and opfunc, but can’t seem to get them to play the way I want.

So, what I am looking for is a combination of motions plus operator pending mappings.

  • 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-05-28T16:06:58+00:00Added an answer on May 28, 2026 at 4:06 pm

    Here is an example implementation of the command described
    in the question, for illustrative purposes.

    nnoremap <silent> s :set opfunc=Surround<cr>g@
    vnoremap <silent> s :<c-u>call Surround(visualmode(), 1)<cr>
    
    function! Surround(vt, ...)
        let s = InputChar()
        if s =~ "\<esc>" || s =~ "\<c-c>"
            return
        endif
        let [sl, sc] = getpos(a:0 ? "'<" : "'[")[1:2]
        let [el, ec] = getpos(a:0 ? "'>" : "']")[1:2]
        if a:vt == 'line' || a:vt == 'V'
            call append(el, s)
            call append(sl-1, s)
        elseif a:vt == 'block' || a:vt == "\<c-v>"
            exe sl..','..el 's/\%'..sc..'c\|\%'..ec..'c.\zs/\=s/g|norm!``'
        else
            exe el 's/\%'..ec..'c.\zs/\=s/|norm!``'
            exe sl 's/\%'..sc..'c/\=s/|norm!``'
        endif
    endfunction
    

    To get user input, the function InputChar() is used, assuming that
    the required argument is a single character.

    function! InputChar()
        let c = getchar()
        return type(c) == type(0) ? nr2char(c) : c
    endfunction
    

    If it is necessary to accept a string argument, change the call to
    InputChar() in Surround() to the call to input(), instead.

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

Sidebar

Related Questions

I have been looking for an elegant way to write a function that takes
Have been looking at the MVC storefront and see that IQueryable is returned from
I have been looking at the new database server we are setting up for
I have been looking for a script which lets you search a map for
I have been looking this but haven't found a concrete example. Is there a
I have been looking into IKVMing Apache's FOP project to use with our .NET
We have been looking at g++ versions 3.2.3 and 4.2.4. With 4.2.4, the performance
I have been looking at metrics for coupling and also look at DSM .
I have been looking for documentation related to interacting with MSPaint from the command
I have been looking at jQUery thickbox for showing modal dialogs with images, it

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.