Search code examples
emacsorg-mode

Change emacs org-mode key binding for code block


In org-mode I use <s then TAB to insert a code block. This action will insert a code block like

 #+BEGIN_SRC
    .
    .
    .
 #+END_SRC

but I want to modify this action to insert something like

#+BEGIN_SRC python -n :results output pp replace :exports both
    .
    .
    .
#+END_SRC

I know it's possible to change default behaviour of :result or :exports in emacs init file but I prefer to change this shortcut behaviour, because it makes me able to change the options in line.


Solution

  • Answering my own question based on legoscia's answer.

    As mentioned in the Easy Templates section of the org-mode manual, you can modify these templates by customizing the variable org-structure-template-alist. Using M-x customize-option and applying changes will add all easy templates to your init file, if you don't like it you can add just one line to your init file to change a template or add one.

    In my case I added this line to my emacs init file to add <p then TAB :

    (add-to-list 'org-structure-template-alist '("p" "#+BEGIN_SRC python -n :results output pp replace :exports both\n?\n#+END_SRC"))
    

    ALL CREDITS GOES TO legoscia