Chenggang Liu

Notes on Emacs

Time management in Emacs

agenda mode C-c a X f forward in time b backward in time F follow mode

Installation Emacs from source

  • install all dependencies
sudo apt-get build-dep emacs24
tar -zxvf emacs-24.3.tar.gz
cd emacs-24.3
./configure
make
sudo make install

Automatically install missing package

(package-initialize)
(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
;; Install use-package
(setq package-list '(use-package))
(dolist (package package-list)
(unless (package-installed-p package)
 (package-install package)))
;; Let use-package to ensure a package is installed.
(use-package THE-PACKAGE-YOU-WANT-TO-INSTALL :ensure t)

helm mode settings

helm is a must-have package, it can Emacs run like a magic. https://tuhdo.github.io/helm-intro.html My settings:

(use-package helm :ensure t)
(use-package helm-swoop :ensure t)
(use-package helm-projectile :ensure t)

(require 'helm-config)
(helm-mode 1)

(define-key helm-map (kbd "<tab>") 'helm-execute-persistent-action) ; rebind tab to run persistent action
(define-key helm-map (kbd "C-i") 'helm-execute-persistent-action) ; make TAB work in terminal
(define-key helm-map (kbd "C-z")  'helm-select-action) ; list actions using C-z


(setq helm-buffers-fuzzy-matching t
helm-recentf-fuzzy-match    t)

;; === projectile ===
(projectile-global-mode)
(setq projectile-completion-system 'helm)
(helm-projectile-on)
(setq projectile-switch-project-action 'helm-projectile)
;; C-c p p switch projects
;; C-c p f find files in a project
;; C-c p F find files in projects

;; helm semantic
;; (semantic-mode 1)
;; (setq helm-semantic-fuzzy-match t
;;       helm-imenu-fuzzy-match    t)

;; hem-man-woman
(add-to-list 'helm-sources-using-default-as-input 'helm-source-man-pages)
(setq helm-locate-fuzzy-match t)

My key bindings:

;; keybinding for helm
(global-set-key (kbd "C-x i") `helm-complete-file-name-at-point)
(global-set-key (kbd "C-x b") `helm-mini)
(global-set-key (kbd "C-x C-b") `helm-buffers-list)
(global-set-key (kbd "C-x C-f") `helm-find-files)
(global-set-key (kbd "C-x C-r") `helm-recentf)
(global-set-key (kbd "C-x r b") `helm-bookmarks)
(global-set-key (kbd "M-x") `helm-M-x)
(global-set-key (kbd "M-y") `helm-show-kill-ring)

(global-set-key (kbd "C-c i") 'helm-semantic-or-imenu)


(global-set-key (kbd "C-x c o") 'helm-occur)
(global-set-key (kbd "C-h SPC") 'helm-all-mark-rings)
(global-set-key (kbd "C-c C-g") 'helm-google-suggest)
(global-set-key (kbd "C-p") 'helm-projectile-find-file)

Programming in Emacs

Find things at fingertip

  • Find files in a project helm projectile
  • Indexing (find function, variable definitions and references) If your project is not big, a good solution is rtags c++ indexer. But if your project is large, I recommend using global with ctags. It works really well and it is super fast
    • Installation
    • A script to generate index files

      #!/bin/bash
      find . -type d \( -name "skip_foler1" \
      -o -name "skip_folder2" \) -prune -o \
      -type f \( -name "*.cc" -o -name "*.hh" -o -name "*.py" -o -name "*.c" -o -name "*.h" -o -name "*.hidl" \)  \
      -print > file_list
      gtags -i -v -f file_list
      
    • Emacs settings

      ;; --------------------------------------------------
      (use-package helm-gtags :ensure t)
      (require 'helm-gtags)
      (setq
       helm-gtags-ignore-case t
       helm-gtags-auto-update t
       helm-gtags-use-input-at-cursor t
       helm-gtags-pulse-at-cursor t
       helm-gtags-prefix-key "\C-cg"
       helm-gtags-suggested-key-mapping nil
       )
      
      ;;; Enable helm-gtags-mode
      (add-hook 'dired-mode-hook 'helm-gtags-mode)
      (add-hook 'eshell-mode-hook 'helm-gtags-mode)
      (add-hook 'c-mode-hook 'helm-gtags-mode)
      (add-hook 'c++-mode-hook 'helm-gtags-mode)
      (add-hook 'asm-mode-hook 'helm-gtags-mode)
      (add-hook 'python-mode-hook 'helm-gtags-mode)
      
      ;; (define-key helm-gtags-mode-map (kbd "C-c g a") 'helm-gtags-tags-in-this-function)
      (define-key helm-gtags-mode-map (kbd "C-j") 'helm-gtags-select)
      (define-key helm-gtags-mode-map (kbd "M-.") 'helm-gtags-dwim)
      (define-key helm-gtags-mode-map (kbd "M-r") 'helm-gtags-find-rtag)
      (define-key helm-gtags-mode-map (kbd "M-t") 'helm-gtags-find-tag)
      (define-key helm-gtags-mode-map (kbd "M-s") 'helm-gtags-find-symbol)
      ;; (define-key helm-gtags-mode-map (kbd "M-,") 'helm-gtags-pop-stack)
      (define-key helm-gtags-mode-map (kbd "M-*") 'helm-gtags-resume)
      ;;
      (define-key helm-gtags-mode-map (kbd "C-c <left>") 'helm-gtags-previous-history)
      (define-key helm-gtags-mode-map (kbd "C-c <right>") 'helm-gtags-next-history)
      ;;
      ;; (define-key helm-gtags-mode-map (kbd "C-c g") 'vc-git-grep)
      
      
  • locate and search
    • global search by M-x rgrep
    • or M-x locate
    • or M-x helm-locate

Index special file format (for example, .hidl files)

To use a specific parser to index a specific file types, you can change the field of ':langmap' in your local .globalrc file. For example, if you want to index .hidl as c++, you can map it to c++ type.

:langmap=c\:.c.h,yacc\:.y,asm\:.s.S,java\:.java,cpp\:.c++.cc.cpp.cxx.hxx.hpp.hh.hidl.C.H,php\:.php.php3.phtml:

Git integration

matgit

elpy usage

  • elpy-goto-definition M-.
  • xref-pop-marker-stack M-,

Other convenient things

  • minimap mode
  • narrowing for programming
    C-x n n
    Narrow down to between point and mark (narrow-to-region).
    C-x n d
    Narrow down to the current defun (narrow-to-defun).
    C-x n w
    cancel narrow down (widen)

Useful commands

Multiple line edit (rectangle edit)

C+x r t (string-rectangle) For more, please see M-x xxx-rectange

How to decide whether to save (diff buffer with original file)

  • M-x diff-buffer-with-file

or

  • C-x s then d

How to split into 3 even windows

  • C-x 3 to add one more frame
  • C-x + to equally size all windows

change font size

  • C-x C-+ increase font size
  • C-x C– decrease fint size

run shell command in Emacs

M-! run shell command in Emacs

Show all key bindings

C+h b (describe-bindings) C+h k (describe-key)

  • C-c ; Toggle the COMMENT keyword at the beginning of an entry, which won't be exported.
  • C-c / search in current buffer shown as a sparse tree.

rtags c++ indexer

General

I have tried rtags with Emacs. It works well after index. It takes a pretty long time to index a large project. Since gtags is much faster and works for me, I don't use it.

install

sudo apt-get install libclang-dev llvm
cd ~/local/
git clone --recursive http://github.com/Andersbakken/rtags
cd rtags
mkdir build
cd build && cmake -DCMAKE_BUILD_TYPE=Release ..
make
sudo make install

usage

  • start $nice ionice -c idle rdm -j6
  • Pointing rtags to the av tree

$ rc -J ./source/bazel-out/local_linux-fastbuild/genfiles/compile_commands.json

$ rc -J ./av/build/compile_commands.json

help

rc –help

Emacs setting for rtags

Terminal in Emacs

  • ansi-term
  • To modify the color scheme: M-x customize-group RET term RET
  • emacs settings

    (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
    (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
    (add-to-list 'comint-output-filter-functions 'ansi-color-process-output)
    
    (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
    (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
    (add-to-list 'comint-output-filter-functions 'ansi-color-process-output)
    

terminal color theme settings

  1. put the following into .bashrc or .profile export TERM=xterm-256color
  2. check the effect in Emacs by: M-x list-colors-display
  3. select color theme you like by: M-x color-theme-xx

set fill-paragraph width

C-x f set-fill-column default is 70

MobileOrg setting

Common key bindings

alt-x
same as M-x
C-h a
search for command c-h i :: information page c-h f :: help on function c-h k :: help on key bindings c-h m :: help on current mode
C-/
undo
C-x (
start recording macro C-x ) :: stop recording macro C-x e :: execute

color theme

Cool website that can generate color theme online:

Using Emacs native color theme (maybe only for V24 or later)

  • try ESC-x color-theme TAB to show all available themes.
  • after you have find a favorite theme, you can add the following to .emacs (theme manoj-dark for example)
(load-theme  'manoj-dark)

Column View in org-mode

  • setting what to show
#+COLUMNS: %25ITEM %TAGS %Effort(EV) %CLOCKSUM(RC) %TODO
C-c C-x C-c
column view
C-c C-x i
generate gdynamic table
q
quit column view

Search in Emacs

  • find a file with its name
M-x find-name-dired

or

*M-x find-dired*  (accept any command that find do)

e.g. case-insensitive search -iname \*xxx\* Note: '\' is necessary

  • find a file with its contain
M-x rgrep
  • locate or locate-with-filter

How to get rid of ^M   EMACS REMOVE

M-x replace-string C-q C-m RET

or run dos2unix

org-mode   EMACS KEYS

C-c C-<
outline-promote
C-c C->
outline-demote
C-j
org-return-indent
C-c C-x v
Copy the visible text in the region into the kill ring.
C-c C-u
Backward to higher level heading.
C-c C-j
Jump to a different place without changing the current outline visibility. Shows the document structure in a temporary buffer, where you can use the following keys to find your destination
(no term)
C-c C-x C-s subtree archive
C-c l
store a internal link
C-c C-l
insert a link to current location
C-c &
return back

demote and promote

  • M-x outline-demote
  • M-x demote selected region

Emacs org clocking   EMACS CLOCK_KEYS

C-c C-x C-i
clock in
C-c C-x C-o
clock out
C-c C-x C-i
clock jump
C-c C-c
recalculate the time interval
C-c C-x C-d
Display time summaries for each subtree in the current buffer (C-c C-c cancel)
C-c C-x C-r
Insert a dynamic block (see Dynamic blocks) containing a clock report
M-x org-resolve-clocks
You can also check all the files visited by your Org agenda for dangling clocks at any time using M-x org-resolve-clocks.

Spreadsheet

  • C-c } you can always turn on the reference visualization grid with
  • first formula: :=vmean($2..$3) hit C-c C-c you should observe two things
  • C-c ' interactive formula editor / C-c = simple interactive formula editor
  • C-c | convertactive region into a table. items can be separated by s whitespace, comma, or tab.
  • C-c ^ sort according to current column.
  • <N> in the column to set the field width. as follows:
| <4>  | <5>   | <7>     |

UI settings

  • How to show line numbers?
    (global-linum-mode 1) ; display line numbers in margin. Emacs 23 only.
  • How to show the cursor's column position?
    (column-number-mode 1)
  • How to have lines soft wrapped at word boundary?
    Pull the menu ?Options ? Line Wrapping in this Buffer?, or call visual-line-mode.
  • How to have lines soft wrapped at word boundary?
    (global-visual-line-mode 1) ; 1 for on, 0 for off.

Switch between input method

  • C-\

Make presentation / slides

for 8.2.3b

  • add the following at the beginning of an org file
#+startup: beamer
#+LaTeX_CLASS: beamer
  • add the following to .emacs
(require 'ox-latex)
(add-to-list 'org-latex-classes
          '("beamer"
            "\\documentclass\[presentation\]\{beamer\}"
            ("\\section\{%s\}" . "\\section*\{%s\}")
            ("\\subsection\{%s\}" . "\\subsection*\{%s\}")
            ("\\subsubsection\{%s\}" . "\\subsubsection*\{%s\}")))

S5 solution (A Simple Standards-Based Slide Show System)

  • add the following to .emacs
;; export slides in org mode
;; remember to copy folder "ui" to your pub folder
(add-to-list 'load-path "~/site-lisp/org-s5-master")
(require 'org-export-as-s5)
;;(setq org-s5-theme "railscast")   ; based off `color-theme-railscasts'
(setq org-s5-theme "default")     ; the default S5 theme
;;(setq org-s5-theme "i18n")        ; the i18n theme by the author of S5
;;(setq org-s5-theme "advanced_gfx")
  • to export an org buffer to S5 slides, run the following command: org-export-as-s5
  • get more themes from: S5_1.3beta7

reveal

  1. copy ~/site-lisp/org-reveal-master to DIR1
  2. copy ~/site-lisp/reveal-master to DIR2
  3. setting emacs
;; org reveal export
(add-to-list 'load-path "~/site-lisp/org-reveal-master/")
(require 'ox-reveal)
(setq org-reveal-root "file:///D:/cgliu/emacs-24.2/site-lisp/reveal-master/")