I know, I know, this is probably old news to must regular users of RStudio. But these things are so useful - how had I not heard about them before?
In short, “code snippets” are little abbreviations that you can use when writing your code. You type a few letters and then hit TAB, in the same way you do to autocomplete functions; but a snippet can be used to insert a whole code template, for example, a for
loop or switch
statement.
And what’s more, you can add your own snippets! Within the Code tab of the global options menu, there’s a little “Edit snippets” button:
And then you can see the built-in snippets (in a whole bunch of languages), and add your own. This post from RStudio explains the syntax (and, indeed, explains the whole concept of snippets better than I have done here!).
In my case, I wanted a quick way to add section dividers to my R scripts: so I’ve defined three new snippets for “single-” and “double-divider” and for a “heading” divider.
Which work like so:
Type two letters, hit TAB. Much quicker than holding down the equals key!