-1

I want to create a function such that when I execute it (:'<,'>call Foo()) with some text selected, it will set my command-line as ':%s/selected_text/selected_text/gc' so that I can edit the destination pattern manually before executing

I can write the function to execute the substitute command but

  1. command-history will not have that execute command, only the call Foo() line
  2. that would still be less than ideal as I would prefer if I could avoid executing at all

Is there a 'set command-line' command maybe?

2
  • 1
    I think you want a mapping, not a function. The mapping should yank the selected text and construct command inserting the yanked text two times. As you don't want to execute the constructed command yet don't append <CR> at the end of the constructed command-line. Commented Jun 18 at 19:03
  • @phd I really wanted a function. It looks like I need to write a remap that will call the function from which that part is extracted Commented Jul 16 at 18:18

1 Answer 1

1

As mentioned in the comments, a function doesn't seem to be strictly needed, here.

The lowest-level approach is commonly found in people's vimrc:

 xnoremap <key> y:%s/<C-r>"//gc<Left><Left><Left>

where we:

  1. yank the visually selected text into the unnamed register with y,
  2. insert a stub Ex command in the command-line, containing the content of the unnamed register,
  3. move the cursor a few characters left, where we can type the replacement text.

which looks like this in action:

substitute currently selected text

See :help c_ctrl-r and :help registers.

It works as-is but it is a bit crude. Possible improvements include:

  • do not pollute registers,
  • sanitize the search pattern before inserting it (escape slashes, etc.),

which would definitely require one or more functions to be written.

Sign up to request clarification or add additional context in comments.

1 Comment

What I want is to be able to select long things like say some_Garbage_cAsING and be able to edit it on the command line before executing a function that will run some further transformations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.