Emacs notes
Table of Contents
1 Emacs
1.1 General info
Emacs is an extensible text editor. Most of emacs is written in elisp which also happens to be its scripting language, which means that it's far more extensible than most other editors, which allow for customization only using a scripting language.
1.2 Random tidbits of info
Key chords or key maps work differently in emacs. They're all bound to an elisp function. So you would bind a new key map to a function mostly, rather than key strokes (like is sometimes done in vim) Macros have the facility to stop during execution for user input.
There's something called Outline mode, seems to be mostly useful for specifying headings in long documents and folding content. Org mode seems more powerful.
Text, especially tabular text can be edited using "rectangular regions" which work by placing marks. Seems more powerful than just vim's visual block selection.
There's picture-mode and artist-mode which can be used to make simple ascii drawings.
1.3 Regex
Regex in emacs is different from that in Vim, which itself is slightly different from something like in Javascript or Perl, because why not? All parentheses require escaping, so does the "|" (or) character.
From the cursory search I did on the web, there seem to be ways to convert Emacs-regex into PCRE (Perl Compatible Regular Expression), but I'm not sure if that'll work for all the places where regex can be used. There's also a regexp-builder
and a re-builder
; functions that help building a elisp compatible regular expression, but I haven't really tried it yet. For more complicated regular expressions, it's probably best to just do that in vim. This page contains more info on emacs regex: http://ergoemacs.org/emacs/emacs_regex.html
Function | PCRE | elisp | Notes |
---|---|---|---|
Capture | (…) | \(…\) |
|
digit | \d | [[:digit:]] |
consider using [0-9] instead |
word | \w | [[:word:]] |
consider using [A-Za-z] instead |
whitespace | \s | [[:space:]] |
consider using " " instead |
1.4 Dired
Dired (Directory Editor) is the equivalent of vim's netrw. Regular keys like hjkl
and /
work for navigation and searching, but other keys might be different from vim
Function | Key |
---|---|
Create dir | + |
Parent dir | ^ |
Mark for delete | d |
Commit operation on marked files | x |
Rename/Move | R |
Reduce/increase details | ( and ) |
Mark file/dir | m |
Unmark file/dir | u |
Unmark all | U |
Invert selection | t |
Copy | C |
Rename/move | e |
Tip: Open dired in 2 split windows and a lot of the file operations become more intuitive. See: dwim
Update: I'm now using treemacs
which comes with Spacemacs v300, and installed vinegar
layer, which simplifies file navigation quite a bit, and provide netrw type key bindings.
1.5 Exec path
The value of the environment variable $PATH is used by emacs when running programs in a shell (not eshell), but exec-path
is used by emacs itself to find programs it needs for features to work. To add paths: (setq exec-path (append exec-path '("/path/to/program")))
.
If adding to exec-path
doesn't work, you can also try modifying the eshell-path-env
variable, or move a symlink to one of the folders currently in eshell-path-env
. You can find its current value by running C-h v eshell-path-env[RET]
. C-h v
runs the describe-variable
function.
2 Spacemacs:
2.1 Some interesting toggles and settings:
SPC T F
-> Toggle fullscreen
SPC t g
-> Toggle 'golden ratio' for easy resizing of windows
SPC f t
-> Neo Tree (or treemacs depending on spacemacs version)
SPC n r
-> Only display selected region in the buffer (useful when refactoring variables)
SPC n w
-> Display all contents in the buffer
SPC t l
-> Toggle truncate lines (line wrapping)
SPC t L
-> Visual line navigation. Esp useful when line wrap is enabled
SPC t w
-> Toggle whitespace
SPC t I
-> Toggle aggressive indentation (indents while typing)
z a
-> Toggle folds
The toggles above are usually bound to elisp functions. If you need some of them to be on/off by default, check the function those toggles call using SPC ?
, then add to dotspacemacs/user-config
2.2 Help:
SPC ?
-> Search for specific key bindings. Ex: SPC\ b
lists all buffer related bindings
SPC <f1>
-> Apropos help (fuzzy search help section topics)
SPC h l
-> Search for help about a particular layer. Very useful to understand platform-specific considerations, key bindings and installed packages.
SPC h p
-> Search for help about a particular package.
SPC h d v
-> Describe variable
SPC h d k
-> Describe key binding
SPC h d f
-> >Describe function
Emacs help section can be different from spacemacs help section. For ex, there is no concept of layers in emacs, and SPC h p
takes you to the code where a package is initialized. To access emacs' help, type C-h
. There are many describe
functions that can be very useful in understanding your emacs setup. For ex, describe-package
is useful to understand dependencies of various packages. Try it out by typing C-h P
and entering a package name. Another useful functions is describe-mode
which gives a bunch of information about the current buffer's major mode and available minor modes.
2.3 File operations:
SPC f b
-> File bookmarks
SPC f o
-> Open file in an external app. Useful for opening an html file in the browser.
SPC D b b
-> ediff-buffers
Start diff between open buffers
2.4 Selection:
SPC v
-> start region selection. Then use v and V to expand or contract by one semantic unit
2.5 Indentation:
Indentation levels seem to be controlled by different variables depending on the major mode one is in.
For ex: In js mode, js-indent-level variable controls the indentation
(defun my-setup-indent (n) ;; java/c/c++ (setq c-basic-offset n) ;; web development (setq coffee-tab-width n) ; coffeescript (setq javascript-indent-level n) ; javascript-mode (setq js-indent-level n) ; js-mode (setq js2-basic-offset n) ; js2-mode, in latest js2-mode, it's alias of js-indent-level (setq web-mode-markup-indent-offset n) ; web-mode, html tag in html file (setq web-mode-css-indent-offset n) ; web-mode, css in html file (setq web-mode-code-indent-offset n) ; web-mode, js code in html file (setq css-indent-offset n)) ; css-mode (my-setup-indent 2) ;
2.6 Layouts and workspaces
Workspaces are similar to vim's tabs, and they can even be navigated using gt and gT. Layouts are more like a collection of windows, and can be used to provide buffer isolation, meaning buffers created under one Layout aren't available in other Layouts. Workspaces created under a Layout all share the same buffer list. A good way to use Layouts is to have one Layout per project.
To open a project in a new Layout, use SPC p l
, then use SPC l 0..9
to switch to a particular Layout.
2.7 Projects
Spacemacs uses projectile package to manage projects. Projectile's functions are accessed through SPC p
. projectile-discover-projects-in-directory
is a useful command to discover all projects within a directory.
2.8 Changing escape key
(setq-default evil-escape-key-sequence "jk")
Use (kbd "")
function if you want to use Control/Meta/Shift keys.
2.9 Some interesting packages found pre-installed in Spacemacs
- Dumb jump: jump to definition capability provided only using search (no tags/external parser)
- devdocs: Open quick documentation for the thing under point in http://devdocs.io
- helm-swoop: Open matches in a new buffer, have to try it out to understand
- auto-yasnippet: Easily create disposable snippet for the buffer
- aggressive-indent: Indent while typing
2.10 Folding
SPC z .
Opens a transient state that can be useful for easily closing/opening folds. Vim's basic fold toggle command za
works as well. In the transient state you may notice that there's no command to recursively close all folds, which is unfortunate, but hs-hide-level
function compensates for it quite well.
One very useful command is hs-hide-level
which when called when the cursor is within a potential fold, closes all potential sub folds. This is especially useful when you want to get an overview of a class' methods. Just go to the first line after the class definition and call the above mentioned function.
2.11 Searching
SPC p f
-> search for a file in the current project.
SPC /
-> search for a pattern in the current project.
SPC *
-> search for the pattern under point in the current project.
If you have too many search results in helm, you can save them to a buffer for easier navigation using C-x C-s
. You can still press RET
in the buffer to open the corresponding file.
2.12 Helm
SPC r y
calls lazy-helm/helm-show-kill-ring
which is like a souped up version of :reg
command in vim. It shows all the recent copied text in a helm buffer which can be navigated using C-j/k
and RET
will yank (paste) in into the current buffer.