Difference between revisions of "Emacs and org-mode notes"

From Notes_Wiki
m
m
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
<yambe:breadcrumb>Emacs</yambe:breadcrumb>
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[Text editors]] > [[Emacs]] > [[Emacs and org-mode notes]]
 
=Keyboard shortcuts=
=Keyboard shortcuts=


Line 97: Line 98:
| C-;                            || Autocorrect spelling of word under cursor.  Repeating C-; will cycle through various auto-correct options.
| C-;                            || Autocorrect spelling of word under cursor.  Repeating C-; will cycle through various auto-correct options.
|-  
|-  
| C-c C-,                        || Insert begin_src / end_src block while giving option to select language for the source.  See https://emacs.stackexchange.com/questions/40571/how-to-set-a-short-cut-for-begin-src-end-src
|-
|}
|}


Line 211: Line 214:




=Content types=
==Useful content which exports properly in both HTML and PDF (LaTeX)==
{| border="1" cellspacing="5" cellpadding="10"
|-
| '''Content''' || '''Description'''
|-
| \bullet || To print a bullet mark
|-
|}
   
   


Line 269: Line 281:
<pre>
<pre>
#+CAPTION: &lt;caption&gt;
#+CAPTION: &lt;caption&gt;
#+ATTR_HTML: alt="&lt;alt-text&gt;"
#+ATTR_HTML: :alt "&lt;alt-text&gt;" :title "&lt;title-text&gt;"
#+LABEL:      &lt;label&gt;
#+LABEL:      &lt;label&gt;
#+ATTR_LaTeX: :width \textwidth
#+ATTR_LaTeX: :width \textwidth
Line 278: Line 290:
<pre>
<pre>
#+CAPTION: All types of files
#+CAPTION: All types of files
#+ATTR_HTML: alt="All types of files" title="All types of files"
#+ATTR_HTML: :alt "All types of files" :title "All types of files" :width 500px
#+LABEL:      fig:all-files
#+LABEL:      fig:all-files
#+ATTR_LaTeX: :width \textwidth
#+ATTR_LaTeX: :width \textwidth
Line 330: Line 342:
and then refer to section using '<tt>\ref{&lt;section-label&gt;}</tt>'
and then refer to section using '<tt>\ref{&lt;section-label&gt;}</tt>'


==Table captions and labels==
One can give caption and label to tables so that table appears with caption and also so that one can refer to the table using table number later on.  To give caption and label to a table use:
<pre>
#+CAPTION: &lt;Caption for table&gt;
#+LABEL:  &lt;Label for table&gt;
</pre>
Then table can be referred later using '<tt>Table\nbsp\ref{&lt;Label for table&gt;}</tt>'.  '''Note that \nbsp sequence should not be used inside tables as it causes problems with LaTeX export'''.




Line 420: Line 422:
</pre>
</pre>
Here after src instead of latex we can use shell, python, etc.  Specifying the type of source does not seem to make any difference.
Here after src instead of latex we can use shell, python, etc.  Specifying the type of source does not seem to make any difference.
==Org tables related tips==
===Table captions and labels===
One can give caption and label to tables so that table appears with caption and also so that one can refer to the table using table number later on.  To give caption and label to a table use:
<pre>
#+CAPTION: &lt;Caption for table&gt;
#+LABEL:  &lt;Label for table&gt;
</pre>
Then table can be referred later using '<tt>Table\nbsp\ref{&lt;Label for table&gt;}</tt>'.  '''Note that \nbsp sequence should not be used inside tables as it causes problems with LaTeX export'''.




==Vertical lines in org-tables==
===Vertical lines in org-tables===


