0

I want to create a custom function that:

  • Take 1 argument
  • Search for a word
  • On that line do a movement like f@
  • change till " (ct") with the argument I provided

Example:

Library "sharedLibrary@xyz"

command zyv should produce

Library "sharedLibrary@zyv"

0

1 Answer 1

2

If you're comfortable with ex-commands, you can use something like this in your function

function MyFn(replacement) abort
  /word
  substitute/@\zs.\{-}\ze"/\=a:replacement/
endfunction

We search for word, then substitute everything between @ and the first " (hence non-greedy matching with \{-}) with the argument a:replacement.

With functions/:normal!, it would be something like

call search('word')
execute printf('normal! f@ct"%s', a:replacement)

Which I think is a bit harder to read.


Another alternative would be to use a macro: I won't save/restore it, but that's not hard to add.

let @q = '/word<C-v><CR>f@ct"'.a:replacement.'<C-v><esc>'
@q

But I don't know of a good way to avoid embedding the control characters literally (you have to type Ctrl-V and the corresponding key (enter, escape) inside the string).

1
  • 1
    Nice it works and then I did this mapping: :nnoremap <buffer> <leader> can :call MyFn(input('Param: '))<CR>
    – Kerby82
    Oct 14, 2020 at 19:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.