13

For example I have an org mode that has the following

#+BEGIN_SRC emacs-lisp


#+END_SRC

Is it possible to configure yasnippets in such a way so all emacs-lisp-mode snippets can be expanded inside that block but not outside it?

Rafa de Castro
  • 1,231
  • 10
  • 14
  • 6
    I think the "standard" way would be to edit those blocks with `C-c '` anyway - in that case, the emacs-lisp major mode would have the appropriate yasnippet snippets loaded while editing the block in its separate window (that also takes care of the right indentation etc.) – VanLaser Sep 11 '16 at 20:25
  • Yes, I agree on VanLaser response. It works for any language supported by emacs. – dmg Dec 18 '16 at 04:57
  • Oh true. You are right. Can you put this on a response so I mark your response as correct? I didn't thought about that :P – Rafa de Castro Dec 18 '16 at 19:18

3 Answers3

9

As of 2017, Jan 22, if you set org-src-tab-acts-natively and org-src-fontify-natively, then TAB in source blocks will expand snippets of the block's language. You may want to set yas-buffer-local-condition to stop org mode snipppets from shadowing the block mode's snippets:

(defun my-org-mode-hook ()
  (setq-local yas-buffer-local-condition
              '(not (org-in-src-block-p t))))
(add-hook 'org-mode-hook #'my-org-mode-hook)

See also https://github.com/joaotavora/yasnippet/issues/761, https://github.com/joaotavora/yasnippet/pull/760

npostavs
  • 9,033
  • 1
  • 21
  • 53
2

I use two methods for this. I have some org snippets to help me create the babel headers. For example, this one creates C++-14 headers for Babel. I type <s C++_ and expand:

# -*- mode: snippet -*-
# name: c++_header
# key: C++_
# --
C++ :main no :flags -std=c++14 -Wall --pedantic -Werror :results output :exports both
#include <iostream>
int main()
{
   $0

   return 0;
}

Then I use C-c ' which switches to the specific language mode and use the snippets for that language. Then use C-c ' to get back to the org file. It works well.

dmg
  • 629
  • 4
  • 13
0

As npostavs already answerered, snippets are expanded differently depending on the languages within one org-mode file.

Just type "co" and [tab] within Python or Emacs Lisp code block.

#+BEGIN_SRC python :session *Python* :results output
co[tab]
# comment
#+END_SRC

#+BEGIN_SRC emacs-lisp :results value scalar
co[tab]
                                        ; comment
#+END_SRC
RUserPassingBy
  • 1,078
  • 7
  • 14