Text Mode Editors: Vim, Emacs, Nano

text editing: mode | movement | selecting | deleting | files | buffers | minibuffer | ido | history | searching | replacing | sorting | counting | case | indentation | rectangles | spelling | panes | tabs | macros | ascii art | input methods | encodings | help

dev tools: file system | shell | multiple file search | tags | diff | version control | build | debug | lisp interaction

keystrokes: single | meta single | double | triple

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
 
$ emacs file1 file2 … $ nano file1 file2 …
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
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
 
:1 M-< M-x beginning-of-buffer M-\
end of document
 
:$ 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
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 filename C-x C-f M-x find-file
M-x ido-find-file
C-r
open file read only
 
C-x C-r M-x find-file-read-only
show buffer file name
 
M-: buffer-file-name
list buffers :ls C-x C-b M-x list-buffers
M-x buffer-menu
switch buffer C-x b M-x switch-to-buffer
M-x ido-switch-buffer
M-<
M->
diff buffer and file
 
M-x diff-buffer-with-file
save buffer
 
:w C-x C-s M-x save-buffer C-o ENTER
save all buffers
 
C-x s M-x save-some-buffer
revert buffer
 
:e! M-x revert-buffer
write buffer to different file :w filename C-x C-w M-x write-file
M-x ido-write-file
C-o filename
write region to file
 
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
 
M-u M-x upcase-word
lowercase word
 
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
indent line TAB M-x indent-for-tab-command
indent region M-x indent-region
replace tabs in region with spaces M-x untabify
fill paragraph M-q M-x fill-paragraph
fill region M-x fill-region
remove all whitespace on left of region M-x delete-whitespace-rectangle
prepend string to each line of rectangle C-x r t RET str M-x string-insert
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 filename C-x 2 M-x split-window-vertically
horizontal split :vsp filename 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
tabs
vim emacs emacs nano
create empty tab :tabnew
next tab :tabn
previous tab :tabp
close current tab :tabclose
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
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
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
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
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
man page :!man ls M-x man RET ls
file system
vim emacs emacs nano
open in dired :e dirname M-x dired C-r C-t
find results in dired M-x find-name-dired
recursive grep results in dired M-x find-grep-dired
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 C-x b file RET RET C-x C-w file
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 M-x dired-sort-toggle-or-edit
shell
vim emacs emacs nano
open shell buffer :!sh M-x shell
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 %
recall prev cmd in history M-p M-x comint-previous-input
recell next cmd in history M-n M-x comint-next-input
ansi terminal M-x term
send character to emacs when terminal pane is active C-c CHAR
send C-c to terminal C-c C-c
multiple file search
emacs emacs
grep M-x grep
recursive grep M-x find-grep
stop grep M-x kill-grep
tags
vim emacs emacs nano
create tags file M-! RET 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
version control
emacs emacs
add C-x v i M-x vc-register
annotate C-x v g M-x vc-annotate
commit C-x v v M-x vc-next-action
diff buffer to tip C-x v = M-x vc-diff
diff working directory to tip C-x v D M-x vc-root-diff
fetch C-x v I M-x vc-log-incoming
log C-x v l M-x vc-print-log
merge C-x v m M-x vc-merge
pull C-x v + M-x vc-update
push C-x v O M-x vc-log-outgoing
revert file to tip C-x v u M-x vc-revert
switch to branch/tag C-x v r M-x vc-retrieve-tag
tag C-x v s M-x vc-create-tag
build
emacs emacs
compile M-x compile
stop compilation M-x kill-compilation
next error in output M-n
prev error in output M-p
next error in source file M-g n
prev error in source file M-g n
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
start pdb M-x pdb
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 C-c v M-x eval-buffer
lisp REPL M-x ielm
____________________________ _________________________________ _________________________________ __________________________________________________ _________________________________

Single Keystrokes