To get vertical lines separating columns in tables exported by org use a special row containing '/' in first column and then '<' or '>' to indicate start or end of a column group in other columns.
To get vertical lines separating columns in tables exported by org use a special row containing '/' in first column and then '<' or '>' to indicate start or end of a column group in other columns.
Line 435: Line 447:
</pre>
</pre>
Learned from http://orgmode.org/manual/Column-groups.html#Column-groups
Learned from http://orgmode.org/manual/Column-groups.html#Column-groups
===Aligning content in org-tables===
To align contents in org-table use &lt;c&gt; or %lt;r&gt; etc. on a dedicated row as shown in below example:
<pre>
#+NAME: Stuff_I_Want_To_Sell
|----------------------+--------|
|        <c>          |    <r> |
| Stuff I Want To Sell  | Amount |
|----------------------+--------|
|        Book        |    20 |
|      Old bike      |    120 |
|----------------------+--------|
|        Total        |    140 |
|----------------------+--------|
#+TBLFM: @>$2=vsum(@3..@-1)
</pre>
Refer: https://emacs.stackexchange.com/questions/71486/org-mode-column-view-align-values-right-or-center
===Using org-tables as spreadsheets===
Org-tables can be used as spreadsheets. To figure out address of a cell use "C-c ?".  Example formulas are
{| class="wikitable"
|-
| :=vsum(D2..D5) || Sum of cells D2 to D5
|-
| =D4*D5        || Product of D4 and D5 cells
|-
| =D5*1.14      || 1.14 times value of D5 cell
|-
| =round(D5*1.14) || 1.14 times value of D5 cell rounded to a nearest integer
|-
|}
Press C-c C-c on #+TBLFM: line that gets generated automatically.  Please note @12$4 typically means twelfth row fourth column. 
Learn more about using org as a spreadsheet at http://orgmode.org/worg/org-tutorials/org-spreadsheet-intro.html




Line 505: Line 556:




==Using org-tables as spreadsheets==


Org-tables can be used as spreadsheets. To figure out address of a cell use "C-c ?".  Example formulas are
{| class="wikitable"
|-
| :=vsum(D2..D5) || Sum of cells D2 to D5
|-
| =D4*D5        || Product of D4 and D5 cells
|-
| =D5*1.14      || 1.14 times value of D5 cell
|-
| =round(D5*1.14) || 1.14 times value of D5 cell rounded to a nearest integer
|-
|}


Press C-c C-c on #+TBLFM: line that gets generated automatically. Please note @12$4 typically means twelfth row fourth column.
==Org-babel mode shortcut for editing source in other major mode==
Use C-c ' to open current source code block in its own major mode.  After editing use C-C ' to pull the changes from major mode back into the org-source block.
 
More on org structure editing at http://orgmode.org/manual/Structure-editing.html


Learn more about using org as a spreadsheet at http://orgmode.org/worg/org-tutorials/org-spreadsheet-intro.html
More about org-babel can be learned using https://www.youtube.com/watch?v=dljNabciEGg








<yambe:breadcrumb>Emacs</yambe:breadcrumb>
[[Main Page|Home]] > [[CentOS]] > [[CentOS 6.x]] > [[Text editors]] > [[Emacs]] > [[Emacs and org-mode notes]]

Latest revision as of 11:10, 21 May 2024

Home > CentOS > CentOS 6.x > Text editors > Emacs > Emacs and org-mode notes

Keyboard shortcuts

File related shortcuts

Shortcut Description
C-x C-s Save file
C-x d List directory contents so that we can choose which file to edit from directory listing


Editing shortcuts

Shortcut Description
C-u <number> <command> Repeat command that follows given number of times.
C-g Cancel previous command. Note that we cannot use Esc and we should use C-g to cancel previous command.
C-x u
C-_
C-/
Undo
C-k Kill from cursor position to end of line
C-@ or C-<space> Start selecting text from cursor position
C-w Cut selected text
C-y Paste text from clipboard to current location
M-y This has to be used just after using C-y. This would change the pasted text to older text that was cut or killed. Emacs remembers all the different killed texts and we can paste older text by using M-y repeatedly after we have used C-y once to restore latest killed text.
C-x C++ To increase font-size
C-x C+- To decrease font-size
M-q To wrap paragraph properly as per set fill-width value
C-s Forward incremental search
C-s C-s Repeat last incremental search
C-r Reverse incremental search
M-l Convert word to lower case
M-u Convert word to upper case
M-c Convert word such that first alphabet is capital
C-x C-l Convert region to lower case
C-x C-u Convert region to upper case


