Search code examples
javascriptvimcode-snippetsvscode-snippetsultisnips

How can you capitalize this word in VIM snippet?


EDIT — This is not possible with coc-snippets. It's possible with Ultisnips.

I've got the following vim snippet that I'm using (with coc-snippets) for React:

snippet STATE_HOOK "useState hook" b
const [${1:state}, set${1:`!v expand('%:t:r')`}] = useState($2)
endsnippet

This could be used to quickly create the following (incorrect) code:

const [color, setcolor] = useState("green");

The problem is that the setcolor needs to be camelcased, like this: setColor

How would one write this snippet so that the expanded input is capitalized?


Solution

  • I was testing some different things and you can use:

    snippet STATE_HOOK "useState hook" b
    const [$1, set${1/\w+/\u$0/g}] = useState("$2")
    endsnippet 
    

    How it works: Checking the documentation I found that you should overrite the text with the same text but capitalized, so \w+takes all the text (I think that '+' is unnecesary) and overrite it with \u$0 (is the same text but capitalized)

    enter image description here