Skip to main content
Fix typo.
Source Link
Ingo Karkat
  • 18k
  • 1
  • 45
  • 63

You can do it very simply with:

function! ReplaceThisWithThat(this, that)
    execute "%s:/" . a:this . "/" . a:that . "/g"
endfunction

Which will substitute every occurrence of a:this with a:that in the whole buffer.

If you want to perform that substitution on the content of some variable or register, you can use substitute() in your function:

function! ReplaceThisWithThat(something, this, that)
    return substitute(a:something, a:this, a:that, "g")
endfunction

which doesn't make much sense.

That said, everything you need to know is in :help eval.txt.

You can do it very simply with:

function! ReplaceThisWithThat(this, that)
    execute "%s:" . a:this . "/" . a:that . "/g"
endfunction

Which will substitute every occurrence of a:this with a:that in the whole buffer.

If you want to perform that substitution on the content of some variable or register, you can use substitute() in your function:

function! ReplaceThisWithThat(something, this, that)
    return substitute(a:something, a:this, a:that, "g")
endfunction

which doesn't make much sense.

That said, everything you need to know is in :help eval.txt.

You can do it very simply with:

function! ReplaceThisWithThat(this, that)
    execute "%s/" . a:this . "/" . a:that . "/g"
endfunction

Which will substitute every occurrence of a:this with a:that in the whole buffer.

If you want to perform that substitution on the content of some variable or register, you can use substitute() in your function:

function! ReplaceThisWithThat(something, this, that)
    return substitute(a:something, a:this, a:that, "g")
endfunction

which doesn't make much sense.

That said, everything you need to know is in :help eval.txt.

Source Link
romainl
  • 42.3k
  • 5
  • 87
  • 118

You can do it very simply with:

function! ReplaceThisWithThat(this, that)
    execute "%s:" . a:this . "/" . a:that . "/g"
endfunction

Which will substitute every occurrence of a:this with a:that in the whole buffer.

If you want to perform that substitution on the content of some variable or register, you can use substitute() in your function:

function! ReplaceThisWithThat(something, this, that)
    return substitute(a:something, a:this, a:that, "g")
endfunction

which doesn't make much sense.

That said, everything you need to know is in :help eval.txt.