Org-mode shortcuts

Shortcut Description
<pipe><hypen> followed by tab To create a table in org mode. The same shortcut also completes a table row
M-ret To split long text within table so that it spans multiple lines.
C-c C-q Apply tag to given section, sub-section etc. Note that tags cannot be applied to bullets started with -
Tab (at heading) Change detail of display of sub-headings of current heading
Shift-tab (at first heading) Change detail of display of all sub-headings in file
C-c . Insert active date using calendar. Calendar accepts options such as . (today), +1 (tomorrow), -1 (yesterday), etc. One can also use Shift+<arrow_keys> to change selected date in displayed calendar
C-c ! Insert inactive date
C-c . <choose_date> C-c . Insert active date range
M-<left-arrow> Decrease level of sub-section or sub-sub-section heading. That is convert sub-section to section or sub-sub-section to sub-section etc.
M-<right-arrow> Increase level of section or sub-section heading. That is convert section to sub-section or sub-section to sub-sub-section etc.
C-c C-s Schedule an event (Shortcut for adding SCHEDULED: )
C-c C-d Mark dead-line for an event (Shortcut for adding DEADLINE: )
Shift-<left/right-arrow> Change bullet and numbering type
C-c C-e Org-export dispatcher to export to pdf, html, etc. formats
C-; Autocorrect spelling of word under cursor. Repeating C-; will cycle through various auto-correct options.
C-c C-, Insert begin_src / end_src block while giving option to select language for the source. See https://emacs.stackexchange.com/questions/40571/how-to-set-a-short-cut-for-begin-src-end-src


View related shortcuts

Shortcut Description
C-x 0 Delete current window
C-x 1 Expand current buffer to fullscreen and hide all other buffers
C-x 2 Split current window into two sections
C-c C-o Visit link
C-x k kill buffer
C-x b switch buffer (Press tab for auto-complete and suggestions)
C-x C-b List all buffers
C-x o Switch to other visible buffers
C-x <right> Go to next buffer
C-x <left> Go to previous buffer
C-l Center current line in view buffer
C-l C-l Make current line first line of current view buffer



Help related shortcuts

Shortcut Description
C-h k <key> Gives help about key in a split window
C-h m Mode help
C-h a PATTERN Help on commands that match given pattern
C-h d PATTERN Help on functions and variables that match given pattern
C-h b List of current key bindings
C-h c Displays command run by given key sequence


Commands

Generic commands

Command Description
buffer-menu Shows list of all buffers to choose from
ispell-buffer Check text in current buffer for spelling errors. One should shift to flyspell-mode by using 'flyspell-mode' command before using ispell for correcting spelling errors. By defaul emacs uses hunspell and stores the private dictionary in '~/.hunspell_en_US' file.


Org-mode commands

Command Description
org-table-delete-column To delete column from org mode table
org-mode To change to org mode. In case extension of file being edited is not .org, we may need to shift to org mode manually.
org-odt-export-to-odt Export org file to libreoffice format. Later it can be saved as doc (MS-Word) format using libreoffice



Configuration

General configuration

