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?
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
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.
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
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.)