C-key k3y KEY key
vim emacs nano vim vim vim
C-@ none set-mark-command forward word SP @ `
C-a none move-beginning-of-line beginning of line ! A insert after end of line a insert after
C-b page up backward-char left " B backward word b back word
C-c none custom prefix cursor position # C delete to eol and insert c
C-d page half down delete-char delete right $ end of line D delete to eol d prefix
C-e scroll up move-end-of-line end of line % first/last (), [], {} E end of word e end of word
C-f page down forward-char right & F find prev char f find char
C-g show status keyboard-quit get help ' G goto line g prefix
C-h left help prefix delete left ( prev sentence H top of window h left
C-i indent-for-tab-command tab ) next sentence I insert after line indent i insert before
C-j down newline-and-indent justify paragraph * J join line with next j down
C-k kill-line cut line + K k up
C-l redraw screen recenter-top-bottom , L bottom of window l right
C-m newline newline - M middle of window m
C-n down next-line down . N repeat search reverse n repeat search
C-o normal: pop tag
insert: one normal mode cmd
open-line write buffer / O insert above o insert below
C-p up previous-line up 0 beginning of line P paste above p paste below
C-q xon quoted-insert 1 digit argument Q ex mode q
C-r redo isearch-backward insert file 2 digit argument R r replace char
C-s xoff isearch-forward 3 digit argument S delete line and insert s delete char and insert
C-t tag pop transpose-chars spell check 4 digit argument T find char before prev char t find char before char
C-u up half screen universal-argument paste line 5 digit argument U restore line u undo
C-v normal: visual mode
insert: insert ctrl char
scroll-up-command page down 6 digit argument V v
C-w kill-region search 7 digit argument W forward word w forward word
C-x extension prefix exit 8 digit argument X delete left x delete char
C-y scroll down yank page up 9 digit argument Y y
C-z suspend-frame : Z prefix z
C-[ ESC ; [ {
C-\ toggle-input-method search and replace < \ | beginning of line
C-] abort-recursive-edit = ] }
C-^ none mark > ^ ~ reverse char case
C-_ undo goto line and column ? _ DEL left

Meta Single Keystrokes

C-M-key M-k3y M-KEY M-key
emacs emacs nano emacs nano emacs nano
C-M-@ mark-sexp M-SP just-one-space back word M-@ mark-word M-` tmm-menubar
C-M-a beginning-of-defun M-! shell-command M-a backward-sentence mark
C-M-b backward-sexp M-" none M-b backward-word
C-M-c M-# none M-c capitalize-word
C-M-d down-list M-$ ispell-word M-d kill-word count words, lines, chars
C-M-e end-of-defun M-% query-replace M-e forward-sentence
C-M-f forward-sexp M-& async-shell-command M-f forward-word
C-M-g none M-' abbrev-prefix-mark M-g prefix goto line and column
C-M-h mark-defun M-( insert-parenthesis beginning of paragraph M-h mark-paragraph
C-M-i ispell-complete-word M-) move-past-close-and-reindent end of paragraph M-i tab-to-tab-stop
C-M-j indent-new-comment-line M-* pop-tag-mark M-j indent-new-comment-line justify file
C-M-k kill-sexp M-+ none scroll down M-k kill-sentence
C-M-l reposition-window M-, tags-loop-continue M-l downcase-word
C-M-m none M-- negative-argument scroll up M-m back-to-indentation
C-M-n forward-list M-. find-tag M-n none
C-M-o split-line M-/ dabbrev-expand end of file M-o face prefix
C-m-p backward-list M-0 digit-argument M-p none
C-M-q none M-1 digit-argument M-q fill-paragraph
C-M-r M-2 digit-argument M-r move-to-window-line-top-bottom search and replace
C-M-s isearch-forward-regexp M-3 digit-argument M-s prefix
C-M-t transpose-sexps M-4 digit-argument M-t transpose-words cut to end of file
C-M-u backward-up-list M-5 digit-argument M-u upcase-word
C-M-v scroll-other-window M-6 digit-argument copy line M-v scroll-down-command insert quoted char
C-M-w append-next-kill M-7 digit-argument M-w kill-ring-save repeat search
C-M-x none M-8 digit-argument M-x execute-extended-command
C-M-y none M-9 digit-argument M-y yank-pop
C-M-z none M-: eval-expression M-z zap-to-char
C-M-[ M-ESC M-; comment-dwim M-[ none M-{ backward-paragraph unindent line
C-M-\ indent-region M-< beginning-of-buffer prev buffer M-\ delete-horizontal-space beginning of file M-| shell-command-on-region
C-M-] none M-= count-words-region scroll down M-] none M-} forward-paragraph indent line
C-M-^ none M-> end-of-buffer next buffer M-^ delete-indentation copy line M-~ not-modified
C-M-_ none M-? none M-_ none M-DEL backward-kill-word
______ _____ ____ ______