Configuration option Description
(setq make-backup-files nil) Prevents emacs from creating backup files while saving
(global-visual-line-mode 1) Enables visual-line-mode in all buffers by default
(set-fill-column 60) To ensure lines wrap at 60 in text-modes. After this also change mode using auto-fill-mode command.
(setq-default fill-column 60)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
Use these two enable automated form-fill for 60 columns when emacs starts
(set-face-attribute 'default nil :height 160) Unit is 1/10pt, so for 10pt font 100 should be used
(setq x-select-enable-clipboard t) Make emacs copy-paste M-w, C-y work with system copy-paste
(setq auto-save-default nil) Preventing emacs from creating auto-save files


Content types

Useful content which exports properly in both HTML and PDF (LaTeX)

Content Description
\bullet To print a bullet mark


Tips / Techniques

To do list using org mode

We can build todo lists using org-mode. For that we should create bullted list using - below a heading or a sub-heading. The hypen should be followed by [ ] empty square braces. We should also put [1/1] in front of sub-heading / heading under which we have built bulleted list. Then we can use C-c C-c shortcut to mark / unmark any item and the count gets reflected at top of list.

Pop-buffer menu using mouse click

If we use single left click while keeping control key pressed on any buffer then we get popup buffer menu for switching buffers which can be very useful to switch buffers when working on multiple files.


Linking to other files

To link to other files using org-mode syntax is:

[[URL][Title]]

where, URL can be of many different formats such as:


Tagging sections or sub-sections

To tag sections or sub-sections using org first we need to add TAGS line such as:

#+TAGS: Saurabh(b) Swetha(w) Swaroop(r) Mahesh(m) Nirmala(n)

and then use C-c C-c short-cut on TAGS line to make emacs reload the buffer. Then one can go to any section or sub-section (Note that it wont work for bulleted or numbered list) and use C-c C-q shortcut to assign a tag. Note often instead of C-C C-q one may press C-x C-q which turns document to read-only mode. Repeat C-x C-q again to make document writable for tagging or other editing to work.


Using org-timeline

Org can prepare a time-line for sections, sub-sections etc. which have data 'SCHEDULED:' as their body. This feature is very useful to mark events which are fixed such as weekly meetings or important appointments which cannot be rescheduled or missed unless emergency. To create a weekly recurrent schedule event one can use something like:

** TODO Friday VLEAD meetings from 11:00am to 01:00pm
   SCHEDULED: <2012-12-21 Fri 11:00-13:00 +1w>

Note that in this case once the meeting date is passed, the org-timeline will still continue to show the same old date such as 2012-12-21 in above case. To make the date point to next week (+1w) one can use C-c C-t short-cut on the section title. It would generate few extra lines which can be optionally removed.

Similarly to schedule a one-time event in a to-do list one can use something like:

** To do list for [2012-12-28 Fri]
*** Select TA(s) for Virtualization course at 03:30pm
    SCHEDULED: <2012-12-28 Fri>

All such events weekly or one-time can then be viewed in a useful manner using 'org-timeline' command.


Including pictures in org-mode

To include image in org-file such that it appears properly in both HTML and LaTeX outputs use:

#+CAPTION: <caption>
#+ATTR_HTML: :alt "<alt-text>" :title "<title-text>"
#+LABEL:      <label>
#+ATTR_LaTeX: :width \textwidth
[[<path-to-image-file>]]

For example,

#+CAPTION: All types of files
#+ATTR_HTML: :alt "All types of files" :title "All types of files" :width 500px
#+LABEL:      fig:all-files
#+ATTR_LaTeX: :width \textwidth
[[./all_files.png]]

Apart from :width \textwidth, :placement [H] can be used to indicate [Here only] placement for figures.

Note that just specifying name of file with "file:<path>" without using [[ and ]] for linking may not work.

Learned from http://orgmode.org/manual/Images-in-LaTeX-export.html and http://orgmode.org/manual/Images-in-HTML-export.html


Recording and playing macros

Emacs supports recording of keyboard macros and later playing them to achieve desired repeatability. Various short-cuts related to recording and playing macros are:

Keyboard shortcut Description
C-x ( Start recording keyboard macro
C-x ) Stop recording keyboard macro
C-x e Play recorded macro
C-u <n> C-x e Play recorded macro n times

Note that unlike vim, emacs only supports recording of one macro at at time.


Linking sections in org-mode

There are two methods of linking to sections in org-mode: 1. Through name 2. Through number

Linking to another section using name

To link to other section or sub-section using name use '[[<section-name>]]' where section-name should be exactly as specified while defining the section. This will generate link to another section using name.


Linking to another section using section number

If link to another section is desired by section number instead of name then label the section using

#+LABEL: <section-label>

and then refer to section using '\ref{<section-label>}'


Changing default application handler for given extension

We can link to external files using [[file:<path>]] links and also visit them using C-c C-o. To change the default application which handles a given extension we can use following steps:

  1. Click on org-menu at top of Emacs window
  2. Choose customize -> Browse Org Group option
  3. Go to "Group Org Link" and then to "Group Org Follow Link"
  4. Click on "Option Org File Apps"
  5. Adding and editing process should be clear. The variable basically stores list of tuples where first describes extension and second the handler with %s being string to be replaced with filename.
  6. Change the association and remember to choose option "Save for future sessions" so that modified value appears in .emacs file for future associations.


Get Bibtex to work with org-export-as-pdf

org-export-as-pdf uses variable org-latex-to-pdf-process (This variable is renamed to org-latex-pdf-process from org version 8.0 onwards). To see the value of variable use

C-h v org-latex-to-pdf-process RET

If we change the value of variable by appending following to .emacs

;;To set proper command for exporting latex to pdf from org-mode
(setq org-latex-to-pdf-process (quote ("pdflatex %f" "bibtex %b" "pdflatex %f")))
(setq org-latex-pdf-process (quote ("pdflatex %f" "bibtex %b" "pdflatex %f")))

Then org-export-as-pdf works without any problem. Note that on first run errors for undefined citation may be displayed, but all future runs should be fine.

For specifying bibliographystyle and bib file use something like this in references section.

#+LATEX: \bibliographystyle{plain}
#+LATEX: \bibliography{papers/ms_thesis}

Finally any citation can be done using normal '\cite{cloud_advantage' the way it is done in LaTeX.

Part of steps learned from http://lists.gnu.org/archive/html/emacs-orgmode/2013-05/msg00797.html Learn more at http://orgmode.org/worg/org-tutorials/org-latex-export.html


Suppress table of contents generation

To suppress generation of table of contents in document export options specify:

#+OPTIONS: toc:nil 


Supress title page generation

To supress generation of tile in pdf export use:

#+LATEX_HEADER: \renewcommand{\maketitle}{}


Include LaTeX package or specify class-options while exporting to LaTeX from org

To include LaTeX package or specify class-options for current documentclass while exporting to LaTeX from org use:

#+LaTeX_CLASS_OPTIONS: [a4paper,twoside,twocolumn]
#+LATEX_HEADER: \usepackage{xyz}

To hide boxes around links from pdf export of org-mode use:

#+LATEX_HEADER: \hypersetup{hidelinks}

In other versions of emacs, org-mode or LateX (eg CentOS) use:

#+LATEX_HEADER: \hypersetup{pdfborder={0 0 0}}

Learned from http://orgmode.org/worg/org-tutorials/org-latex-export.html


Include other files as example/verbatim in org

To include other files such as sources, scripts, etc. in org in verbatim style use:

   #+INCLUDE: "<relative or absolute path>" src latex

Here after src instead of latex we can use shell, python, etc. Specifying the type of source does not seem to make any difference.

Org tables related tips

Table captions and labels

One can give caption and label to tables so that table appears with caption and also so that one can refer to the table using table number later on. To give caption and label to a table use:

#+CAPTION: <Caption for table>
#+LABEL:   <Label for table>

Then table can be referred later using 'Table\nbsp\ref{<Label for table>}'. Note that \nbsp sequence should not be used inside tables as it causes problems with LaTeX export.


Vertical lines in org-tables

To get vertical lines separating columns in tables exported by org use a special row containing '/' in first column and then '<' or '>' to indicate start or end of a column group in other columns.

For example:

     |-------------------------------------+--------------------+------------|
     | *Operation*                         | *(Specify Yes/No)* | *Comments* |
     |-------------------------------------+--------------------+------------|
     | /                                   | <>                 | <          |
     |-------------------------------------+--------------------+------------|

Learned from http://orgmode.org/manual/Column-groups.html#Column-groups


Aligning content in org-tables

To align contents in org-table use <c> or %lt;r> etc. on a dedicated row as shown in below example:

#+NAME: Stuff_I_Want_To_Sell
|----------------------+--------|
|         <c>          |    <r> |
| Stuff I Want To Sell  | Amount |
|----------------------+--------|
|         Book         |     20 |
|       Old bike       |    120 |
|----------------------+--------|
|        Total         |    140 |
|----------------------+--------|
#+TBLFM: @>$2=vsum(@3..@-1)

Refer: https://emacs.stackexchange.com/questions/71486/org-mode-column-view-align-values-right-or-center


Using org-tables as spreadsheets

Org-tables can be used as spreadsheets. To figure out address of a cell use "C-c ?". Example formulas are

:=vsum(D2..D5) Sum of cells D2 to D5
=D4*D5 Product of D4 and D5 cells
=D5*1.14 1.14 times value of D5 cell
=round(D5*1.14) 1.14 times value of D5 cell rounded to a nearest integer

Press C-c C-c on #+TBLFM: line that gets generated automatically. Please note @12$4 typically means twelfth row fourth column.

Learn more about using org as a spreadsheet at http://orgmode.org/worg/org-tutorials/org-spreadsheet-intro.html


Literate programming and tangling file

It is possible to write source code blocks in org-file and then export them to a external desired source file using tangle. To do this following properties should be defined at top:

#+PROPERTY: session *scratch* 
#+PROPERTY: results output 
#+PROPERTY: tangle build/test.yaml 
#+PROPERTY: exports code 

Then the source should be written between:

+BEGIN_SRC YAML
+END_SRC

blocks. Finally the output file suggested in PROPERTY can be created using 'M-x org-babel-tangle' with shortcut C-c C-v t. Note that build folder must exist if tangle output is expected to go to build/test.yaml. Emacs or babel will not create the folder as part of tangle operation. This is just a quick way to export yaml or erlang code using emacs. But babel supports executing code for languages such as C, C++, python, perl, shell etc. directly from emacs using babel and even the output can be captured directly into other block. More information on this is available at http://orgmode.org/worg/org-contrib/babel/

To send individual source blocks to different files use:

+BEGIN_SRC YAML :tangle <file-path>
+END_SRC


Tangling file or exporting file using shell-command

It is possible to tangle file or export file using external shell commands. Example shell command for tangling a literate programming .org file is:

emacs src/site.txt --batch -f org-mode -f org-babel-tangle --kill

Note that if extension is not .txt then "-f org-mode" might not be required.

Similarly to export any org file to HTML, following shell command can be used:

emacs src/site.txt --batch -f org-mode -f org-html-export-to-html --kill

Apart from -f emacs also supports -l for running elisp code or for including .emacs file which may not be included by default in batch mode.


Prevent underscore (sub-script) and super-script in org-mode export

It is hard to use _ in org_mode as it leads to creation of sub_scripted text and sometimes escaping with \ also fails. To prevent org from treating _ specially use:

#+OPTIONS: ^:nil

at top of the org-file

Learned from http://stackoverflow.com/questions/698562/disabling-underscore-to-subscript-in-emacs-org-mode-export


Linking to section in other org file using name

While creating an org-project which can be published as described at Using org-publish to publish org project it is important to be able to refer to other org-files or specific section in other org-files. To refer to other org-files normal linking such as:

[[./other-org.org]]

is sufficient. Please note use of ./ for absolute path and also use of .org extension to indicate link to org file. The pdf and html exported versions automatically link to pdf and HTML files created using publishing of other-org.org file and not to original source org file. To link section of other org-file using:

[[file:other-org.org::*<Section-name>][<Title>]] 

For example:

[[file:common_vars.org::*Common Variables][Common vars]] 

Note that one star or asterisk (*) in front of section name has nothing to do with actual number of stars in org-file in front of section. Thus even when linking to sub-sections or sub-sub-sections only one star is required.



Org-babel mode shortcut for editing source in other major mode

Use C-c ' to open current source code block in its own major mode. After editing use C-C ' to pull the changes from major mode back into the org-source block.

More on org structure editing at http://orgmode.org/manual/Structure-editing.html

More about org-babel can be learned using https://www.youtube.com/watch?v=dljNabciEGg



Home > CentOS > CentOS 6.x > Text editors > Emacs > Emacs and org-mode notes