evil-mode, flx-ido, helm, smex, key-chord, hydra, company-mode, ggtags
This is an emacs configuration that I use for my day-to-day work, which may be best described as glorified robotic code monkey - kind of like the Terminator, only with urxvt and much more head-scratching. We aim to make the customer go bananas over our product.
My repo is a fork off of Bin Chen's vim-oriented emacs.d, which is in turn inspired by Steve Purcell's classic setup. Refer to Bin Chen's repo for more info on how this setup is structured. As time goes by, every emacs setup tends to accumulate tons of unused keymappings. Therefore, the following commands/keybindings documented here are the ones that are my all-time favorites, most of which I use every single day.
NOTE: I've transitioned to spacemacs! So this repository is now deprecated. For my spacemacs configuration, which carries over some of the same conveniences, check out the repo here.
Keybinding | Description |
---|---|
C-c f | helm-for-files. Allows you to find/switch-to/open a file from anywhere in your filesystem. Searches existing buffers, recentf list, bookmarks, files in cwd, and finally all files on your computer by tapping into Unix locate. |
C-c i | helm-semantic-or-imenu. Displays a list of all functions and variables in the current buffer. Try typing "fun" followed by SPC to filter and view only functions, then enter the function name and press RET . |
, d j | dired-jump. Instantly opens up dired buffer for the folder of the current file. Press ^ to go up a directory, > /< to cycle through folders, a to launch an ag (grep) search, d x to mark and delete a file, C to copy, R to rename, and RET to visit a file. More dired info here. |
HG | helm-swoop on word under cursor. Shows all lines containing the word in current buffer, allowing you to cycle through them with C-n /C-p . Hit M-i to switch over to helm-multi-swoop, which does the same search in all open buffers. C-c C-e lets you edit the instances right there in the buffer. |
C-c p s s | projectile-ag. Grep for a specified string in all files from the current buffer's git repository. Ag, the silver searcher, is a more efficent grep program that must be installed as a prerequisite. |
vv | other-window. Key-chord for switching to the other window; a much faster and ergonomic way of doing C-x o . |
M-. | ggtags-find-tag-dwim. If the cursor is over a function call, jump to its definition. Pop back to your original cursor location with M-* (it's a stack, so you can pop out repeatedly). If the cursor is over a function definition, it finds the first reference (place where function is used). Use M-n and M-p to navigate between multiple references. Requires GNU Global to be installed. |
SPC | ace-jump-mode. In ace-jump-mode, press any character that you want your cursor jump to on the screen and then press the corresponding character that shows up. Once activated, lets you jump anywhere on your screen in usually two keystrokes. |
Keybinding | Description |
---|---|
M-; | comment-or-uncomment-region-or-line. A saner way of commenting code in emacs. |
u | undo-tree-undo. Lets you undo the latest change. |
M-S-- | undo-tree-redo. Lets you redo the latest change. |
C-s | isearch-forward. Search file for word. Currently uses ace-isearch, which changes over to ace-jump for single character searches or helm-swoop for longer searches. Press M-i while in isearch to switch to helm-swoop and press M-r to switch to isearch-forward-regexp. |
qr | query-replace. Lets you enter a string and the string to replace it with. Press y to do replace on the current selection and go to next instance, n to skip replacing and go to next instance, and , to replace, but stay at the current location. |
, e d | ediff-buffers. If you have two buffers open, you can run this command to select and diff them. Use n and p to visit next and previous diffs, and hit a to copy Buffer A's hunk or b to copy Buffer B's hunk. It can be used in conjunction with Magit to resolve git merge conflicts. |
M-c | endless/capitalize. Capitalize the word after the cursor. |
M-l | endless/downcase. Change the word after the cursor to lowercase. |
M-u | endless/upcase. Change the word after the cursor to uppercase. |
M-m | back-to-indentation. Moves the cursor back to the first non-whitespace character on a line. Useful when you want to insert text at the start of an indented line. |
C-x f | recentf-open-files. Lists your most recent files opened. Press a number from 0-9 to open the file you want. |
bj | bookmark-jump. Extremely useful for switching to files and locations you visit frequently. |
C-x r m | bookmark-set. Lets you name and set a bookmark at the current position in the buffer. You can even bookmark a dired-buffer. Once you exit emacs normally or run bookmark-save, the bookmark is automatically stored in a file. |
C-x * * | calc. Launches emacs' special reverse-polish notation (RPN) calculator. Use RET to dup the stack and TAB to swap, kind of like Forth. Enter "16#" followed by a number to input hex and display hex with d 6 . Change back to base 10 with d 0 . Logs, square roots, trig, matrix, and bitwise operations are all supported! |
C-l | recenter-top-bottom. Centers the screen around where your cursor is. Handy for when you can't see the rest of your code. |
C-x 1 | Make the current buffer the only window. |
C-x 2 | Split the window horizontically. |
C-x 3 | Split the window vertically. |
C-g | keyboard-quit. Cancel whatever command you are currently in. Used almost all the time when you do something accidentally or want to exit some mode. |
M-x package-install | Install a package from the MELPA repository. Alternatively, use M-x package-list-packages to browse the full list. |
M-x load-theme | Lets you select a color theme. Some pretty ones are tomorrow-night-paradise, solarized, monokai (a la Sublime Text), wombat, and tango-dark. Find the motherlode at emacsthemes.caisah.info. |
See this tutorial for some nice info about text objects. If you're migrating from vim, you might find this helpful. For something comprehensive, this reference has just about all the essentials. The amazing thing is that almost all of them are supported by evil-mode.
Because of space, I can't cover all of the basic commands in vim, but some of the ones I use a lot are ddp
and xp
to transpose words and characters, f
, t
, F
, T
followed by a character to jump to it, C-v
followed by A
or I
for column editing, :
followed by a line number to goto line, A
, o
, V
, ciw
, vi)
, da)
, =i}
, =4j
, etc...
evil-surround is also installed, allowing you to wrap things in parentheses by first selecting the word/region and then typing sb
or s)
. s(
will wrap things with spaces inserted, ( for example ). Also, any bracket or even letter can be used as the wrapping, e.g. s*
, s}
, or sx
.
Vim's buffer-local marks also work and come in handy. Press m
followed by a single character to store a location in the buffer (I usually just hit m m
), and jump to it with the backtick character (`m
in this case). C-o
, C-i
, and ``
take you back through cursor history, forward through cursor history, and to your last position, respectively.
Evil-mode also supports vim syntax for registers. Use "ay
to copy a selection into register a
instead of the traditional C-x r s a
keybinding. You can paste the selection with "ap
, instead of C-x r i a
. However, the highly useful emacs point registers cannot be accessed by vim syntax, as far as I know (C-x r SPC a
and C-x r j a
for register a). Though similar to vim marks, they are not buffer-local and allow you to jump to a position in a file from any other file.
It's also worth noting that C-z
is used to switch between evil-mode and normal emacs mode, should you ever need the latter.
Most of these commands can be issued repeatedly, if the first match isn't what you wanted. Additionally, company-mode will often detect something it can expand and either provide a candidate that can be accepted with RET
or a popup list that you can select things from using Alt+some number.
Keybinding | Description |
---|---|
C-n | evil-paste-pop-next. Completes the current word with the closest match after your cursor. |
C-p | evil-paste-pop-prev. Completes the current word with the closest match before your cursor. |
M-/ | hippie-expand. Completes the current word with the closest match, as determined by hippie. Hippie-expand uses semantic and a list of sources to perform the complete. It even works inside query-replace, so you don't have to type long strings. |
C-x l | hydra-C-x-extended/my-expand-lines. Do full line completion, like C-x C-l in vim. |
C-y | evil-copy-from-above. Copy characters from the line directly above. |
C-e | evil-copy-from-below. Copy characters from the line directly below. |
Keybinding | Description |
---|---|
M-S-right | sp-slurp-hybrid-sexp. Press Alt+Shift+right arrow key to pull the next s-expression into your braces/brackets/parentheses. For example, if you have an if {...} statement followed by braces and you want to pull the next line into your if statement, issue this slurp command. This command is only the tip of the iceberg of smartparens, a package based on paredit, which is a mind-boggling way of manipulating lisp and parentheses. |
C-c w s | hydra-window/body. Swap windows (for example, if you split windows with C-x 3 ). |
C-j | helm-gtags-select. When in a gtags-supported project, opens up a list of all tags (variables, function, etc.) to interactively select with helm. |
g x | evil-exchange. If you have two words or regions you want to swap, select the first (using v ) and press g x . Then, select the second and press g x to swap the two. |
q | evil-record-macro. Press q followed by a letter to store the macro in that register and start recording a macro. You can perform a complex series of actions and then press q again to stop recording. Then, press @a to execute the macro. Use @@ to repeat the last macro. It's useful for "automating" repetitive text editing tasks. |
, x x | er/expand-region. Select the current region and expand by semantic units with x . Contract with z . |
, r b f | revert-buffer. Reload the current buffer. If another program has modified the current file, this will reload the file. |
C-x n | git-gutter:next-hunk. Move to the next git-gutter hunk. For a file under git revision control, git-gutter will show you all lines that have been modified from the current HEAD. A hunk can be a line or collection of consecutive lines. Run git-gutter:previous-hunk with C-x p |
C-c right | windmove-right. Move cursor to the window on the right. An easy way to switch between lots of windows. Works for up, down, and left as well. |
C-x up | winner-undo. Restores the previous window configuration. For example, if you have two windows open and you change one of them to a different file, you can restore the old view with this command. To undo the effect of your undo, do C-x down to run winner-redo. |
m Q | In a dired buffer, mark all the files you want to replace text in with m . Then, press Q to do a dired-do-query-replace-regexp over all the files at once! You can use C-x s ! to save all files afterwards. |
M-x find-name-dired | Allows you to specify a command and then a wildcard matching pattern (e.g. *.cpp) to grab a dired buffer with only those matching files. You can mark the files you want in dired and then run the query-replace method above. It's recursive and can be a huge timesaver. Ever had to replace hundreds of words in lots of different files from different directories? This dired command makes it a breeze. |
W | wdired-change-to-wdired-mode. In a dired buffer, issue this command to make your dired buffer writable. You can then use all of emacs' text editing abilities to rename files. One of my favorite things to do is to change to wdired and then run a query-replace to rename a bunch of files. |
C-x g | magit-status. Opens magit, an powerful interface for running git commands. A good tutorial is located here and a complete cheatsheet can be found here. It's a lot easier to use magit to delete/ignore/stage files rather than having to type out the entire filename and path manually. You can even do interactive rebase with E . |
In progress...
In progress...
C-x
means Ctrl-x
M-x
means Alt-x
M-S--
means Alt-Shift-minus key
, d j
means press the leader key (currently ","), followed by d and j.
- this is a feature of the evil-leader package, part of evil-mode.
qr
means press q, quickly followed by r.
- this is a key-chord; the order of the keys actually doesn't matter, so rq would do the same thing.
C-x c f
means press Ctrl-x, let go, press c, let go, and press f. Of course, it happens much more quickly than it sounds!
What are some good resources for emacs?
- emacs-fu -- djcb - Useful tips and tricks for extending emacs; by the author of the mu4e emacs email client.
- Tuhdo's emacs guides - Animated demos of advanced emacs features for C/C++ dev work.
- Sacha Chua - Famous Canadian emacs user; great tips on new packages and org-mode. She also does webcast interviews with notable emacs users.
- Xah Lee - Opinionated emacs poweruser with great tutorials on basic operations and ergonomics.
- emacsrocks Screencasts -- Magnar Sveen - Mind-blowing demos of emacs features like paredit and multiple-cursors.
- Github Awesome Emacs list - Community-curated list of resources for emacs. Not comprehensive by any means, but a good starting place.
Rebind your Right Alt to Ctrl to save your pinky! Your tucked thumb can push it with no complaints.
You can use RAlt::Ctrl
in AutoHotKey on Windows, or setxkbmap -option ctrl:ralt_rctrl
under Linux.
- The commands documented above are only a tiny fraction of what's available in emacs. Some things I didn't cover, but still exist in this repo, include AucTeX (LaTeX editor), eshell (emacs' built-in shell), w3m (web browser), erc (IRC client), rectangles, lisp interpreters, multiple cursors, yasnippet, and the immensely useful org-mode.
- I learned about emacs and org-mode when I was a lowly sophomore, as yet unable to distinguish the Linux kernel from corn. Default emacs keybindings made me wonder what awful editor I had stumbled upon. They make the learning curve a lot steeper than it needs to be. As the saying goes, "emacs is an operating system that lacks a decent text editor." Vim is that decent text editor, so using evil-mode from the get-go will make emacs much more enjoyable to use.
- Using emacs feels like using a tool that is always under development. Very often, something new you've added will break or not work quite right, and emacs puts the burden on you to fix it. If you don't have a lot of setup time and want something rock-solid and fast, switch to vim instead. You can get ~90% of the features emacs has. However, it's less extensible (vimscript==blarg) and lacks a couple of advanced features, like wdired. Things like org-mode and eshell that have been "cloned" in vim often feel like cheaper, hacked-up versions of the real thing. In the end, the choice between vim and emacs is largely a matter of personality - a solid, no-frills surgical scalpel vs. an endlessly customizable Swiss army knife (to which you can attach a scalpel!). Your mileage may vary.