Emacs

Introduction

This is a very short list with some of the shortcuts and facilities that make me an emacs user. They are not intended to proivde explanations of the functionalidades. For that, there are some links in the sections. Maybe in the near future I add some explanations in the document. So far, it is for people who may want a quick cookbook but who already know how to use emacs. Most of the instructions contain shortcuts created by me, for my personal convenience. I am constantly updating this file as I start using new packages of tricks.

.emacs File

Basics

C- : stands for control+ the key that follows (ex: C-x o)
S- : stands for shift+
A- : stands for command+
M- : stands for Alt (option)
M-x: stands for esc+x and is used to execute command in emacs

To check the keybuilding in a specific mode C-h m

For keybuilding instructions, see here

Search/Replace words

Function command Original shortcut Modified
Search      
Reg-exp search (return lines with match) occur    
Search word isearch-forward   A-f (C-S for next occurrence)
       
Replacement      
Replace reg-exp in region replace-regexp   A-r

Moving and navigation

Table 1: navigate
Function command Original shortcut Modified
Cursor      
Next word forward-word M-f  
Prev word backward-word M-b  
End of line move-end-of-line C-e  
Beg of line move-beginning-of-line C-a  
Next Head (same level)   C-c C-f  
Prev Head (same level)   C-c C-b  
Next Head   C-c C-n  
Prev Head   C-c C-p  
Jump from head to head   C-c C-j  
       
Windows      
Move cursor window at left/right, … windmove-left/right/…   A-<left>,<rignt>…
One window delete-other-windows   A-1
Split window below split-window-below   A-2
Split window right split-window-right   A-3
Return # windows before splintting delete-window   A-4
       
