Change emacs org-mode key binding for code block - emacs

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.

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

As of Org 9.2 the method employed by #shae128 is no longer valid. Instead you'll need to use tempo-define-template, like this:
(tempo-define-template "python-block"
'("#+begin_src python :results raw output"
n n p n n
"#+end_src")
"<p"
"Insert an outputting Python block"
'org-tempo-tags)
n stands for newline, p for where to leave the mark, <p for the command to expand from when hitting tab.
Thanks to Omar's answer here

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. (Use M-x customize-option.)
For <s, the default expansion is "#+BEGIN_SRC ?\n\n#+END_SRC". You can just edit it to include the options you want after BEGIN_SRC. Alternatively, you could add a new template, e.g. <p, that expands to the text you want.

Related

org-indent-line always adds a comma before an leading asterisk in code block

Launch Emacs with emacs -q and write in org-mode:
#+BEGIN_SRC C
/*
* This is a comment line
*/
#+END_SRC
Press TAB inside the block. It becomes:
#+BEGIN_SRC C
/*
,* This is a comment line
,*/
#+END_SRC
Is there any way to get rid of this behevior?
This is a quoting mechanism to make sure that the asterisks are not interpreted as headline markers and throwing off Org mode's parser. See Literal Examples in the manual.
There is no way to turn it off and you shouldn't anyway because Org mode will be very confused otherwise. The commas do not affect the exporting of the document or the evaluation of a source block: they are properly stripped at the appropriate time.

emacs org mod with math expression

I am using org-mod to take note of some of my work, I have recently switched from sublime where I was used to writing some mathjax snippet.
What would be the correct and easiest way of having this snippet rendered correctly (either in the buffer or if not possible, via C-c C-c or C-c C-e).
\hat{y} = \hat{\beta}_{0}+\hat{\beta}_{1}x_{1}+\hat{\beta}_{2}x_{2}+...+\hat{\beta}_{p}x_{p}
ps : in my .emacs conf file I don't have anything closely related to mathjax or Latex.
Kr,
Enclose the latex fragment in \( ... \) or \[ ... \]and do C-c C-x C-l (bound to org-toggle-latex-fragment). See the doc string of this function for argument usage.
In recent versions of org, you customize org-preview-latex-default-process to set it to the value of the backend you want. You need either dvipng, dvisvgm or ImageMagick as the backend. See also the doc string of the variable org-preview-latex-process-alist: it might help explain some things, although you don't have to do anything with it.
If these variables don't exist, you might be using an earlier version of org where the setup is different. If that's the case, then please post which version you are using: M-x org-version would tell you.
EDIT: in earlier versions, the function is called org-preview-latex-fragment and it's not bound to a key by default. You can call it with M-x org-preview-latex-fragment and you can get rid of the preview with C-c C-c. The backend setup is different as well: there is support for dvipng and ImageMagick only and the variables are different.

How do I prevent org-mode from executing all of the babel source blocks?

I have an org file with lots of babel source blocks in it that only need to be re-executed when the code is changed.
How do I prevent org from executing all of the blocks during export? In other words, set them all to manual execution only?
I would prefer a single global option rather than having to edit each block individually.
The variable org-export-babel-evaluate, if set to nil, will prevent any code from being evaluated as part of the export process. This way, only the results inserted by way of manual execution will be exported.
You can define it, and others, as a file variable by placing the following comment line at the top of your org file:
# -*- org-export-babel-evaluate: nil -*-
Setting the variable org-export-babel-evaluate to nil will avoid code evaluation, but it will also cause all source block header arguments to be ignored This means that code blocks with the argument :exports none or :exports results will end up in the export. This caught me off guard.
The alternative is to use the header argument :eval never-export on a file basis and then remove that line when re-running the source code:
#+PROPERTY: header-args :eval never-export
See the docstring for org-babel-evaluate:
Switch controlling code evaluation and header processing during export.
When set to nil no code will be evaluated as part of the export
process and no header arguments will be obeyed. Users who wish
to avoid evaluating code on export should use the header argument
‘:eval never-export’.
You can set the cache to yes (see http://orgmode.org/manual/cache.html). This can also be set a property line in the file to act globally.
#+Property: header-args :cache yes just make sure to C-c C-c on that line to activate the property.
After placing the following:
# -*- org-export-use-babel: nil;-*-
at the top of the file, and executing C-c C-c, It didn't work for me. The variable's value is not set accordingly.
But the following:
#+BIND: org-export-use-babel nil
with an application of C-c C-c works as expected.
I found the answer here: http://joelmccracken.github.io/entries/org-mode-specifying-document-variables-and-keywords/
There are probably some changes in emacs 26, which I'm using.

how to override the save-file command in org-babel

I really like the org-babel that enables me to organize my scripts in org-mode, however, i found there are some issues with the org src buffer when i edit the source code in a separate buffer (using keybinding C-c ').
first thing is, even i explicitly run write-file, and then specifies the file path and name to save, the buffer is not saved to that file, but the source code block in the .org file gets updated and the .org -file is saved.
second thing is, whenever i run save-buffer in org src buffer, the buffer screen will automatically scroll down till current mouse position is the last line in the buffer. this is annoying because sometimes i lose tracking my scripts.
i am not so familiar with elisp, and can only do simple work like define-key or add-hook, i hope i can get help from here. thanks
Org-babel is not meant for organizing scripts, but for including source code as part of your document.
You may be interested in tangling, which allows to join and extract source code blocks from your orgmode document into separate files.
The following example will merge 2 source code fragments into the file test.m when tangling (org-babel-tangle, bound to C-c C-v t):
* Tangling example
Set up a vector:
#+begin_src octave :tangle test.m
a = 1:10;
#+end_src
Then find out squares
#+begin_src octave :tangle test.m
b = a.^2
#+end_src

How to insert square brackets within org-mode in Emacs

I know that when you want to insert a link you can [[www.google.com][Google]], the square brackets will disappear, but when I want to insert the [[]] and show them like the following code example(I am learning bash and making some notes about it), how do I do??
if [[ "$gender" == f* ]]
BTW, when I put my cursor in the link descriptor(Google in the link example), but I don't want to open it with my Browser, how can I know the original url(www.google.com)?
You can use org-babel source blocks for scripts.
That looks like:
#+begin_src shell-script
if [[ "$gender" == f* ]]
#+end_src
Pressing C-c C-c inside such a block will execute it. The results are shown underneath. Pressing C-c ' gives you another buffer for editing the source block (in its own major mode).
You find a short description at: C-h i g (org) Literal examples.
BTW, the menu items Org → Hyperlinks → Descriptive Links and Org → Hyperlinks → Literal Links let you switch between descriptive and literal links.