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" operatoriw— 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 asfunctionname(text) - Use
Finstead offto 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)fwraps 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 usecs()thenysiwf) dstdeletes surrounding tags (HTML);dsfwould delete the function wrapper but this is not built-in — useds(to remove the parens, then delete the function name manually- Works with
vim-repeat(.) to repeat the wrap operation on subsequent text objects