YASnippet and Babel

Org-mode allows you to use Babel. As it says in the manual:
Babel is Org-mode’s ability to execute source code within Org-mode documents

Here’s a bit of Java, wrapped up in Babel

#+BEGIN_SRC java :classname example
    public class example
    {
            public static void main (String args [])
            {
                System.out.println("Go Babel!");
            }
    }
#+END_SRC

Put the point in the block and

C-c '      to edit the code
C-c C-c     to run the code

In the past, I never used Babel as often as I should, mainly because I could never quite remember the syntax.

Then I had the idea of adding Babel to YASnippet.  I added the following to my org-mode snippets:

# -*- mode: snippet -*-
# name: jbabel
# key: jbabel
# --
#+BEGIN_SRC java :classname $1
    public class $1
    {
            public static void main (String args [])
            {
                System.out.println("$0");
            }
    }
#+END_SRC

… and now I just have to type jbabel and hit tab to have my Babel block ready to go.

It works with ditaa too. As I could never quite remember all the codes when I needed them, I just put a few of ones I used most frequently in a YASnippet example:

# -*- mode: snippet -*-
# name: ditaa babel
# key: dbabel
# --
#+begin_src ditaa :file $1.png
/-----------------\
| Things to do    |        +-----+
| cGRE            |    |{s}  |
| o Cut the grass *-+----->|     |
| o Buy jam       |    |     |
| o Fix car       |    +-----+
| o Make website  |
\-----------------/
#+end_src

And here’s what it looks like when converted (if you’re reading this on Google+, you might want to head over to my website: TonyBallantyne.com to see the image)
dit

5 Comments

  1. Xi says:

    Thanks again for your excellent use-case!
    I am a new emacs user, who have spent a month time studying it. I have embraced one of most famous .emacs setups in github as foundation of my own setups.
    It was so thrilling when I :
    started the setup customization
    swapped keys and enabled sticky keys feature to stay away from RSI
    now, I am ready to start the Org mode journey , which is said to be a master piece work in emacs!

    Like

  2. admin says:

    You’re welcome! And enjoy org-mode!

    Like

  3. Grant Rettke says:

    Great idea to keep a reminder right in the snippet itself.

    Like

    1. admin says:

      Cheers!

      Like

Leave a Comment