text editing: mode | movement | selecting | deleting | files | buffers | minibuffer | ido | history | searching | replacing | sorting | counting | case | indentation | rectangles | spelling | panes | macros | ascii art | org | input methods | encodings | help
dev tools: browse files | shell | grep | tags | diff | git | compile | debug | lisp interaction | syntax highlighting
programming: variables | arithmetic and logic | strings | lists | dictionaries | functions | execution control | libraries and namespaces
other topics: documentation | terminology | line editors | bindings in other apps
startup | ||||
---|---|---|---|---|
vim | emacs | nano | ||
launch |
$ vim | $ emacs | $ nano | |
open file |
$ vim file | $ emacs file | $ nano file | |
open file on line 100 |
$ vim +100 file | $ emacs +100 file | $ nano +100 file | |
open file on column 20 of line 100 |
$ vim +100 -c 'normal 20|' file | $ emacs +100:20 file | $ nano +100,20 file | |
open file in read-only mode |
$ vim -R file | $ nano -v file | ||
open multiple files |
$ vim file1 file2 … only first file is displayed; use :bn and :bp to change buffers; :ls to list buffers |
$ emacs file1 file2 … window is split into two panes; if more than two files, one of the panes is a buffer list; use C-x o to switch panes; use C-x b to switch buffers |
$ nano file1 file2 … only first file is displayed; use M-> and M-< to change buffers |
|
open directory |
$ vim dir | $ emacs dir | ||
startup file | ~/.vimrc | ~/.emacs ~/.emacs.el ~/.emacs.d/init.el |
~/.nanorc | |
skip startup file |
$ vim -u NONE | $ emacs -q | $ nano -I | |
text editing | ||||
vim | emacs | emacs | nano | |
show version |
:version | M-x emacs-version | ||
redraw display |
C-l | M-x redraw-display | ||
insert 'x' | insert mode: x |
x | x | |
insert by unicode point | insert mode: C-v u 03c0 |
C-x 8 RET 03c0 RET | M-x ucs-insert 03c0 RET | |
insert by unicode name |
C-x 8 RET greek small letter pi RET | M-x ucs-insert greek small letter pi RET | ||
get unicode point |
M-x describe-char | |||
cancel |
ESC | C-g | M-x keyboard-quit | |
insert control character |
C-v char | C-q char | M-x quoted-insert char | M-v |
insert newline |
C-v RET | C-q C-j | ||
auto complete | C-n C-p |
M-/ | M-x hippie-expand | |
repeat next command n times |
n | C-u n | M-x universal-argument n | |
repeat 4 times |
4 | C-u | M-x universal-argument | |
repeat 16 times |
16 | C-u C-u | ||
font size |
use menu or OS | C-x C-=, C-x C-- | M-x text-scale-adjust | |
exit |
:q | C-x C-c | M-x save-buffers-kill-terminal | C-x |
save changes and exit | ZZ :wq |
C-x C-s C-x C-c | M-x save-buffer M-x save-buffers-kill-terminal |
|
discard changes and exit |
:q! | M-x kill-emacs | ||
mode | ||||
vim | emacs | emacs | nano | |
change mode | normal to insert: a, i, o, O back to normal: ESC |
M-x mode-name | ||
show mode |
insert, visual, select, and replace indicated on bottom line | displayed on mode line | M-: major-mode | |
mode documentation |
C-h m | M-x describe-mode | ||
set key binding |
:map key cmd | M-x global-set-key RET key cmd | ||
enter insert mode before cursor, after cursor |
i a |
|||
enter insert mode on adjacent line above current line, below current line |
o O |
|||
enter and exit ex mode | Q :vi |
|||
movement | ||||
vim | emacs | emacs | nano | |
location of point |
C-g | C-x = | M-x what-cursor-position | C-c |
swap point and mark | visual mode: o |
C-x C-x | M-x exchange-point-and-mark | |
goto line | 42G :42 |
M-g M-g | M-x goto-line | C-_ M-g |
goto character |
:goto n | M-x goto-char | ||
down | j ↓ |
C-n ↓ |
M-x next-line | C-n ↓ |
up | k ↑ |
C-p ↑ |
M-x previous-line | C-p ↑ |
forward | i → |
C-f → |
M-x forward-char M-x right-char |
C-f → |
backward | h ← |
C-b ← |
M-x backward-char M-x left-char |
C-b ← |
forward beginning of word |
w bigger word: W | C-SPACE | ||
forward end of word |
e bigger word: E | M-f | M-x forward-word | |
backward word |
b bigger word: B | M-b | M-x backward-word | M-SPACE |
beginning of line |
0 | C-a | M-x move-beginning-of-line | C-a |
first nonblank character of line |
^ | M-m | M-x back-to-indentation | |
end of line |
$ | C-e | M-x move-end-of-line | C-e |
previous sentence |
( | M-a | M-x backward-sentence | |
next sentence |
) | M-e | M-x forward-sentence | |
start of paragraph |
{ | M-{ C-↑ |
M-x backward-paragraph | M-( |
end of paragraph |
} | M-} C-↓ |
M-x forward-paragraph | M-) |
matching paren, bracket, or brace | % | C-M-n C-M-p |
M-x forward-list M-x backward-list |
M-] |
column 20 |
20 | | M-g TAB 20 | M-x move-to-column RET 20 | C-_ ,20 |
page down |
C-f | C-v | M-x scroll-up | C-v |
page up |
C-b | M-v | M-x scroll-down | C-y |
half page down |
C-d | M-r M-r M-r C-l | ||
half page up |
C-u | M-r M-r C-l | ||
page other window |
C-M-v | M-x scroll-other-window | ||
page right |
C-x < | |||
page left |
C-x > | |||
center window |
zz | C-l | M-x recenter-top-bottom | |
center point |
M H L |
M-r | M-x move-to-window-line-top-bottom | |
beginning of document |
1G :1 |
M-< | M-x beginning-of-buffer | M-\ |
end of document |
G :$ |
M-> | M-x end-of-buffer | M-/ |
selecting and deleting | ||||
vim | emacs | emacs | nano | |
mark | v V |
C-SPACE C-@ |
M-x set-mark-command | C-^ M-a |
mark rectangular region | C-v | |||
mark whole buffer | ggVG | M-x mark-whole-buffer | ||
delete next char | x | C-d | M-x delete-char | C-d |
delete previous char | X | DEL | M-x delete-backward-char | C-h |
delete next word | dw | M-d | M-x kill-word | |
delete previous word | M-DEL | M-x backward-kill-word | ||
delete to char | M-z | M-x zap-to-char | ||
delete to end of line | d$ D |
C-k | M-x kill-line | |
delete line | dd | C-a C-k C-k | C-k | |
delete newline | point on line: J |
point on line: C-e C-d point on following line: C-a DEL |
||
copy line vim calls this yank |
yy | |||
kill region | visual mode: d |
C-w | M-x kill-region | |
copy region | visual mode: y |
M-w | M-x kill-ring-save | M-a M-6 |
paste emacs calls this yank |
p P |
C-y | M-x yank | C-u |
replace paste with previous copy | M-y | M-x yank-pop | ||
paste line below/above current line | if buffer populated by dd or yy: p P |
|||
delete lines matching regexp | M-x flush-lines | |||
delete lines not matching regexp | M-x keep-lines | |||
system copy | C-c or ⌘c | M-x clipboard-kill-ring-save | ||
system paste | C-v or ⌘v | M-x clipboard-yank | ||
system cut | M-x clipboard-kill-region | |||
files and buffers | ||||
vim | emacs | emacs | nano | |
show current directory |
:!pwd | M-! pwd | ||
change current directory |
:cd dir | M-x cd | ||
open file | :e path | C-x C-f | M-x find-file M-x ido-find-file |
C-r |
open file read only |
:view path | C-x C-r | M-x find-file-read-only | |
toggle buffer read only | M-x read-only-mode | |||
show buffer file name |
:echo bufname("%") | M-: buffer-file-name | ||
list buffers | :ls | C-x C-b | M-x list-buffers M-x buffer-menu |
|
switch buffer | :b bufnum :bprev :bnext |
C-x b | M-x switch-to-buffer M-x ido-switch-buffer |
M-< M-> |
diff buffer and file |
:w !diff % - | M-x diff-buffer-with-file | ||
save buffer |
:w | C-x C-s | M-x save-buffer | C-o ENTER |
save all buffers |
:wa | C-x s | M-x save-some-buffer | |
revert buffer |
:e! | M-x revert-buffer | ||
write buffer to different file | :w path | C-x C-w | M-x write-file M-x ido-write-file |
C-o path |
write region to file |
:w path Don't remove the '<,'> vim inserts after the colon. |
M-x write-region | ||
insert file | :r | C-x i | M-x insert-file M-x ido-insert-file |
C-r |
minibuffer | ||||
vim | emacs | emacs | nano | |
enter minibuffer | : | use command which prompts for an argument | ||
move cursor left, right |
← → |
|||
delete previous char | DEL | DEL | ||
delete previous word | C-w | M-DEL | backward-kill-word | |
go to beginning of command line | C-b | C-a | move-beginning-of-line | |
go to end of command line | C-e | C-e | move-end-of-line | |
clear buffer | C-u | C-a C-k | ||
cancel command | ESC C-c |
C-g | abort-recursive-edit | |
complete | TAB | TAB | minibuffer-complete | |
display completions | C-d | ? | minibuffer-completion-help | |
select completions buffer | M-v | switch-to-completions | ||
show history | :history | |||
go back in history | M-p | previous-history-element | ||
go forward in history | M-n | next-history-element | ||
ido | ||||
vim | emacs | emacs | nano | |
select first match | RET | ido-exit-minibuffer | ||
select input string | C-j | ido-select-text | ||
remove directory from input string | DEL | ido-delete-backward-updir | ||
put first match at end of list | C-s ← |
ido-prev-match | ||
put last match at front of list | C-r → |
ido-next-match | ||
cycle through work directory history next, previous |
M-n M-p |
ido-next-work-directory ido-prev-work-directory |
||
remove current directory from work directory history | M-k | ido-forget-work-directory | ||
goto root directory | // | |||
goto home directory | ~/ | |||
toggle between substring and prefix matching | C-p | ido-toggle-prefix | ||
show matches in separate buffer | ? | ido-completion-help | ||
fall back to switch-buffer | C-b | |||
fall back to find-file | C-f | |||
open in dired | C-d | |||
kill buffer or delete file | C-k | ido-delete-file-at-head | ||
create directory | M-m | ido-make-directory | ||
history | ||||
vim | emacs | emacs | nano | |
repeat |
. | C-x z | M-x repeat | |
undo | u | C-/ C-_ C-x u |
M-x undo | none or M-u when invoked with -u flag |
redo | C-r | move cursor to break undo chain, then C-x u |
none or M-e when invoked with -u flag | |
undo last command not undone |
M-x undo-only | |||
goto version |
:undo 10 | |||
goto previous version 1 version ago, 5 versions ago |
g- 5g- |
|||
goto next version 1 version hence, 5 versions hence |
g+ 5g+ |
|||
go back in time seconds, minutes, days, hours |
:earlier 10s :earlier 10m :earlier 10h :earlier 10d |
|||
go forward in time seconds, minutes, days, hours |
:later 10s :later 10m :later 10h :later 10d |
|||
goto saved version previous, next |
:earlier lf :later lf |
|||
keystroke history |
C-h l | M-x view-lossage | ||
undo branches |
:undolist | |||
searching and replacing | ||||
vim | emacs | emacs | nano | |
search | / | C-s | M-x isearch-forward | C-w |
repeat last search | n | C-s | M-w | |
repeat last search backward | N | C-r | ||
search backwards | ? | C-r | M-x isearch-backward | |
next 'x' on line | f x | |||
previous 'x' on line | F x | |||
search and replace | :%s/pat/repl/g | M-x query-replace | C-\ M-r |
|
recursive edit during search and replace | C-r | |||
exit recursive edit | C-M-c | M-x exit-recursive-edit | ||
search multiple buffers | M-x multi-occur | |||
sorting and counting | ||||
vim | emacs | emacs | nano | |
sort region | visual mode: !sort |
M-x sort-lines | ||
numeric sort | visual mode: !sort -n |
M-x sort-numeric-fields | ||
sort by 2nd field whitespace delimited |
visual mode: !sort -k 2 |
C-u 2 M-x sort-fields | ||
sort by fixed width column | place mark at start of field M-x sort-columns |
|||
reverse | visual mode: !tac |
M-x reverse-region | ||
count lines, words, and chars in document | !wc % | M-x count-words | ||
count regex matches in region |
M-x count-matches | |||
case | ||||
vim | emacs | emacs | nano | |
uppercase word |
gUw uppercase line: gUU |
M-u | M-x upcase-word | |
lowercase word |
guw lowercase line: guu |
M-l | M-x downcase-word | |
capitalize word |
M-c | M-x capitalize-word | ||
uppercase region | visual mode: gU |
C-x C-u | M-x upcase-region | |
lowercase region | visual mode: gu |
C-x C-l | M-x downcase-region | |
title-case region |
M-x capitalize-region | |||
indentation and rectangles | ||||
vim | emacs | emacs | nano | |
insert tab at beginning of line | >> | |||
remove tab from beginning of line | << | |||
indent region | visual mode: > |
|||
syntactically indent line | but electric-indent default in 24.4: TAB |
M-x indent-for-tab-command | ||
syntactically indent region | M-x indent-region | |||
replace tabs in region with spaces | M-x untabify | |||
fill paragraph | M-q | M-x fill-paragraph | ||
justify paragraph | C-u M-q | |||
fill region | M-x fill-region | |||
remove all whitespace on left of region | M-x delete-whitespace-rectangle | |||
vertically align parts in region matching regexp | M-x align-regexp | |||
prepend string to each line of rectangle | C-x r t RET str | M-x string-insert | ||
replace each line of rectange with string | M-x string-rectangle | |||
insert blank rectangle | C-v … I … ESC | C-x r o | M-x open-rectangle | |
delete rectangle | C-v … d | C-x r k | M-x kill-rectangle | |
paste rectangle | C-x r y | M-x yank-rectangle | ||
clear rectangle | C-x r c | M-x clear-rectangle | ||
spelling | ||||
vim | emacs | emacs | nano | |
spellcheck buffer | M-x ispell | C-t | ||
spellcheck mode | :setlocal spell spelllang=en_us | M-x flyspell-mode | ||
panes | ||||
vim | emacs | emacs | nano | |
vertical split | :sp path | C-x 2 | M-x split-window-vertically | |
horizontal split | :vsp path | C-x 3 | M-x split-window-right | |
close other panes | C-x 1 | M-x delete-other-windows | ||
close current pane | :q | C-x 0 | M-x delete-window | |
move to next pane | C-w h, C-w ← C-w l, C-w → C-w j, C-w ↑ C-w k, C-w ↓ |
C-x o C-x O |
M-x other-window C-u -1 M-x other-window |
|
widen pane | C-x { C-x } |
M-x enlarge-window-horizontally | ||
narrow pane | C-x { C-x } |
M-x shrink-window-horizontally | ||
make pane taller | C-w + | M-x enlarge-window | ||
make pane shorter | C-w - | |||
rotate panes | C-w r | |||
balance panes | C-x + | M-x balance-windows | ||
macros | ||||
vim | emacs | emacs | nano | |
start recording macro | q letter | C-x ( | M-x kmacro-start-macro | |
end recording macro | q | C-x ) | M-x kmacro-end-macro | |
run macro | @ letter | C-x e | M-x kmacro-end-and-call-macro | |
run macro on each line in region | C-x C-k r | M-x apply-macro-to-region-lines | ||
insert counter | C-x C-k C-i | M-x kmacro-insert-counter | ||
set counter | C-x C-k C-c | M-x kmacro-set-counter | ||
set counter format | uses printf-style format specifiers: C-x C-k C-f |
M-x kmacro-set-format | ||
query user to continue | user can respond with: y n q C-x q |
|||
put user in recursive edit | C-u C-x q | |||
ascii art | ||||
vim | emacs | emacs | nano | |
ascii art mode | M-x picture-mode | |||
return to original mode | C-c C-c | M-c picture-mode-exit | ||
up | C-p | available in all modes; bound to C-p only in picture-mode: M-x picture-move-up |
||
down | C-n | M-x picture-move-down | ||
left | C-b | M-x picture-forward-column | ||
right | C-f | M-x picture-backward-column | ||
beginning of line | C-a | M-x picture-beginning-of-line | ||
beginning of next line | RET | M-x picture-newline | ||
move to next non-blank on current line | C-u M-TAB | C-u M-x picture-tab-search | ||
move under next non-blank on previous line | M-TAB | M-x picture-tab-search | ||
set movement to left after insert | C-c < | M-x picture-movement-left | ||
set movement to right after insert | C-c > | M-x picture-movement-right | ||
set movement to up after insert | C-c ^ | M-x picture-movement-up | ||
set movement to down after insert | C-c . | M-x picture-movement-down | ||
set movement to up and left after insert | C-c ` | M-x picture-movement-nw | ||
set movement to up and right after insert | C-c ' | M-x picture-movement-ne | ||
set movement to down and left after insert | C-c / | M-x picture-movement-sw | ||
set movement to down and right after insert | C-c \ | M-x picture-movement-se | ||
move in direction of movement | C-c C-f | M-x picture-motion | ||
move opposite direction of movement | C-c C-b | M-x picture-motion-reverse | ||
last nonblank on line | C-e | M-x picture-end-of-line | ||
insert blank line | C-o | M-x picture-open-line | ||
duplicate line | C-j | M-x picture-duplicate-line | ||
replace with space | SPC C-d |
M-x picture-clear-column | ||
move left and replace with space | DEL | M-x picture-backward-clear-column | ||
delete char | C-c C-d | M-x picture-delete-char | ||
delete to end of line | C-k | M-k picture-clear-line | ||
org | ||||
vim | emacs | emacs | nano | |
outlines | ||||
cycle list item visibility folded, children, descendants |
TAB | M-x org-cycle | ||
cycle buffer outline visibility overview, contents, all |
C-u TAB | M-x org-global-cycle | ||
tables | ||||
create table | C-c | | M-x org-table-create-or-convert-from-region | ||
align table columns | C-c C-c | M-x org-table-align | ||
move column at point right | M-→ | M-x org-table-move-column-right | ||
move column at point left | M-← | M-x org-table-move-column-left | ||
insert row above point | M-x org-table-insert-row | |||
insert column left of point | M-x org-table-insert-column | |||
insert horizontal line under point | C-c - | M-x org-table-insert-hline | ||
delete column at point | M-x org-table-delete-column | |||
sort table by column at point | C-c ^ | M-x org-sort | ||
import tab delimited file | M-x org-table-import | |||
export tab delimited file | M-x org-table-export | |||
hyperlinks | ||||
visit link | C-c C-o RET |
M-x org-open-at-point | ||
export | ||||
export as html | C-c C-e h h | M-x org-export-dispatch h h | ||
input methods and encodings | ||||
vim | emacs | emacs | nano | |
insert unicode character | C-v u 221e | C-x 8 RET infinity C-x 8 RET 221e |
M-x ucs-insert | |
set input method |
C-x RET C-\ | M-x set-input-method | ||
list input methods |
M-x list-input-methods | |||
enable/disable input method |
C-\ | M-x toggle-input-method | ||
describe input method |
C-h I | M-x describe-input-method | ||
how to type character at point | M-x quail-show-key | |||
set file encoding | :w ++enc=utf-8 foo.txt | C-x RET f | M-x set-buffer-file-coding-system | |
set end of line character | M-x set-buffer-file-coding-system RET utf-8-unix M-x set-buffer-file-coding-system RET utf-8-dos M-x set-buffer-file-coding-system RET utf-8-mac |
|||
show file encoding | M-: buffer-file-coding-system | |||
list available encodings | :h encoding-values | M-x list-coding-systems | ||
help | ||||
vim | emacs | emacs | nano | |
help (q to exit help mode) | :help | C-h i | M-x info | C-g |
summarize help commands | C-h ? | M-x help-for-help | ||
tutorial | :!vimtutor | C-h t | M-x help-with-tutorial | |
list key bindings | C-h b | M-x describe-bindings | ||
describe mode | C-h m | M-x describe-mode | ||
show key binding | :help key | C-h c C-h k |
M-x describe-key-briefly M-x describe-key |
|
function documentation | :help :func :help key |
C-h f | M-x describe-function | |
variable documentation | :help 'var' | C-h v | M-x describe-variable | |
apropos | C-h a | M-x apropos-command also variables, properties, functions, and macros: M-x apropos |
||
info-mode | ||||
visit help link | C-] | RET | M-x Info-follow-nearest-node | |
help link style | |braced by pipe chars| | underlined and blue | ||
go back a help link, forward a help link | C-o none |
l r |
M-x Info-history-back M-x Info-history-forward |
|
navigate info page hierarchy | n p u |
M-x Info-next M-x Info-prev M-x Info-up |
||
man page | :!man ls | M-x man RET ls | ||
browse files | ||||
vim | emacs | emacs | nano | |
display directory in buffer | :e dirpath | M-x dired | C-r C-t | |
search for files recursively by name | M-x find-name-dired | |||
search for files recursively by contents | M-x find-grep-dired | |||
search for files recursively by contents using ag -Q | install ag and ag.el: M-x ag-dired M-x ag-project-direct |
|||
search for files recursively by contents using ag | M-x ag-dired-regexp M-x ag-project-direct-regexp |
|||
dired-mode | ||||
delete | D | |||
flag for deletion | d | M-x dired-flag-file-deletion | ||
mark | m | M-x dired-mark | ||
mark by regex | % m | M-x dired-mark-files-regexp | ||
unflag or unmark | u | M-x dired-unmark | ||
unflag or unmark all | U | M-x dired-unmark-all-marks | ||
delete flagged files | x | M-x dired-do-flagged-delete | ||
mark autosave files for deletion | # | M-x dired-flag-auto-save-files | ||
mark twiddle files for deletion | ~ | M-x dired-flag-backup-files | ||
visit file | RET | RET or f | M-x dired-find-file | RET or s |
visit file in other window | o | M-x dired-find-file-other-window | ||
visit parent directory | ^ | M-x dired-up-directory | ||
create new file | M-! touch path | |||
create new directory | + | M-x dired-create-directory | ||
shell command passes file under point as argument | ! | M-x dired-do-shell-argument | ||
show subdirectory | i | M-x dired-maybe-insert-subdir | ||
hide subdirectory | $ | M-x dired-hide-subdir | ||
hide all subdirectories | M-$ | M-x dired-hide-all | ||
toggle sort order filename or last modification time |
s | s | M-x dired-sort-toggle-or-edit | |
shell | ||||
vim | emacs | emacs | nano | |
open shell buffer | :!sh | M-x shell install multi-term: M-x multi-term |
||
emulate a shell | M-x eshell | |||
execute shell command | :!cmd args … | M-! | M-x shell-command | |
send region to shell command | M-| | M-x shell-command-on-region | ||
send document to shell command | !cmd % | |||
shell-mode | ||||
recall prev cmd in history | M-p | M-x comint-previous-input | ||
recell next cmd in history | M-n | M-x comint-next-input | ||
grep | ||||
emacs | emacs | |||
grep | :grep pattern * :cw |
M-x grep | ||
grep directories recursively | :grep -r pattern . :cw |
M-x find-grep | ||
stop grep | C-c | M-x kill-grep | ||
grep buffer | M-x occur | |||
ag -Q | install ag and ag.vim: :Ag -Q pattern |
install ag and ag-mode: M-x ag M-x ag-project |
||
ag -Q -G | M-x ag-files M-x ag-project-files |
|||
ag | :Ag pattern | M-x ag-regexp M-x ag-project-regexp |
||
close all ag-mode buffers | M-x ag-kill-buffers | |||
close other ag-mode buffers | M-x ag-kill-other-buffers | |||
grep-mode | ||||
next match in source | :cn | n | M-x next-error-no-select | |
prev match in source | :cp | p | M-x previous-error-no-select | |
next match in output not in current file | :cnf | M-} | M-x compilation-next-file | |
prev match in output not in current file | :cpf | M-{ | M-x compilation-previous-file | |
goto match in source | C-c C-c RET |
M-x compile-goto-error | ||
stop grep | C-c C-k | M-x kill-compilation | ||
re-run grep | g | M-x recompile | ||
quit grep mode | :ccl | q | M-x quit-window | |
tags | ||||
vim | emacs | emacs | nano | |
create tags file | M-! find . -name '*.c' | etags | |||
load tags file | M-x visit-tags-table | |||
goto tag | :tag name | M-. | M-x find-tag | |
goto tag at point | C-] | |||
open tag in other window | C-x 4 . | M-x find-tag-other-window | ||
pop tag stack | C-o :pop |
M-* | M-x pop-tag-mark | |
show tag stack | :tags | |||
show tag signature | ||||
tag search | M-x tags-search M-, to go to next occurrence |
|||
diff | ||||
vim | emacs | emacs | nano | |
diff two files | $ vimdiff file file | M-x ediff | ||
diff three files | $ vimdiff file file file | M-x ediff3 | ||
switch to other window | C-W w | |||
ediff-mode | ||||
apply hunk | do | a | ||
apply hunk in reverse | dp | b | ||
next hunk | ]c | n | ||
previous hunk | [c | p | ||
save first file | wa | |||
save second file | wb | |||
save diff | wd | |||
git | ||||
emacs | emacs | |||
diff buffer with version control | C-x v = | M-x vc-diff | ||
revert buffer using version control | C-x v u | M-x vc-revert | ||
git blame on buffer | install vim-fugitive :Gblame |
C-x v g | M-x vc-annotate | |
git status; use working directory | install vim-fugitive :Gstatus |
install magit M-x magit-status |
||
git status; prompt for project path | C-u M-x magit-status | |||
magit-mode | ||||
refresh status | g | M-x magit-refresh | ||
stage | s | M-x magit-stage-item | ||
unstage | u | M-x magit-unstage-item | ||
commit | c | M-x magit-key-mode-popup-committing | ||
push | P | M-x magit-key-mode-popup-pushing | ||
add file to .gitignore | i | M-x magit-ignore-item | ||
show commit history | l | M-x magit-key-mode-popup-logging | ||
show git process output | $ | M-x magit-display-process | ||
close status buffer | q | M-x magit-mode-quit-window | ||
magit-commit-mode | ||||
submit commit | C-c C-c | M-x git-commit-commit | ||
magit-log-mode | ||||
compile | ||||
emacs | emacs | |||
compile | :make | M-x compile | ||
stop compilation | M-x kill-compilation | |||
compilation-mode | ||||
recompile | g | M-x recompile | ||
next error in output | M-n | |||
prev error in output | M-p | |||
next error in different source file | M-} | |||
prev error in different source file | M-{ | |||
debug | ||||
vim | emacs | emacs | ||
start gdb | M-x gdb | |||
start jdb | M-x jdb | |||
set breakpoint | C-x SPC | M-x gud-break | ||
send ^C to debugger | C-c C-c | M-x comint-interrupt-subjob | ||
lisp interaction | ||||
emacs | emacs | |||
evaluate in minibuffer | M-: | M-x eval-expression | ||
evaluate sexp and insert result after point | C-j | M-x eval-print-last-sexp | ||
evaluate sexp and echo result in minibuffer | C-x C-e | M-x eval-last-sexp | ||
evaluate region | M-x eval-region | |||
eval buffer | M-x eval-buffer | |||
lisp REPL | M-x ielm | |||
syntax highlighting | ||||
vim | emacs | emacs | ||
turn on | :syntax enable | M-x font-lock-mode | ||
turn off | :syntax of | M-x font-lock-mode | ||
show mode | :setlocal syntax? | C-h m | M-x describe-mode | |
face at point | C-u C-x = | M-x describe-face RET | ||
customize face | :hi Comment ctermfg=darkgray | M-x customize-face | ||
list faces | :help group-name | M-x list-faces-display | ||
list colors | :runtime syntax/colortest.vim | M-x list-colors-display | ||
____________________________ | _________________________________ | _________________________________ | __________________________________________________ | _________________________________ |
Startup
launch
How to launch the editor from the command line.
open file
How to launch the editor from the command line with a file loaded into a buffer.
open file on line 100
How to launch the editor from the command line with a file loaded and the point on line 100 of the buffer.
open file on column 20 of line 100
How to launch the editor from the command line with a file loaded and the point on column 20 of line 100 of the buffer.
open file in read-only mode
open multiple files
open directory
startup file
skip startup file
Text Editing
show version
How to get the version of the editor.
It is also possible to determine the version from the command line:
$ vim --version
$ emacs --version
$ nano --version
redraw display
How to make the editor redraw the display.
insert 'x'
How to insert the character 'x' into a document.
vim:
One must be in insert mode to insert 'x'.
In normal mode keys are bound to commands. The key 'x' is bound to a command which deletes the character under the point.
emacs:
In Emacs modes used for editing documents, printing characters are inserting by typing the corresponding keys. All printing characters are bound to the Emacs command self-insert-command.
insert by unicode point
How to insert a character by its 4 hex digit Unicode point.
insert by unicode name
How to insert a character by its Unicode name.
emacs:
When typing the Unicode name tab completion is available.
get unicode point
How to get the Unicode point of the character at the point.
emacs:
Information about the character including location in the buffer, point, Unicode name, and Unicode category is displayed in a separate buffer.
cancel
How to abort the current operation.
vim:
In all modes except for ex mode, ESC returns Vim to normal mode.
emacs:
C-g (M-x keyboard-quit) can be used in two situations in Emacs: (1) if you executed a command and are being prompted for input in the minibuffer, but would like to cancel the command without entering input, and (2) to remove an argument that was set with the C-u (M-x universal-argument) command.
If you've entered part of a multi-keystroke command sequence and wish to cancel, C-g can be used because there aren't any multi-keystroke sequences that end with C-g.
insert control character
How to insert a literal control character.
The control characters are assigned ASCII codes 0 through 31 and 127.
The control characters can be entered by holding down the control key and then pressing the ASCII character with the same code as the control character plus 64 modulo 128. The control character sequences for ASCII 0 through 31 are C-@, C-A, …, C-Z, C-[, C-\, C-], C-^ and for ASCII 127 is C-?. In particular ESC is C-[. Although the ASCII letters are uppercase, it is not necessary to use the shift key when entering the control sequence.
Both Vim and Emacs will display most of the control characters using caret notation: ^@, ^A, …, ^Z, ^[, ^\, ^], ^^, and ^? for DEL.
vim:
Some control key combinations are bound to vim commands. To insert a literal control character, precede the combination with C-v.
emacs:
Most control key combinations are bound to emacs commands. To insert a literal control character into a buffer, precede the combination with C-q.
insert newline
How to insert a newline in contexts where the RET key won't insert a newline.
vim:
The C-v RET mechanism is necessary when using :map to define a key binding for a command sequence containing a newline.
emacs:
C-q C-j is necessary to put a newline in the either the search pattern or the replacement string of M-x query-replace.
auto complete
How to auto complete a partially typed word using other words in the buffer.
emacs:
M-x hippie-expand will use all open buffers to find candidates to complete the word.
repeat next command n times
How to repeat a command n times.
vim:
When in normal mode, any positive integer can be used to repeat a command multiple times.
emacs:
When given a universal argument of n, many emacs commands will repeat themselves n times, but the behavior is not universal.
repeat next command 4 times
How to repeat a command 4 times.
emacs:
The C-u command defaults to 4 when no argument is provided.
repeat next command 16 times
How to repeat a command 16 times.
emacs:
Typing C-u n times is the same as setting the universal argument to 4n.
font size
How to adjust the font size. This can only be done when the editors are running as GUI applications.
vim:
When using MacVim use ⌘- and ⌘= to decrease or increase the font size.
When using gVim change the font via the menu system: Edit | Select Font…
emacs:
Aquamacs will adjust font size in response to ⌘= and ⌘- but GNU Emacs will not.
exit
How to exit the editor. If there are unsaved changes the user is prompted whether to save them.
vim:
When there are multiple tabs or panes, :q only closes the current tab or pane.
save changes and exit
How to save any unsaved changes to the current buffer and exit.
vim:
If there are multiple panes or tabs, vim does not exit but instead closes the current tab or pane.
emacs:
If there are changes to other buffers, emacs will prompt whether they should be saved.
discard changes and exit
How to discard all unsaved changes and exit.
vim:
If there are other tabs or panes, vim does not exit but instead closes the current tab or pane.
Mode
vim:
mode | label | description |
---|---|---|
normal | none | Also called command mode. Most keys are bound to commands. |
insert | -- INSERT -- | Keys insert literal text into the buffer. |
replace | -- REPLACE -- | Like insert mode but existing text is overwritten. |
visual | -- VISUAL -- | Region between where cursor was at visual mode entry and current position is highlighted. If an operator is selected it will operate on the highlighted region. |
select | -- SELECT -- | Highlights like visual mode. Hitting a printable character deletes selection and enters insert mode. |
operator-pending | Entered after executing a command which expects motion keys to be used to define a region. | |
cmdline | Cursor is in bottom row of window. Line editor commands can be entered. | |
ex | Like cmdline mode, except that does not return to normal mode after a command is executed. | |
_______________________ | _______________________ |
emacs:
Here are some commonly used Emacs modes:
- fundamental-mode
- text-mode
- markup modes: HTML mode, LaTeX mode, …
- programming language source modes: C mode, Python mode, …
- dired-mode
- shell-mode
- lisp-interaction-mode
Each buffer has a mode which in turn determines the keybindings and the values in variables which customize the behavior of Emacs.
When opening a file for editing in a buffer, Emacs uses the following four techniques in succession to determine the correct mode to use:
- file variables in first two lines of file: -*- mode: Lisp;-*-
- interpreter-mode-alist: a mapping from interpreter names in the shebang to modes
- magic-mode-alist: usually empty, but can map regexes applied to text in the file to modes
- auto-mode-alist: maps regexes applied to the file name to modes. Usually they identify file type by suffix.
change mode
vim:
The above diagram documents how one moves between modes
Pressing ESC twice returns vim to normal mode except when it is in ex mode. To exit ex mode type vi.
show mode
vim:
Normally when vim is in insert, visual, select, or replace mode, the mode name is indicated on the bottom line of the screen between double hyphens. This behave can be turned off or on with the commands
:set noshowmode
:set showmode
mode documentation
How to get documentation for the current mode.
set key binding
vim:
:map defines a binding for normal, visual, and operator-pending modes.
The commands :nmap, :imap, :vmap, :omap, and :cmap can be used to define commands specific to normal, insert, visual, operator-pending, and cmdline mode.
emacs:
global-set-key changes the key binding for all modes. local-set-key can be used to change the key binding for the current major mode. Also global-unset-key and local-unset-key can be used to make a key have no binding.
Here's an example of how to set a key binding in the emacs startup file init.el.
(global-set-key "\C-cb" 'revert-buffer)
The keys for printing characters are bound to the command self-insert-command.
enter insert mode
How to enter insert mode on the current line.
Only Vim has insert mode.
enter insert mode on adjacent line
How to enter insert mode by creating a new line either above or below the current line.
Only Vim has insert mode.
enter and exit ex mode
How to exit ex mode.
Only Vim has ex mode.
Movement
location of point
vim:
Vim calls the point the cursor.
emacs:
The minor modes line-number and column-number can be used to display the location of the point in the mode line:
M-x column-number-mode
M-x line-number-mode
swap point and mark
vim:
Vim does not use the term mark. The documentation refers to it as "the other end of the selection".
goto line
How to go to a specific line number. The editors number the lines in the buffer starting from one.
nano:
If two arguments are provided separated by a comma, they are taken to be the line and column to which the cursor should be moved.
goto character
How to go to a character by its position in the buffer.
down
How to go down one line.
up
How to go up one line.
forward
How to go forward one character.
emacs:
In the typical case of left-to-right scripts, M-x forward-char and M-x right-char are synonyms.
In right-to-left scripts such as Arabic M-x forward-char and M-x left-char are synonyms.
backward
How to go back one character.
forward beginning of word
How to advance to the beginning of the next word.
forward end of word
How to advance to the end of the current word or the end of the next word if already at the end of the current word.
backward word
How to go back by one word.
beginning of line
How to go to the beginning of the current line.
first nonblank character of line
How to go to the first non-whitespace character of the current line.
end of line
How to go to the end of the current line.
previous sentence
If the point is mid-sentence, how to go to the start of the sentence. If already at the start of the sentence, how to go to the start of the previous sentence.
emacs:
By default sentence boundaries are defined by periods . or question marks ? followed by whitespace.
The variable sentence-end can be set to a regular expression to customize the definition of a sentence.
next sentence
If the point is mid-sentence, how to go the the end of the sentence. If already at the end of the sentence, how to go the end of the following sentence.
start of paragraph
If the point is mid-paragraph, go to the start of the paragraph. If the point is already at the start of a paragraph, go to the start of the previous paragraph.
emacs:
Paragraphs are defined by blank lines.
The behavior can be customized by setting the variables paragraph-start and paragraph-separate to appropriate regular expressions.
end of paragraph
If the point is mid-paragraph, go to the end of the paragraph. If the point is already at the end of a paragraph, go to the end of the following paragraph.
matching paren, bracket, or brace
column 20
How to move the point to column 20 of the current line.
page down
How to scroll the window down the buffer by one page.
A page in this context is defined by how much can be displayed in the window. After scrolling down, none of the content that was previously visible in the window will be visible unless the end of the buffer was reached.
After paging down, the point is placed at the top of the window.
emacs:
If the end of the buffer is already visible, Emacs beeps and does not move the point.
vim:
If the end of the buffer is already visible, Vim will place the last line at the top of the window. If the last line is already at the top of the window Vim beeps.
page up
How to scroll the window up the buffer by one page.
half page down
How to scroll the window down the buffer by half a page.
half page up
How to scroll the window up the buffer by half a page.
page other window
How to scroll the window down in a buffer that isn't currently selected.
emacs:
If a numeric argument is provided, the window scrolls down by that number of lines. If the numeric argument is negative the window scrolls up.
If there is more that one other window, C-M-v operates on each of them in turn.
page right
page left
center window
How to move the window (the visible portion of the buffer) so that the point is centered. The position of the point in the document does not change.
emacs:
Repeated use of C-l cycles the point through the positions center, top, and bottom of the window. The portion of the buffer visible in the window changes, not the position of the point in the buffer.
center point
How to move the point to the center of the window (the visible portion of the buffer). The portion of the buffer visible in the window does not change.
vim:
M moves to the beginning of the line at the center of the window; H to the beginning of the first line at the top of the window; L to the beginning of the first line at the bottom of the window.
emacs:
Repeated use M-r cycles the point through the positions center, top, and bottom of the window. The point moves in the buffer, but the portion of the buffer visible in the window does not change.
beginning of document
How to move the point to the beginning of the buffer.
end of document
How to move the point to the end of the buffer.
Selecting and Deleting
delete to end of line
Delete all characters to the end of the line.
vim:
The character under the cursor is also deleted. If the point is on the last character of the line it is the only character that will be deleted. The end-of-line character between the current and subsequent line is never deleted.
emacs:
If the point is already at the end of the line, the following line is joined to the end of the current line. Repeated use of C-k can thus be used to remove several adjacent lines.
Files and Buffers
show current directory
How to show the current directory.
change current directory
How to change the current directory.
emacs:
A current directory is associated with each buffer. If the buffer is visiting a file the current directory will be the directory containing the file. Other buffers will have a default buffer directory which might be the current directory when emacs was launched or the user home directory. The current directory for any buffer can be changed with M-x cd.
open file
How to open a file for editing. A copy of the file is made in a buffer.
emacs
Emacs can edit files on remote machines. The format for specifying a remote file is
/USER@MACHINE:/PATH/TO/FILE
The format for editing a file as root is
/sudo::/PATH/TO/FILE
open file read only
How to open a file for viewing only.
show buffer file name
How to show the complete path name for the file the buffer is visiting.
list buffers
switch buffer
diff buffer and file
save buffer
save all buffers
revert buffer
write buffer to different file
write region to file
insert file
Minibuffer
The minibuffer is a line at the bottom of the frame which Emacs uses to prompt the user for an argument.
The Vim analog of the minibuffer is the command line. It also appears at the bottom of the window. The Vim command line is used for entering a command and all of its arguments, whereas the Emacs minibuffer is generally used for entering a single argument. However, in the case of M-: and M-! that argument is a Lisp expression to be evaluated or a shell command to be evaluated.
Both the Emacs minibuffer and the Vim command line offer tab completion.
The Emacs minibuffer is to a certain extent a buffer like any other. Commands used to navigate and edit other buffers are general available in the minibuffer. It is an error to call a command which solicits an argument via the minibuffer when already in the minibuffer.
The Vim command line is a mode distinct from the other Vim modes: command mode, insert mode, and visual mode. It provides its own set of commands for manipulating the text which appears on the command line.
Ido
The Ido library ships with Emacs. It modifies the behavior of the minibuffer when prompting for buffer names and file names.
When the minibuffer is in ido-mode, TAB completion is replaced by a match list which is updated each time a character of input is entered. Hitting RET selects the first item in the match list. The arrow keys can be used to bring a different match up to the front of the list.
The rules for finding matches can be customized. By default matches for which the input string is a prefix are listed first, following by matches for which the input string is a substring.
Another feature of ido-mode is the directory history. This is a list of all the directories that have been visited in ido-mode, sorted by time of most recent visit. The directory history is only relevant when searching for files, not buffers. When searching for files the input is divided by ido-mode into the dirname and basename portion. Older directories in the directory history can be recalled with M-p.
To enable ido-mode, put the following in the emacs startup file:.
(require 'ido)
(ido-mode t)
History
Vim and Emacs keep a history for each buffer. Every command and edit which modifies the buffer can be undone, and every command that was undone can be redone.
As a buffer is being edited, the history of the buffer can be represented as a sequence of buffer versions connected by the commands which were used to transition between them. If the redo command did not have to be supported, the undo operation could be implemented by deleting the most recent version from the buffer history. The redo command introduces complexity which Vim and Emacs handle in different ways.
vim:
Vim represents buffer history with a tree of versions. Each version was created from its parent in the tree by a command. A branch is created when a user undoes one or more edits and then starts to edit a non-leaf version. The undo and redo commands only permit moving between version on the current branch, but Vim provides two mechanisms for recovering versions from other branches. Vim numbers versions sequentially in the order in which they were first created. Providing the :undo command with an integer argument will recover the version. One can also use the g- or g+ commands to move to a version number relative to the current version number. Vim also records a timestamp whenever the buffer is changes, permitting the user to recover buffer version that was current 5 minutes ago.
emacs:
Emacs keeps its buffer version history in a sequential list. Undo is implemented not by deleting from the end of the list, but by adding a new version to the list created by a command which reverses the command which is being undone. Thus the undo command is a redo when the command that is being undone was itself an undo. However, there is a wrinkle which is necessary to make it possible to go back more than one version. When the undo command is issued multiple times in succession, the commands which reverse the commands being undone are not added to the version history until a command other than undo is made. This other command is said to break the undo chain. The undo-only command is useful when one breaks the undo chain accidentally.
repeat
How to repeat the last command.
undo
How to undo the last command.
redo
How to redo a command that was just undone.
undo last command not undone
emacs:
When using Emacs one may sometimes break the undo chain accidentally. For example, suppose that one wanted to undo the last 8 changes. If one hit undo 7 times and then accidentally moved the cursor, the undo chain would be broken and Emacs would insert 7 undo commands onto the version history. Now the desired version is 15 versions back. The solution is to use M-x undo-only which skips all versions created by the undo command and the versions that were undone by those undo commands.
goto version
Put the buffer into the state it had at the specified version.
vim:
Vim numbers all versions of the buffer sequentially from 1 in the order that they were first created.
goto previous version
Put the buffer into the state it had the specified number of versions ago.
goto next version
When not currently at the most recent version, this command puts the buffer in a state a specified number of versions more recent.
go back in time
Put the buffer in the state it had the specified amount of time ago.
go forward in time
When not currently at the most recent version, this command puts the buffer in state that it had at the specified amount of time after it had the current buffer state.
goto saved version
keystroke history
undo branches
Searching and Replacing
Sorting and Counting
sort region
How to sort the lines in a region.
numeric sort
How to sort the lines in the region numerically.
sort by 2nd field
How to sort the lines in a region by the second field where the fields are whitespace delimited.
sort by fixed width column
How to sort the lines by a column when the columns are fixed width.
reverse
How to reverse the lines in a region.
count lines, words, and chars in document
How to count the number of lines, words, and characters in the document.
count regex matches in region
How to count the number of strings in a region which match a regular expression.
Case
uppercase word
lowercase word
capitalize word
uppercase region
lowercase region
title-case region
Indentation and Rectangles
Spelling
Panes
Macros
ASCII Art
Org
Input Methods and Encodings
insert unicode character
How to insert a unicode character by name or by 4-hex code.
vim:
For Unicode characters outside of the BMP, use a capital U and 8 hex characters. This will enter the code for the Egyptian hieroglyph depicting Ra seated with an ankh:
C-v U 0001305b
set input method
How to set the input method.
Input methods provide a more convenient method of entering non-English script. Some input methods remap the keyboard to a configuration used in a non-English speaking country. Other input methods preserve the English QWERTY layout but remap digraphs or combining characters to characters which are otherwise unavailable.
list input method
List the available input methods.
Emacs 24.1 has 187 input methods. We describe a select few input methods in the following table:
input method | description |
---|---|
arabic | Keys mapped in a manner popularized by the IBM PC. This keyboard places letters in the traditional locations used by Arabic typewriters. The positions of the letters must be memorized; there is no input method which allows someone who knows the Arabic letters and the QWERTY keyboard to predict the location of the letters. |
cyrillic-jcuken | JCUKEN keyboard. This is the most common keyboard layout in Russia. |
cyrillic-yawerty | Places letters where phonologically similar Latin letters are found on the QWERTY keyboard. Note the following substitutions: q:я w:в y:ы u:у [:ш ]:щ f:ф h:х j:й x:ь c:ц v:ж |
french-azerty | French AZERTY keyboard. This is the most common keyboard layout in France. |
french-postfix | QWERTY keyboard. Use ` ' ^ " after vowels to create accented vowels. Use , after c to create ce cedille. Use << and >> to create guillemots. |
german | German QWERTZ keyboard. This is the most common keyboard layout in Germany. |
german-posfix | QWERTY keyboard. Use ae, oe, ue, and sz digraphs to create the special letters. |
german-prefix | QWERTY keyboard. Use double quote " before vowel to create umlaut. Use " before s to create long s. |
greek | Places letters where the corresponding Latin letters are found on the QWERTY keyboard. Note the following substitutions: w:ς y:υ u:θ f:φ h:η j:ξ x:χ c:ψ v:ω Since 1982 modern Greek only uses the acute accent and the dieresis which can be entered with the ; (acute) and : (dieresis) prefixes. To enter a literal ; or : use q or Q. Use ;< or ;> for guillemots. Ancient Greek is written using the polytonic system which also has a grave accent, circumflex, and rough breathing mark. Unicode provides code points for letters marked with these diacritics, but there is no Emacs input method for them. |
japanese | QWERTY keyboard. Type in romanji and the characters are converted to kana on the fly. In a second pass some of the kana are converted to kanji on the fly. |
spanish-keyboard | A layout close to the layout used in Spain and Latin America. |
spanish-postfix | QWERTY keyboard. Use suffix ' ~ " for accents. Double suffix (n~~ -> n~) to prevent accent. Use !/ and ?/ for upside down punctuation. |
spanish-prefix | QWERTY keyboard. Use prefix ' ~ " for accents. Use ~! and ~? for upside down punctuation. Use ~< and ~> for guillemots. |
____________________ |
enable/disable input method
Used to turn off or re-enable an input method.
describe input method
Document for an input method. By default the current input method is described.
Help
show key binding
How to show the function invoked by a key.
vim:
In Vim the key is the only name for a function. Here is the notation to see the function bound to a control character:
:help CTRL-B
To see the function bound to a command available in insert mode or visual mode:
:help i_CTRL-O
:help v_CTRL-]
emacs:
describe-key-briefly shows the function invoked by the key; describe-key shows the function and its documentation.
Browse Files
Shell
emacs:
Emac provides two modes for running external shells: shell-mode and term-mode.
shell-mode buffers behave more like Emacs edit buffers. It is possible to use the movement commands and the edit commands to change a previous command. Hitting RET then runs the modified version of the command. It is also possible to edit the results of the commands, which might be useful when cleaning up a shell session before sharing it. The arrow keys are interpreted by Emacs, but M-p and M-n to recall commands from the command history.
By default, the buffer is *shell*. Use C-u M-x shell to be prompted for a different name.
Commands which expect ANSI escape support do not work in a shell-mode buffer, e.g. fish, less, tmux, vi, or emacs itself. For such commands, one can use term-mode. We describe term-mode as it behaves when the 3rd party library multi-term.el is installed. multi-term.el has these advantages:
- Can create more than one terminal buffer.
- Control and Meta modified keystrokes are sent to Emacs instead of the terminal process.
- No toggle between line mode and character mode.
term-mode under multi-term.el has quirks that can be confusing. For example, M-DELETE is interpreted by Emacs and erases part of the current command in the buffer, yet the characters being erased have already been sent to the shell.
Here is a summary of how keystrokes are handled in shell-mode and term-mode with multi-term.el installed:
shell-mode | term-mode w/ multi-term | |
---|---|---|
RET | current line to process | RET to process |
C-c C-c | C-c to process | C-c to process |
C-c KEY | C-c KEY to emacs | C-c KEY to emacs |
KEY | KEY to emacs | KEY to process |
M-KEY | M-KEY to emacs | M-KEY to emacs |
C-KEY | C-KEY to emacs | C-KEY to emacs |
open shell buffer
execute shell command
send region to shell command
send document to shell command
Grep
vim:
After performing a grep, the :cw or :copen command can be used to open the matching lines in a quickfix buffer. The quickfix buffer can be used to navigate to locations in the source files.
Vim has a command called :vimgrep which can be used in place of :grep. :vimgrep is an internal command and hence it works on systems where :grep is not installed. It does not have any option flags, however.
Running :grep or :vimgrep puts the results in a single quickfix buffer and hence overwrites the results of a previous search. The variants :lgrep and :lvimgrep put the results in a quickfix buffer which is local to the current buffer. The commands for navigating a local quick fix buffer are different: :ln, :lp, :lnf, :lcl.
emacs:
Emacs provides commands for running grep and displaying the results in a grep-mode buffer. The advantage of displaying the results in grep-mode is that each match is a hyperlink pointing to the location of the match in its original file. grep-mode is a child mode of compilation-mode.
For the hyperlinks in grep-mode to work, the output of the grep must have file names and line numbers, which is whygrep is invoked with the -nH flags.
When invoking M-x grep, Emacs provides the command that it is going to execute and lets the user edit it. The user must add the search term and the name of the files to search. The user can change the flags to search directories recursively, i.e.grep -r. The user can even use a different command entirely, such as a combination of find and xargs grep. The only difference between M-x grep and M-x find-grep is the default choice for the command and flags. These choices can be changed by setting the grep-command and grep-find-command variables.
There is a command line tool ag which can be used as a replacement for grep -r or find | xargs grep. It has simpler syntax for invocation, and will use version control ignore files such as .gitignore to select the files to be searched intelligently. To install ag, look for a package name such as the_silver_searcher or silvesearcher-ag.
ag-mode, which must be installed, invokes ag with the correct arguments so that the matches can be displayed in an Emacs buffer. Whereas M-x grep and M-x find-grep use a single *grep* buffer and replace the results of a previous search, ag-mode will create a new buffer for each search.
Tags
How to create emacs tags for all files in dir1 and dir2 with the suffix .rb:
find dir1 dir2 -name '*.rb' | xargs etags
Diff
Git
Compile
Debug
Lisp Interaction
Syntax Highlighting
Programming
vimscript | emacs lisp | ||
---|---|---|---|
startup script | ~/.vimrc | ~/.emacs.d/init.el | |
execute file | :source foo.vim | M-x load-file foo.el | |
execute function | M-: (foo) if interactively callable: M-x foo |
||
execute command | normal x | ||
set variable | let i = 1 | (setq i 1) | |
display variable | echo i | i | |
delete variable | unlet i no error msg if doesn't exist: unlet! i |
||
show variables | let | ||
global variable | let g:foo = 42 | ||
script local variable | let s:foo = 42 | ||
window local variable | let w:foo = 42 | ||
file local variable | |||
buffer local variable | let b:foo = 42 | ||
end-of-line comment | echo 1 + 1 " addition | (+ 1 1) ; addition | |
arithmetic and logic | |||
vimscript | emacs lisp | ||
falsehoods | |||
logical operators | && || ! | ||
relational operators | == != < > <= >= | ||
conditional expression | x > 0 ? x : -x | (if (> x 0) x (- x)) | |
arithmetic operators | + - * / % | ||
float division of integers | n * 1.0 / m | ||
strings | |||
vimscript | emacs lisp | ||
literal | "don't say \"no\"" 'don''t say "no"' |
||
escapes | in double quote: \n |
||
concatenate | "Hello, " . "World!" | ||
length | strlen("lorem") | ||
lists | |||
vimscript | emacs lisp | ||
dictionaries | |||
vimscript | emacs lisp | ||
functions | |||
vimscript | emacs lisp | ||
define function | function Add(x, y) return a:x + a:y endfunction |
(defun add (x y) (+ x y)) | |
invoke function | echo Add(1, 2) outside of expression: call Add(1, 2) |
(add 1 2) | |
list udfs | function | ||
delete function | delfunction Add | ||
execution control | |||
vimscript | emacs lisp | ||
if | if i > 0 echo "i is positive" endif |
||
while loop | while i < 10 echo "i is" i let i += 1 endwhile |
||
for loop | for i in range(0, 9) echo "i is" i let i += 1 endfor |
||
libraries and namespaces | |||
vimscript | emacs lisp | ||
show load path | :set runtimepath? | ||
append to load path | |||
load library | runtime foo.vim |
Vimscript
Emacs Lisp
Documentation
Vim documentation
GNU Emacs Manual
nano Command Manual
Terminology
key binding
A key binding is a mapping from a keystroke or a sequence of keystrokes to an editor command. When the keybinding is in effect, pressing the keystroke(s) invokes the command.
A keystroke is an operating system event generated when the user presses a key on the keyboard, possibly in combination with a modifier key such as shift, control, or meta.
Sometimes there is no key for meta. Emacs will interpret an ESC-followed-by-keystroke sequence as the meta modified version of the keystroke. In the past there were keyboards that had a key labelled Meta, such as the keyboard used on Symbolics lisp machines. On a PC keyboard one will usually use the Alt key as the Meta key. On a Mac keyboard one will use the Option key. It may be necessary to specify this mapping. In the Preferences of the Mac Terminal App, there is a checkbox under Settings and Keyboard.
In this document the notation C-x and M-x are used to denote control and meta modified keystrokes. This is the notation used in Emacs documentation. The nano documentation uses ^X and M-X. The Vim documentation uses CTRL-X and nothing for the meta modified keystrokes which are not used by Vim.
On PC and Mac keyboards the control key is in an awkward position. If one is willing to give up CAPS LOCK one can map that key to Control. On Windows make this edit to the registry:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00
If the entry does not exist create it with REG_BINARY. A reboot is necessary.
On a Mac the remapping can be performed by going to System Preferences | Keyboard | Modifier Keys …
In Emacs, most control and meta modified keys are mapped to commands, but a few are prefixes. Prefixes permit mapping commands to multiple keystroke sequences. The normal prefixes are C-c, C-h, C-x, M-g, M-o, and M-s.
mode
A set of key bindings. …and also variables which control how text in the buffer is displayed.
In Emacs the mode is chosen automatically for each buffer when a file is opened for editing or viewing. There is usually no reason to change the mode unless Emacs chose the mode incorrectly.
In Vim the user switches between modes frequently during the course of editing a file. The most commonly used modes are normal mode, insert mode, visual mode, and cmdline mode.
buffer
A buffer is a sequence of characters which editors use to read file contents into memory. A change to a buffer is immediately rendered on screen if the portion of the buffer that was changed is currently visible.
Some buffers do not contain the contents of a file. Buffers can be used to display directory contents, help screens, error messages, or shell output.
In Emacs if a buffer is not associated with a file, it is given a name containing asterisks, e.g. *scratch*.
Emacs buffers which contain file contents take their name from the basename of the file. When there are buffer name collisions Emacs appends <1>, <2>, etc. to the ends of the buffer names. The uniqify library can change this behavior to use part of the full pathname instead.
window
The portion of a screen used to display a buffer. In Emacs the mode line and the fringe are considered part of the window.
An Emacs window is not synonymous with the windows provided by the GUI that Emacs is running in. Emacs calls GUI windows frames.
frame
When the editor is running as a GUI application, the frame is the entire canvas provided by the GUI window. When running as a text-mode application in a terminal, the frame is the entire display of the terminal.
Vim documentation calls the frame the screen.
mode line
A line at the bottom of a window with information about the buffer. In Emacs it has the buffer and major mode and minor modes currently in effect.
fringe
The left and right edge of the window.
In Emacs the fringe is used to indicate lines that were wrapped or truncated.
visible portion of the buffer
The portion of the buffer that is currently displayed in a window. Sometimes we will simply call this portion of the buffer the window.
echo area/minibuffer
An area at the bottom of the frame which Emacs used to display messages and accept arguments. When it is displaying a message it is called the echo area. When it is accepting an argument it is called the minibuffer. The minibuffer is used to enter a buffer name, a file name, a search term, the full name of an emacs command, a lisp expression, or a shell command.
Vim reserves the bottom of the frame for a similar purpose. When a : is typed, one enters command line mode and the line is called the command line. Commands which require an argument such as a file name usually must be entered on the command line. When not in command line mode Vim will sometimes messages on what the documentation calls the "bottom of the screen". Some of these messages are equivalent to information that Emacs would display in the mode line, not echo area.
point (cursor)
The place in the buffer where characters will be inserted or commands will have effect.
mark (the other end of the selection)
When of the buffer is being selecting, it is defined by the current location of the point and an entity called the mark which was the location of the point when the selection process was started.
pane
frame
A GUI window owned by the editor when the editor is being run as a GUI application.
copy/paste/yank?
registers/kill ring
face
Line Editors
vi | ex | sed | ed | |
---|---|---|---|---|
insert text after current line. Use a line with single period . to exit input mode. | o | a or .a | a or .a | |
delete first line | :1d | 1d | sed 1d | 1d |
delete first 10 lines | :1,10d | 1,10d | sed 1,10d | 1,10d |
delete all lines after 10th | :11,$d | 11,$d | sed '11,$d' | 11,$d |
delete current line | :d or :.d | d or .d | d or .d | |
delete current and all later lines | :.,$d | .,$d | .,$d | |
switch to editing file, but warn if there are unsaved changes | :e file | e file | e file | |
switch to editing file unconditionally | :e! file | e! file | E file | |
insert text at front of current line. Use a line with a single period . to exit input mode. | O | i or .i | i or .i | |
append line 2 to the end of line 1 | :1,2j | 1,2j | 1,2j | |
append following line to end of current line | J | j | j | |
print current line number and its contents | n or .n | |||
print line numbers for lines 1 thru 10 and their contents. Also move to line 10 | 1,10n | |||
display line numbers with lines | :set nu | set nu | ||
don't display line numbers with lines | :set nu! | set nu! | ||
print first line and move to it | 1p | sed -n 1p | 1p | |
print line 10 and move to it | 10p | sed -n 10p | 10p | |
print lines 1,10 and move to line 10 | 1,10p | sed -n 1,10p | 1,10p | |
quit, but warn if there are unsaved changed | :q | q | q | |
quit unconditionally | :q! | q! | Q | |
insert contents of file after line 10 | :10r file | 10r file | 10r file | |
replace first occurrence of foo with bar on first line | :1s/foo/bar/ | 1s/foo/bar/ | sed 1s/foo/bar/ | 1s/foo/bar/ |
replace all non-overlapping occurrences of foo with bar on first line | :1s/foo/bar/g | 1s/foo/bar/g | sed 1s/foo/bar/g | 1s/foo/bar/g |
replace foo with bar on first ten lines | :1,10s/foo/bar/ | 1,10s/foo/bar/ | sed 1,10s/foo/bar/ | 1,10s/foo/bar/ |
replace foo with bar on current line | :s/foo/bar/ | s/foo/bar/ | s/foo/bar/ | |
insert a copy lines 1 thru 10 after line 20 | :1,10t20 | 1,10t20 | 1,10t20 | |
undo; in ed 2nd invocation undoes the first; vim and ex keep history | u | u | u | |
write all lines to file | :w file | w file | w file | |
write lines 1 thru 10 to file | :1,10w file | 1,10w file | 1,10w file |
Vim has a built-in line editor called ex. Editing in ex mode is like running commands in cmdline mode with the colon :. The only difference is that in ex mode one does not return to normal mode after a command finishes.
One can switch to the line editor with the command Q. Once in the line editor the line editing commands can be issued without the preceding colon. If vim is launched using the name ex then it goes immediately into the line editor. The command vi switches back to the visual editor.
A line editor such as ex or ed is the only way to edit a file when working in a terminal which does not support redrawing the screen.
ed is the only way to edit files on ancient versions of Unix such as Version 6. ed may be the only editor installed in a resource constrained environment: the Ubuntu vim executable is 1.8M and the ed executable is 48k.
The stream editor sed can be used to apply line editing commands to files at the command line.
Vim Bindings in Other Applications
Shells | Pagers | Multiplexer Copy Mode | |||
---|---|---|---|---|---|
bash | zsh | less | screen | tmux | |
page down | C-f | ||||
page half down | C-d | ||||
page up | C-b |
This command gives Bash Vim-style bindings when editing a command:
set -o vi
This command gives Zsh Vim-style bindings:
bindkey -v
Emacs Bindings in Other Applications
Shells | Pagers | Multiplexer Copy Mode | Browser Textareas | IDEs | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Bash | Zsh | less | screen | tmux | Chrome (Mac) | Firefox (Mac) | Safari (Mac) | Xcode | Visual Studio | Eclipse (Mac) | |
move to beginning of line | C-a | C-a | C-a | C-a | C-a | C-a | C-a | C-a | |||
move forward char | C-b | C-b | C-b | C-b | C-b | C-b | C-b | C-b | |||
delete forward char | C-d | C-d | C-d | C-d | C-d | ||||||
move to end of line | C-e | C-e | C-e | C-e | C-e | C-e | C-e | C-e | |||
move backward char | C-f | C-f | C-f | C-f | C-f | C-f | C-f | C-f | |||
delete backward char | C-h | C-h | C-h | ||||||||
insert tab | C-i | C-i | |||||||||
delete to end of line | C-k | C-k | C-k | C-k | C-k | C-k | C-k | C-k | |||
move to next line | C-n | C-n | C-n | C-n | C-n | C-n | C-n | ||||
open line | C-o | C-o | C-o | C-o | |||||||
move to previous line | C-p | C-p | C-p | C-p | C-p | C-p | C-p | ||||
reverse search of command history | C-r | ||||||||||
submit form | C-s | C-s | |||||||||
transpose characters | C-t | C-t | C-t | C-t | C-t | C-t | |||||
page down | C-v | C-v | C-v | C-v | C-v | C-v | |||||
page up | M-v | M-v | M-v | ||||||||
yank | C-y | C-y | C-y | C-y | C-y | C-y | C-y | ||||
move backward word | M-b | M-b | M-b | M-b | |||||||
move forward word | M-f | M-f | M-f | M-f | |||||||
delete backward char | DEL | DEL | DEL | DEL | |||||||
delete backward word | M-DEL | M-DEL | M-DEL | M-DEL | |||||||
delete forward word | M-d | M-d | M-d | M-d |
To get Emacs style bindings in Visual Studio, one must go to the Tools | Options… | Environment | Keyboard section and set the additional keyboard mapping scheme to Emacs.
To get Emacs style bindings in Eclipse, one must go to Preferences | General | Keys and set the Scheme to Emacs.