Notes on Emacs
Table of Contents
- 1. Installation Emacs from source
- 2. Automatically install missing package
- 3. helm mode settings
- 4. Programming in Emacs
- 5. Useful commands
- 6. rtags c++ indexer
- 7. Terminal in Emacs
- 8. terminal color theme settings
- 9. set fill-paragraph width
- 10. MobileOrg setting
- 11. Common key bindings
- 12. color theme
- 13. Column View in org-mode
- 14. Search in Emacs
- 15. How to get rid of ^M
- 16. org-mode
- 17. Emacs org clocking
- 18. Spreadsheet
- 19. UI settings
- 20. Switch between input method
- 21. Make presentation / slides
- 22. How to create a beautiful homepage with Emacs org-mode and Bootstrap
1 Installation Emacs from source
- install all dependencies
sudo apt-get build-dep emacs24
- download source code from http://www.gnu.org/software/emacs/
- extract emacs-xx.x.tar.gz and install
tar -zxvf emacs-24.3.tar.gz cd emacs-24.3 ./configure make sudo make install
2 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)
3 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)
4 Programming in Emacs
4.1 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
- install Exuberant Ctags http://ctags.sourceforge.net/
- install Universal Ctags https://github.com/universal-ctags/ctags
install gtags as:
./configure --with-exuberant-ctags=PATH_TO_CTAGS --with-universal-ctags=PATH_TO_UNIVERSAL_CTAGS ./make sudo make install
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)
- Installation
- locate and search
- global search by M-x rgrep
- or M-x locate
- or M-x helm-locate
4.2 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:
4.3 Git integration
matgit
4.4 elpy usage
- elpy-goto-definition M-.
- xref-pop-marker-stack M-,
4.5 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)
5 Useful commands
5.1 Multiple line edit (rectangle edit)
C+x r t (string-rectangle) For more, please see M-x xxx-rectange
5.2 How to decide whether to save (diff buffer with original file)
- M-x diff-buffer-with-file
or
- C-x s then d
5.3 How to split into 3 even windows
- C-x 3 to add one more frame
- C-x + to equally size all windows
5.4 change font size
- C-x C-+ increase font size
- C-x C– decrease fint size
5.5 run shell command in Emacs
M-! run shell command in Emacs
5.6 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.
6 rtags c++ indexer
6.1 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.
6.2 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
6.3 usage
- start $nice ionice -c idle rdm -j6
- Pointing rtags to the av tree
$ rc -J ./source/bazel-out/locallinux-fastbuild/genfiles/compilecommands.json
$ rc -J ./av/build/compilecommands.json
6.4 help
rc –help
6.5 Emacs setting for rtags
7 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)
8 terminal color theme settings
- put the following into .bashrc or .profile export TERM=xterm-256color
- check the effect in Emacs by: M-x list-colors-display
- select color theme you like by: M-x color-theme-xx
9 set fill-paragraph width
C-x f set-fill-column default is 70
10 MobileOrg setting
11 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
12 color theme
12.1 Cool website that can generate color theme online:
12.2 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)
13 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
14 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
15 How to get rid of ^M
M-x replace-string C-q C-m RET
or run dos2unix
16 org-mode
- 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
16.1 demote and promote
- M-x outline-demote
- M-x demote selected region
17 Emacs org clocking
- 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.
18 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> |
19 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.
20 Switch between input method
- C-\
21 Make presentation / slides
21.1 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\}")))
21.2 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: S51.3beta7
21.3 reveal
- copy ~/site-lisp/org-reveal-master to DIR1
- copy ~/site-lisp/reveal-master to DIR2
- 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/")
22 How to create a beautiful homepage with Emacs org-mode and Bootstrap
Configure org-mode html export project
(setq org-publish-project-alist '( ("org-notes" ;Used to export .org file :base-directory "~/www/" ;directory holds .org files :base-extension "org" ;process .org file only :publishing-directory "~/public_html/" ;export destination :recursive t :publishing-function org-html-publish-to-html :headline-levels 4 ; Just the default for this project. ;; :auto-preamble t :auto-sitemap t ; Generate sitemap.org automagically... :sitemap-filename "sitemap.org" ; ... call it sitemap.org (it's the default)... :sitemap-title "Sitemap" ; ... with title 'Sitemap'. :export-creator-info nil ; Disable the inclusion of "Created by Org" in the postamble. :export-author-info nil ; Disable the inclusion of "Author: Your Name" in the postamble. :auto-postamble nil ; Disable auto postamble :table-of-contents t ; Set this to "t" if you want a table of contents, set to "nil" disables TOC. :section-numbers nil ; Set this to "t" if you want headings to have numbers. :style-include-default nil ;Disable the default css style :html-preamble nil :html-postamble nil ) ("org-static" ;Used to publish static files :base-directory "~/www/" :base-extension "css\\|js\\|png\\|JPG\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf" :publishing-directory "~/public_html/" ;export destination :recursive t :publishing-function org-publish-attachment ) ("org" :components ("org-notes" "org-static")) ;combine "org-notes" and "org-static" into one function call ))
- Download Bootstrap and extract .zip. Make sure these folders (css, img and js) are in the same folder as your org files.
Create level-0.include as following and save it to YOURINCPATH
#+options: author:nil creator:nil timestamp:nil email:nil #+options: h:0 toc:nil title:nil #+options: html-style:t #+HTML_HEAD: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> #+HTML_HEAD: <title>Chenggang Liu's Homepage</title> #+HTML_HEAD: <link rel="stylesheet" href="./fonts/Serif/cmun-serif.css" /> #+HTML_HEAD: <link rel="stylesheet" href="./fonts/Serif-Slanted/cmun-serif-slanted.css" /> #+HTML_HEAD: <!--BOOTSTRAP--> #+HTML_HEAD: <link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet"> #+HTML_HEAD: <!--mobile first--> #+HTML_HEAD: <meta name="viewport" content="width=device-width, initial-scale=1.0"> #+HTML_HEAD: #+HTML_HEAD: <!--removed html from url but still is html--> #+HTML_HEAD: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> #+HTML_HEAD: #+HTML_HEAD: <!--font awesome--> #+HTML_HEAD: <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> #+HTML_HEAD: #+HTML_HEAD: <!--fonts: allan & cardo--> #+HTML_HEAD: <link href="http://fonts.googleapis.com/css?family=Droid+Serif" rel="stylesheet" type="text/css"> #+HTML_HEAD: <link href="http://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet" type="text/css"> #+HTML_HEAD: <link href="./css/sticky-footer-navbar.css" rel="stylesheet"> #+HTML_HEAD: #+HTML_HEAD: <!--Highlight--> #+HTML_HEAD: <link href="./highlight/styles/github.css" rel="stylesheet"> #+HTML_HEAD: <link href="./favicon.ico" rel="shortcut icon" /> #+HTML_HEAD: <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> #+BEGIN_EXPORT html <nav class="navbar navbar-inverse navbar-static-top" role="navigation"> <div class="container"> <!--Toggle header for mobile--> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand active" href="./index.html" id="home">Chenggang Liu's Homepage</a> </div> <!--normal header--> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> <li><a href="./index.html"><span class="glyphicon glyphicon-home"></span> Home</a></a></li> <li><a href="./blog.html"><span class="glyphicon glyphicon-pencil"></span> Blog</a></li> <li><a href="./projects.html"><span class="glyphicon glyphicon-tags"></span> Projects</a></li> <li><a href="./about.html"><span class="glyphicon glyphicon-user"></span> About</a></li> <li><a href="./contact.html"><span class="glyphicon glyphicon-envelope"></span> Contact</a></li> </ul> </div><!--/.nav-collapse --> </div> </nav> #+END_EXPORT
On top of your org files, add:
#+INCLUDE: "YOUR_INC_PATH/level-0.include"
- You are done and enjoy!