Tabbar      
Next tab tabbar-forward-tab   C-}
Prev tab tabbar-backward-tab   C-{
Next tab group tabbar-forward-group   C-)
Prev tab group tabbar-backward-group   C-(
       
Bookmarks      
create a bookmark for the current buffer   C-x r m  
jumpt to a bookmark   C-x r b  
delete a bookmark by name   M-x bookmark-delete  
list your bookmarks   C-x r l  
after C-x r l, use   D to mark bookmark  
    X to delete marked  
    R to rename  
       
Jump to directory of te file in buffer   M-x dired-jump  
Open buffer list   C-x C-b  

For more commands and details visit this page

Org-mode

Text Editing

Table 2 show some text modifiers

Table 2: text modificers
command effect
*Bold* Bold
/Italic/ Italic
_Underline_ Underline
= Code = Code in the text line
~ Verbatim ~ verbatim env in text line
Internal Cross-reference <<sec:cr>>
   
Selecting text  
C-@ start to select a region of the text
M-h select the paragraph or region or line
   
Narrowing  
C-x n s narrow to current *s*ubtree (section, subsection)
C-x n b narrow to current code *b*lock
C-x n n narrow to current region (between point and mark)
C-x n e narrow environment, as equation, etc. Note that it is
  possible to display the equation in image in the narrowed buffer.
C-c ` edit cell in a separate window
   
C-x n w go back/widen
see more here  
Bullet Lists  
C-c ^ Sort the list according to some criterion as time edited, alfabetical order, etc
S- <left> <right> With the prompt on the bullet mark, it changes the bullet style
C-c C-c Check the checkbox itemization
   
See more here  
Hyperlinks  
[[path to file][label] ] will open the file specified in “path to file”. It is useful to
   
  use open-with package to open some files
  as pdf, for instance, in external programs
[[url][label] ] will open the url in the browser
[[file+emacs:path to foder][label] ] will open the folder in a emacs buffer

autocompletion

Table 3: Autocompletion in org-mode
key Autocompletion
<s <tab> #+BEGIN_SRC … #+END_SRC
<e #+BEGIN_EXAMPLE … #+END_EXAMPLE
<q #+BEGIN_QUOTE … #+END_QUOTE
<v #+BEGIN_VERSE … #+END_VERSE
<c #+BEGIN_CENTER … #+END_CENTER
<l #+BEGIN_LaTeX … #+END_LaTeX
<L #+LaTeX:
<h #+BEGIN_HTML … #+END_HTML
<H #+HTML:
<a #+BEGIN_ASCII … #+END_ASCII
<A #+ASCII:
<i #+INDEX: line
<I #+INCLUDE: line

Equations

More information you can find here.

Key building Effect
C-c C-x C-l Covert equation env in image in the cursor position
  Note: the other will be reconverted in latex text
C-u C-u C-c C-x C-l Visualize equations and other environments in the buffer
C-u C-c C-x C-l Convert all the equations into image
C-c C-c Return all the images to latex text
M-x eq Return only the equation in current position to lates text
  Note: functionality created in .emacs file using lisp block

; return to show the latex code for current equation under the cursos
; position instead of image and keep all the other as image
(defun req ()
(interactive)
(let ((overlays (overlays-at (point))))
(mapc (lambda (ov)
(when (eq (overlay-get ov ‘org-overlay-type) ‘org-latex-overlay)
(setq org-latex-fragment-image-overlays
(delete ov org-latex-fragment-image-overlays))
(delete-overlay ov)))
overlays)))

With numbers

Which produce:

\begin{eqnarray} \hat{f}(x) & \propto & \sum_{\nu} \frac{|F(\nu)H(\nu)|^2}{|N(\nu)|^2} \frac{G(\nu)}{H(\nu)} e^{\frac{2 \pi i \nu x}{N}}\\ & \propto & \sum_{\nu} \frac{|F(\nu)|^2}{|N(\nu)|^2} H(\nu) H^*(\nu) \frac{G(\nu)}{H(\nu)} e^{\frac{2 \pi i \nu x}{N}}\\ & \propto & \sum_{\nu} H^*(\nu) G(\nu) e^{\frac{2 \pi i \nu x}{N}} \end{eqnarray}

Latex

package command description
org-ref org-ref-extract-bad-entries list all the broken links in the org-file
org-ref org-ref-extract-bibtex-entries extract the bibentries used in the file
org-ref ref:<nome do label> link equation, figure, table, etc, even usgin \label{}
    Note: if we use #+NAME instead of \label{}, we use [[] to refer to the figure inside the buffer, and it will export correctly to Latex.
  eqref:<equation label> same, but it apears with parentesis
  pageref:<label of enr> appear the page (olny) where the obj was printed (table, figure, etc)
  nameref:<label of enr> for sections, etc appear the name/caption of the section
    for the begining of the files open a buffer with all the tables in the document (click in the link to check)
    same for figures
input file #+BEGIN_SRC latex \lstinputlisting[firstline=a,lastline=b]{""} #+END_SRC  

Citations and Cross-reference

Description Command
Citation with page [[cite:key][comment, as pg. X, for instance] ]

It is possible to create cross-reference for section first writing, for instance,

* Internal Cross-reference
<<sec:cr>>

and then using [[sec:cr]]. In such case it will show just Section sec:cr

Tables

Table 4: Managing table
Function command original shortcut Modified
adjust the width of the column in the buffer <number>    
Insert table   ! <tab>*  
Insert line   ! <tab>  
       
Realign column   <tab> or C-c C-c  
       
Move current column left (rignt)   M-left (right)  
Move corrent line down (up)   M-down (up)  
       
Insert column (at left)   M-S-right  
kill current column   M-S-left  
Insert row (above)   M-S-down  
kill current row   M-S-up or A-<del>  
       
Insert line BELOW current line   C-c –  
Insert line BELOW+BLANK LINE current line a   C-c <RET>  
       
Sort table lines by current column   C-c ^  
       
edit cel in separate window org-mode-edit-field C-c ` (C-c C-c to go back)  
       
       

OBS: ! stands for | For heading of latex table, see here For more about tables see (see org-mode page, also here, and here) *: there are other easy way to insert tables automatically. For instance:

  1. | kasdkjf | aksdjfh|-<tab> you create a table
  2. jkhadsf, asdfl, lasdfklj
    laksdjh,asdkfl,sdk
    and them select the region and C-c |, and a table will be created.
  3. Very useful: if a column is too large, insert <N>, where N stands for a paramenter to defined to width of the column in the buffer (not in the latex export or so). Set the cursor in a line and press C-c ` to edit the line in a separate buffer. C-c C-c to return. Putting the mouse on the colunm it pops-up such information just provided here in this item.

Code block

Table 5: Parameters for code block
Instruction Description
#+Caption: Caption of the code block
#+name: Label of the code block
#+begin_src <langauge> basic begining of the code block
, #+end_src Print the #+end_src at the end of the code block
#+end_src finish code block
:result The type of result. It can be executed in section of nonsection. Section works if :section is present in the code #+begin_src head
  Non-section
  – value evaluate a code block as it were inside a function. So, we need to specify the return()
  output The code block is evaluated in the language and the org-mode print the result as text (stdout of the language
  Section
  Same, the difference is that for output you see the whole output without concatenation
:exports – code Export the code, but not the result of its evaluation
  results Export the result, but not the code that generated that
  both Export both code and result of the evaluation
  none Neither code or result is exported
:file the file to save the results of code evaluation
:tangle To use this, we have first to insert #+PROPERTY: tangle yes in the head of the document
  C-c C-v t : in the current file, it will create another file with the codeblocks, with the appropriate extension (e.g. .R).
  C-c C-v f : will execute the tangle instruction in the specified file
  There are three possibilities:
  1. Including #+PROPERTY: tangle yes in the header of the file will include all the code block and create a file with the same name of the current file, but with the appropriate extension
  1.1 If we include :tangle no in the line with #+begin_src <langauge>, the code block will not be tangled
  2. Not Including #+PROPERTY: tangle yes in the header:
  2.1 You have to include :tangle yes in the line with #+begin_src <langauge>.
  3. File name: :tangle filename will create a file with such name in the folder of the current file. All the blocks with the same file name will be saved in the same file. If there is no name, the block will be saved in the file with the same name of the current file
#+BEGIN_SRC latex\lstinputlisting[firstline=a,lastline=b]{""}#+END_SRC Include source from file to be printed in latex outputs

Information about key building here.

Example with Plot in R

Execute and show both the code and the result of the code block, write:

#+begin_src R :exports both :results graphics :file Plots/example.png :tangle no
plot(1:10)
#+end_src

Such code will produce:

plot(1:10)

example.png

We can also silent the code block and show only the plot. You just write:

#+begin_src R :exports results :results graphics :file Plots/example2.png :tangle no
par(las=1,cex.axis=.7,bty='l', pch=20, cex.main=.9, mar=c(4,5,3,2)) 
plot(1:10)
#+end_src

And you get simply:

example2.png

We can also insert a plot we have saved before, and them we can insert a caption, as in graphic 3

example2.png

Figure 3: A figure

Another example, with code block, var and Plot

So suppose we have a data like

Them we may perform a regression and the result is

And we can also plot the result of the regression, as in the fig-LinReg

plot(data$V3, data$V1)

./plot/exampleLinearRegression.pdf

Full document: evaluate inline code, tangle coe block, printing table, and plot.

Instructions

Desctiption Function Comment
Execute code block C-c C-c Inside the code block, it executes it
    if :exports results the execution will show the results below, and the code will not appear in the exported file
    It can be: results/code/both
    :results output will result in the result of the evaluation. For tables, write output latex (see Example)
in line code block C-c C-c src_R[headings]{“code”}
    The command C-c C-c inside the code will execute according to the heading instruction.
Tangle C-c C-v t will tangle the code and save in a file as specified
    if the name of the file is the same in two code block in the tangle instruction, the code will be put in the same file
     

add properties head and environment

commands description
C-c C-x p include properties in heading

org-ref

Dired

Shortcut Description
C-x C-j Go to parent directory
(alt/opt)-o Dired ommit mode (ommit irrelevant files)
   

Latex/AucTex

Compile using Auctex

  1. In AucTex mode: C-c C-c, option Latex
  2. In AucTex mode: C-c C-c, option Bibtex
  3. In AucTex mode: C-c C-c, option Latex again
  4. In AucTex mode: C-c C-c, option Latex and again

Insert envoronments

command description
C-c C-e Insert environment, it asks which one (equation, align, etc), and asks for the label
C-c C-s Insert Section of chapter, or subsection (it asks which one and the optional label)
C-c ] Close the envoronment that is open

see also this file

org2blog

Function mommand Shortcut Alias
log-in M-x org2blog/wp-login   blog-in
initiate new a post M-x org2blog/wp-new-entry   blog-new
post buffer as draft M-x org2blog/wp-post-buffer C-c d blog-postb
post buffer as page draft M-x org2blog/wp-post-buffer-as-page C-c D  
publish buffer C-u M-x org2blog/wp-post-buffer C-c p  
publish buffer as page C-u M-x org2blog/wp-post-buffer-as-page C-c P  
when used inside the buffer, it delets the entry in the website M-x org2blog/wp-delete-entry    
idem, but delets the page M-x org2blog/wp-delete-page    
it asks you to provide the postid that you want to delete C-u post-id M-x org2blog/wp-delete-entry    
It is a key-building functionality that will enable C-c d, C-c p and C-c t (complete tag name when inserting it at the top of the page) M-x org2blog/wp-mode    
turn in the wordpress sourcecode /sourcecode for code blocks org2blog/wp-use-sourcecode-shortcod    
log-out M-x org2blog/wp-logout   blog-out

Snippet

Mirror a field with transformation

The code below trigger the theorem environment with 8th <TAB>, and fill authomatically the \label with the heading of the theorem you have typed, substituing unwanted characters of the heading as space, punctuation (./,\$#%$\() and so on) by “-“. It also substitute any upper case character by its respective lower case char. Pressing <TAB> you jump from heading to label in case you want to change it, and then with <TAB> again you jump to the body of the theorem environment. First you have to define and load a function (I load it in the .emacs file so that it is available when I run emacs).

(defun string-for-latex-labels (string)
  (interactive)
  "format automatic labels for latex environment to use with snippet"
  (setq string  (replace-regexp-in-string "[[:punct:]]" "-" string))
  (setq string  (replace-regexp-in-string "[[:space:]]" "-" string))
  (setq string  (replace-regexp-in-string "-+" "-" string))
  (setq string  (replace-regexp-in-string "^-" "" string))
  (setq string  (replace-regexp-in-string "-$" "" string))
  (setq string  (downcase string))
 )

The final snippet is (see also here):

# -*- mode: snippet; require-final-newline: nil -*-
# name: theorem
# key: 8th
# binding: direct-keybinding
# --
\begin{theorem}[$1]
\label{thm-${2:${1:$(string-for-latex-labels yas-text)}}}
$0
\end{theorem}

Bibliography management

I have a master .bib file that contains all my bilbiography in a Dropbox folder. I use this file to load the bibliography whenever I am working (my Mac or Ubuntu). Using org-mode (or latex-mode) org-ref (or ref-tex in latex) I can easily access them.

In my emacs file I have the following code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s