Search code examples
rvimultisnips

utltsnips - surround with space


I have the following utilsnips script that I use for Vim:

snippet - "assignment"
<-
endsnippet

I use it for R to expand a dash to the assignment operator. I would like to make it so that a space is put both before and after the <- on expansion. However, when I put a space before it in the snippet like <-, it won't expand on hitting Tab. How should I modify the script to have spaces around the operator? Desired result: <-.


Solution

  • You could use r option to include head and trailing spaces around snippets. r will treat snippet as a python regular expression and you should define your snippet within quotes when using this flag.

    snippet " -" "assignment" r
     <- 
    endsnippet
    

    Note that there is a space before and after <- in snippet definition.

    As a bonus, It's more interesting to define the snippet like the following:

    snippet " - " "assignment" rA
     <- 
    endsnippet
    

    A is autoexpansion. so now you dont need to hit tab anymore! just type - and as soon as you type space after - it will expand to <- Automatically.