vimtricks.wiki Concise Vim tricks, one at a time.

How do I wrap a word in a function call using vim-surround?

Answer

ysiwf

Explanation

vim-surround's f and F surrounds let you wrap any text object inside a function call, prompting you for the function name. Unlike parenthesis-only surrounds, f adds the function name before the opening paren, turning any word or selection into a function invocation.

How it works

  • ys — "you surround" operator
  • iw — inner word text object (works with any motion or text object)
  • f — function call surround type; Vim prompts for the function name
  • Type the function name and press <CR> — vim-surround wraps it as functionname(text)
  • Use F instead of f to add spaces inside the parens: functionname( text )

Example

Cursor on value:

return value

Type ysiwf, then parseInt<CR>:

return parseInt(value)

This also works in visual mode: select text with v, then S + f + function name.

Tips

  • Combine with any text object: ysa)f wraps the contents of the outer parentheses in a function call
  • To change an existing function call's name, use csff — it will prompt for the old and new function names (actually use cs() then ysiwf)
  • dst deletes surrounding tags (HTML); dsf would delete the function wrapper but this is not built-in — use ds( to remove the parens, then delete the function name manually
  • Works with vim-repeat (.) to repeat the wrap operation on subsequent text objects

Next

How do I re-insert the text from my last insert session and immediately return to normal mode?