Double Keystrokes

C-h key emacs C-w key vim C-x k3y emacs C-x C-key emacs C-x key emacs
C-h ‘ none C-x SP none C-x C-@ pop-global-mark C-x ` next-error
C-h a apropos-command C-w a C-x ! none C-x C-a none C-x a prefix
C-h b describe-bindings C-w b bottom right window C-x " none C-x C-b list-buffers C-x b switch-buffer
C-h c describe-key-briefly C-w c C-x # none C-x C-c save-buffers-kill-terminal C-x c none
C-h d apropos-documentation C-w d open definition in window C-x $ set-selective-display C-x C-d list-directory C-x d dired
C-h e view-echo-area-messages C-w e C-x % none C-x C-e eval-last-sexp C-x e kmacro-end-and-call-macro
C-h f describe-function C-w f open file in window C-x & none C-x C-f find-file C-x f set-fill-column
C-h g describe-gnu-project C-w g C-x ’ expand-abbrev C-x C-g none C-x g none
C-h h view-hello-file C-w h left window C-x ( kmacro-start-macro C-x C-h none C-x h mark-whole-buffer
C-h i info C-w i open keyword in window C-x ) kmacro-end-macro C-x C-i indent-region C-x i insert-file
C-h j none C-w j down window C-x * calc-dispatch C-x C-j none C-x j none
C-h k describe-key C-w k up window C-x + balance-windows C-x C-k kmacro-keymap C-x k kill-buffer
C-h l view-lossage C-w l right window C-x , none C-x C-l downcase-region C-x l count-lines-page
C-h m describe-mode C-w m C-x - shrink-window-if-larger-than-buffer C-x C-m prefix C-x m compose-mail
C-h n view-emacs-news C-w n new window C-x . set-fill-prefix C-x C-n set-goal-column C-x n prefix
C-h o none C-w o close other windows C-x / none C-x C-o delete-blank-lines C-x o other-window
C-h p finder-by-keyword C-w p last window C-x 0 delete-window C-x C-p mark-page C-x p none
C-h q help-quit C-w q close window C-x 1 delete-other-windows C-x C-q toggle-read-only C-x q kbd-macro-query
C-h r info-emacs-manual C-w r C-x 2 split-window-below C-x C-r find-file-read-only C-x r prefix
C-h s describe-syntax C-w s C-x 3 spit-window-right C-x C-s save-buffer C-x s save-some-buffers
C-h t help-with-tutorial C-w t C-x 4 prefix C-x C-t transpose-lines C-x t none
C-h u none C-w u C-x 5 prefix C-x C-u upcase-region C-x u undo
C-h v describe-variable C-w v C-x 6 prefix C-x C-v find-alternate-file C-x v prefix
C-h w where-is C-w w C-x 7 none C-x C-w write-file C-x w none
C-h x none C-w x C-x 8 prefix C-x C-x exchange-point-and-mark C-x x none
C-h y none C-w y C-x 9 none C-x C-y none C-x y none
C-h z none C-w z C-x : none C-x C-z suspend-frame C-x z repeat
C-x ; comment-set-column C-x { shrink-window-horizontally
C-x < scroll-left C-x | none
C-x = what-cursor-position C-x } enlarge-window-horizontally
C-x > scroll-left C-x ~ none
C-x ? none C-x DEL backward-kill-sentence

Triple Keystrokes

C-x C-k C-key emacs C-x C-k key emacs C-x RET key emacs C-x 4 key emacs C-x r key emacs
C-x C-k C-a kmacro-add-counter C-x 4 a add-change-log-entry-other-window
C-x C-k C-b none C-x C-k b kmacro-bind-to-key C-x 4 b switch-to-buffer-other-window
C-x C-k C-c kmacro-set-counter C-x RET c universal-coding-system-argument C-x 4 c clone-indirect-buffer-other-window C-x r c clear-rectangle
C-x C-k C-d kmacro-delete-ring-head C-x 4 d dired-other-window C-x r d delete-rectangle
C-x C-k C-e kmacro-edit-macro-repeat C-x C-k e edit-kbd-macro
C-x C-k C-f kmacro-set-format C-x RET f set-buffer-file-coding-system C-x 4 f find-file-other-window
C-x C-k C-g none
C-x C-k C-h none
C-x C-k C-i kmacro-insert-counter
C-x C-k C-j none
C-x C-k C-k kmacro-end-or-call-macro-repeat C-x RET k set-keyboard-coding-system C-x r k kill-rectangle
C-x C-k C-l kmacro-call-ring-2nd-repeat C-x C-k l kmacro-edit-lossage C-x RET l set-language-environment
C-x C-k C-m kmacro-edit-macro C-x 4 m compose-mail-other-window
C-x C-k C-n kmacro-cycle-ring-next C-x C-k n kmacro-name-last-macro
C-x C-k C-o none C-x r o open-rectangle
C-x C-k C-p kmacro-cycle-ring-previous C-x RET p set-buffer-process-coding-system
C-x C-k C-q none C-x C-k q kbd-macro-query
C-x C-k C-r none C-x C-k r apply-macro-to-region-lines C-x RET r revert-buffer-with-coding-system C-x 4 r find-file-read-only-other-window
C-x C-k C-s kmacro-start-macro C-x C-k s kmacro-start-macro
C-x C-k C-t kmacro-swap-ring C-x RET t set-terminal-coding-system C-x r t string-rectangle
C-x C-k C-u none
C-x C-k C-v kmacro-view-macro-repeat
C-x C-k C-w none
C-x C-k C-x prefix C-x RET x set-selection-coding-system
C-x C-k C-y none C-x r y yank-rectangle

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

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:

VIM Mode Transition Diagram

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)

Emacs Key Notation

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

Ido is a library which ships with Emacs. It can be used to modify 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 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 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

Tabs

Macros

ASCII Art

Input Methods and Encodings

insert unicode character

Unicode Character Name Index

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.

File System

Shell

open shell buffer

execute shell command

send region to shell command

send document to shell command

Multiple File 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

Version Control

add

This performs the add command of most version control systems.

git:

Under Git one normally performs add on modified files before each commit. However, when using Git from within Emacs, using an add on files already under version control is unnecessary.

annotate

How to show the file with each line annotated.

The annotation contains the commit that introduced or last modified the line with the committer and the date of the commit. Some version control systems call this command blame.

commit

How to create a commit from modified files in the working directory.

Emacs will open a new buffer to create the commit message. Use C-c C-c to submit the commit or C-x k to cancel.

Build

Debug

Lisp Interaction

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
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

Vim Documentation: usr_41

Emacs Lisp

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.

The meta key is labelled Alt on PC keyboards and Option on Mac keyboards. 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 Meta key, 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.

content of this page licensed under creative commons attribution-sharealike 3.0