Académique Documents
Professionnel Documents
Culture Documents
The GNU Emacs Manual calls Emacs the extensible, customizable, self-documenting realtime display editor, but this description tells beginners little about what Emacs is capable of. To give you an idea, here is a sampling of the things you can do with Emacs: eyond !ust being able to edit plain te"t files, Emacs includes special features to help you write in many different human languages and programming#mar$up languages:
(as well as tools for compiling, running, and testing programs. Emacs integrates with G) to provide an *)E %M-x gdb':
Emacs can compare two files and highlight their differences %M-x ediff':
Emacs can read news, mail, and -.. feeds %M-x gnus':
/ou might see now why some people consider Emacs to be not merely a te"t editor but almost a complete operating system. .ome users find that they can do almost all of their wor$ from within Emacs.
Why Emacs?
Emacs helps you be productive by providing an integrated environment for many different $inds of tas$s:
0ll of the basic editing commands %and there are lots of them' are available no matter what you1re trying to do: write code, read a manual, use a shell, or compose an email. 0ll the tools Emacs provides for opening, saving, searching, and processing te"t %and more' are available to you no matter what you1re doing.
This uniformity means that wor$ing within Emacs is often easier than learning to use a separate program, especially when that program is liable to have its own set of editing capabilities and shortcuts.
*f Emacs doesn1t wor$ the way you1d li$e, you can use the Emacs 3isp %Elisp' language to customi4e Emacs, automate common tas$s, or add new features. Elisp is very easy to get started with and yet remar$ably powerful: you can use it to alter and e"tend almost any feature of Emacs. /ou can ma$e Emacs whatever you want it to be by writing Elisp code5 one testament to this is the fact that all of the features pictured above %and many more described later in this tour' are written in Elisp. Emacs is also portable. /ou can use the same editor %with the same configuration' on many platforms, including GNU#3inu", .) and other Uni" derivatives, and some proprietary operating systems such as Microsoft 6indows.
.tarting and e"iting Emacs asic te"t movement and editing commands 8pening and saving files Emacs concepts: windows, frames, files, and buffers *nvo$ing commands with $eybindings and with M-x
To run the tutorial, start Emacs and type C-h t, that is, Ctrl-h followed by t. 0ll the features described in this tour wor$ in GNU Emacs ,2. .ome features described weren1t included in previous versions of Emacs but can be installed separately. 8ccasionally *1ll say something li$e this: .ee (info "(emacs)Using Region") for more information. This refers to a page in the Emacs manual which you can read by following the lin$. 9owever, you can also read such pages directly in Emacs by using the built7in documentation reader, called *nfo. To do this, press M-:, then type (info "(emacs)Using Region") followed by RET:
The Emacs manual is an e"cellent resource for learning about Emacs5 you can read it from within Emacs by typing C-h r. /ou can also read the manual on the web. Emacs has numerous other help features, some of which will be described later. /ou can view a list of all help features by typing C-h C-h. :
*n this mode, changing the filenames in the right7hand column and then typing C-x C-s %;save;' renames the indicated files. -enaming files is as easy as editing te"t. atch renaming %e.g., renaming every file of the form icon!" to bac#u$!icon!"' is as easy as performing a search and replace %M-x quer%-re$lace or M-&'5 there1s no need to learn to use a separate batch renaming tool. <rom within Emacs, you can interact with not !ust files and directories, but system utilities, compilers, debuggers, personal information managers, the web, email, chat, various other internet services, and more, and there1s an ecosystem of third7party tools to ma$e even more functionality accessible. ecause of this integration, the benefits of learning to use any of Emacs1 editing features %say, search and replace' are multiplied many times over.
C-b C-$
C-e M-b
6hen you get used to these $eys, they1re faster than their more familiar e=uivalents in other applications %9ome, End, &trl@3eft, etc.' because you don1t have to move your hands from the touch typing position. 0nd these $eys are far faster than using a mouse to move around in a buffer. Emacs often provides additional commands for moving around in conte"t7specific ways %e.g. in source code, commands to move to the previous or ne"t function definition'. Many of the above commands move to a location relative to your current position in the buffer, so you can use them repeatedly %e.g. C-$ C-$ C-$ to move bac$ three lines'. /ou can use the prefix argument to save time: C-u followed by a number and a movement command repeats that command the specified number of times. /ou can also use M-*digit+ instead of C-u *digit+. *f you use C-u without specifying a digit, the default is :. 8mitting the digit can save time when you don1t $now precisely how many units you want to !ump anyway.
C-u , C-$ C-u -. C-f M-- M-. C-f C-u C-n C-u C-u C-n C-u C-u C-u C-n
ac$ 2 lines <orward +A characters <orward +A characters <orward : lines <orward +B lines <orward B: lines
.earching for te"t is a handy way to move around in a buffer. Thin$ of search as !ust another facility for movement. 6hen you1re loo$ing for something specific, you can use incremental search to ta$e you right there instead of scanning by lines or pages. More about search later.
C-s C-r
Mar!
Emacs remembers something called the mark, which is a previous cursor position. /ou can set mar$ to indicate a particular place in your buffer so you can return to it easily. C-x C-x at a later time will return point to mar$. 0ctually, that command also moves mar$ to where point formerly was5 therefore, a second C-x C-x returns point to its original position.
/ou can set mar$ e"plicitly, but certain commands set mar$ for you, providing you with convenient footholds as you move around your buffer: 6hen you( Type C-/0C Cump to either end of the buffer %M-( or M-)' E"it incremental search /an$ te"t *nsert a buffer or file mar$ is set to( your current location your previous location where you began searching the beginning of the yan$ed region the beginning of the inserted te"t
0s you can see, Emacs tries to be helpful: many commands that have the potential to ta$e you long distances set mar$ so that a simple C-x C-x ta$es you bac$ to where you were. Emacs makes it difficult to lose your place in a buffer: even if you ta$e a detour, you don1t need to scroll around to get bac$ to where you were. Emacs saves many previous values of the mar$ for you. /ou can cycle through the mark ring, which contains the last +B mar$s you1ve set in the current buffer:
C-u C-/0C
"egion
Mar$ serves another purpose: mar$ and point together delineate the region. Many commands operate only on the te"t in the region %i.e. between mar$ and point'. /ou can set the region e"plicitly by setting mar$ %C-/0C' and then moving point elsewhere, or by clic$ing and dragging with the mouse. Emacs provides some commands which set the region for you by moving point and mar$ appropriately, for e"ample:
C-x h M-h
Ma$e region contain the entire buffer %;.elect all;' Ma$e region contain the current paragraph
8ther commands helpfully set the region as part of what they do. C-% %yan$', inserting a file, and inserting a buffer all set the region to surround the inserted te"t. Narrowing restricts the view %and editing' of a buffer to a certain region. This is handy when you1re only wor$ing with a small part of a buffer %e.g. a chapter in a boo$'. Then commands li$e incremental search, or beginning-of-buffer or end-of-buffer don1t lead you out of the region of interest, and commands li$e search and replacement don1t affect the entire file.
C-x n n C-x n
0s with te"t movement, Emacs provides commands for deleting te"t in various amounts. $ills the portion of the current line after point %or deletes the newline following point if point is at the end of line'. The prefi" argument for C-# can be used to $ill multiple lines:
C-# C-# C-u -. C-#
The following commands operate on the region, and are the closest analogs to ;cut; and ;copy; in Emacs:
CM-
Eill region %;cut;' .ave region to $ill ring without deleting %;copy;'
0ll of the above commands kill the te"t being deleted, which means that Emacs removes the te"t and s=uirrels it away for later retrieval %;yan$ing;'. Most commands which delete significant amounts of te"t $ill it rather than simply removing it, so that you can use those commands either to ;delete; te"t or to ;cut; it for later use.
-ecall that most commands which delete a large amount of te"t in fact $ill it %i.e. place it in the $ill ring' so you can restore it later. Emacs makes it very difficult to lose a lot of text permanently: in editors with only a single clipboard, one can easily accidentally delete a large chun$ of te"t or clobber the contents of the clipboard %by cutting two items in succession'. ut in Emacs, in either of those cases, the lost te"t can easily be retrieved from the $ill ring.
(ndo
Emacs1 undo facility wor$s slightly differently from that of other editors. *n most editors, if you undo some changes, then ma$e some new changes, the states formerly accessible with ;redo; can no longer be recoveredF .o when using ;undo; and ;redo; e"tensively, one has to be very careful to avoid accidentally clobbering the redo list.
Emacs uses a different undo model which does not have this deficiency. 0fter any consecutive se=uence of undos, Emacs ma$es all your previous actions undoable, including the undos. %This will happen whenever a se=uence of undos is bro$en by any other command.' *f this sounds complicated, !ust remember that ;undo; is always capable of getting you bac$ to any previous state your buffer was in %unless Emacs has run out of memory to store the undo history'. The principle here is that Emacs makes it very difficult to accidentally lose your work. Undo is available via three different $eys: Undo Undo C-x u Undo
C-2 C-!
.o if you need to get bac$ to a previous buffer state, simply move the cursor %so as to brea$ any e"isting se=uence of undos', and press C-2 until you find what you want. To learn more about undo, see (info "(emacs)Undo").
)ncremental search
C-s
*ncremental search
Typing C-s followed by some te"t starts incremental search. Emacs !umps to the ne"t occurrence of whatever you have typed, as you are typing it %you may have seen similar behavior in Mo4illa <irefo" or other web browsers', and all matches visible on your screen are highlighted.
6ithin incremental search, you can type C-s again at any time to !ump to the ne"t occurrence. 6hen you1ve found what you1re loo$ing for, you can either type RET %or use almost any movement command' to e"it search at the occurrence you1ve found, or C-g %;cancel;' to return to where your search started. *f you e"it search at the found occurrence, you can easily !ump bac$ to where you started with C-x C-x since incremental search sets mar$ appropriately. These commands help you to issue previously issued =ueries:
C-s C-s
>revious item in search history Ne"t item in search history C-h # C-s Guide to more commands available in incremental search
C-s M-$ C-s M-n
/ou can perform a bac$ward incremental search with C-r. %0ll the above commands can be activated similarly from within bac$ward search.' 0t any time during a forward %or bac$ward' search, you can type C-r %C-s' to switch to a bac$ward %forward' search.
C-r
Iuery replace
The =uery replace command prompts you for a search string and a replacement. Then, for each match in the buffer, you can choose whether or not to replace the search string. 9ere are some of the options available at each prompt:
Type % to replace the current match. Type n to s$ip to the ne"t match without replacing. Type q to e"it without doing any more replacements. Type 4 to replace this match, then e"it. Type 5 to replace all remaining matches with no more =uestions.
.ee (info "(emacs)6uer% Re$lace") for more information about these %and other' options. /ou can also type 7 anytime inside a search7and7replace operation to see a guide.
-egular e"pressions are a succinct way of searching for many different strings at once by using a special language to describe the form of what you1re loo$ing for. -egular e"pression synta" is beyond the scope of this tour5 see (info "(emacs)Regex$s") for more information. *f you1re new to rege"ps, or you are constructing a particularly complicated rege"p, you can use the rege"p builder %M-x re-builder'. This command pops up a separate window in which you can test out your rege"p, and any matches in your original buffer will get highlighted as you edit your rege"p. *nstead of !umping through matches one by one, you can also choose to display them all at once. M-x occur prompts you for a regular e"pression, then displays in a separate buffer a list of all lines in the current buffer which match that rege"p %as well as their line numbers'. &lic$ing on any occurrence ta$es you to that line in the buffer. +A
the original found te"t 8-, 8:, etc. the +st, ,nd, etc. parenthesi4ed subgroup in the found te"t 8; the number of replacements done so far 87 a string obtained by prompting the user on each match 8<(lis$-ex$ression =) the result of evaluating an arbitrary function 9ere1s an e"ample. .uppose we have a buffer containing names li$e this:
>eorge ?ashington @ohn Adams Thomas @efferson @ames Madison @ames Monroe
*f we run M-x re$lace-regex$ and replace the rege"p 8(8 B8) 8(8 B8) with 8< (u$case 8:)< 8-, our buffer now loo$s li$e this:
?A/C31>TD1< >eorge AEAM/< @ohn @EFFER/D1< Thomas MAE3/D1< @ames MD1RDE< @ames
0s you can see, rege"p replacement is capable of doing some pretty sophisticated transformations. %-oughly, the search e"pression searches for two words5 the replacement string inserts an uppercased version of the second word, followed by a comma, followed by the first word.'
++
<or e"ample, this se=uence of $eys does the e"act same transformation that we did with regular e"pression replacement earlier, that is, it transforms a line containing >eorge ?ashington to ?A/C31>TD1< >eorge:
M-d C-d M-u < */0C+ C-% C-n C-a
0fter we record that $ey se=uence as a macro, we can type M-. FG to transform the buffer pictured earlier5 in this case, Emacs runs the macro repeatedly until it has reached the end of the buffer. .ee (info "(emacs)Ie%board Macros") for more information.
.hows documentation for the command associated with any particular $ey.
C-h f
.hows documentation for any particular command, by name %i.e. what you would type after M-x'. <or e"ample, C-h # C-s and C-h f isearch-for ard RET both display a page describing incremental search:
This is handy, for e"ample, if you don1t remember what C-s does, or if you remember that it invo$es incremental search but want to $now more about that feature. The documentation gives the full name of the command, shows which %if any' $eys are bound to it, and gives a complete description of what the command does. 8n the other hand, if you don1t remember how to invo$e a particular feature, you can use apropos to search for it:
C-h a
+,
<or e"ample, if * remember that * want to activate narrowing, but don1t remember how, * can type C-h a narro RET which shows a brief list of commands having to do with narrow, one of which is M-x narro -to-region.
.tarts a shell in the buffer named "shell", switching to it if it already e"ists. Use C-u M-x shell to use a buffer with a different name.
M-x com$ile
*nvo$es ma#e %with targets and options of your choice' and displays output in a new buffer. Emacs identifies error lines containing filenames and line numbers, and you can clic$ on them to !ump directly to the corresponding buffer and line.
M-x gdb
*nvo$es gdb in a new buffer. /ou can use the gdb command line as usual in that buffer. 9owever, Emacs lets you set brea$points directly from your source buffers and shows e"ecution by mar$ing the active line in your source buffers. Emacs can also display brea$points, the stac$, and locals, simultaneously and each in their own window.
+2
M-x gre$
*nvo$es gre$ and prints results in a new buffer. 3i$e M-x com$ile, when you clic$ on a match Emacs opens the correct file and ta$es you to the matching line.
M-x man
9ere are some more assorted tools: 0 simple calculator. 0 calendar. M-x $hases-of-moon .hows upcoming =uarters of the moon.
M-x calculator M-x calendar
+:
M-x shell-command
or M-5
or M-J
E"ecutes a command, piping in the current region as input5 displays the output in a new buffer. <or either command, a C-u prefi" will insert the output in your current buffer rather than using a temporary buffer.
,ersion control
Emacs helps you manipulate and edit files stored in version control. Emacs supports &J., .ubversion, b4r, git, hg, and other systems, but it offers a uniform interface, called J&, regardless of the version control system you are using. Emacs automatically detects when a file you1re editing is under version control, and displays something li$e this in the mode line: CK/--4-G to indicate the version control system in use, and the current version. or C-x ' ' commits the current file %prompting you for a log message' if you1ve modified it. %Under version control systems that re=uire loc$ing, this command also ac=uires a loc$ for you.'
M-x 'c-next-action
)isplays a diff showing the changes you1ve made to the current file.
C-x ' M
>rompts you for a version number and shows you that version of the current file in another window.
C-x ' g
)isplays an annotated version of the file showing, for each line, the commit where that line was last changed and by whom. 8n any line you can press N to view the log message for that commit or E to view the associated diff.
C-x ' l
)isplays a log of previous changes to the file. 6hen the cursor is on a particular log entry, you can press d to view the diff associated with that change or f to view that version of the file. .ee (info "(emacs)Kersion Control") for more information.
+?
.ome special features are enabled when you are loo$ing at a diff %whether from a file, or one produced by J&'. Emacs lets you move around in a diff by hun$s or files, apply individual hun$s to a file, reverse a diff, and do other operations useful for reading or manually editing diffs. .ee (info "(emacs)Eiff Mode") for more information.
Emacs server
.ome people li$e to $eep only a single instance of Emacs open and edit all their files in there. )oing this has a few advantages:
/ou can $ill#yan$ te"t between buffers in the same instance of Emacs. Emacs remembers argument histories %what commands you1ve used, what files you1ve opened, terms you1ve searched for, etc.', but only within each instance. *f you have many customi4ations, starting new instances of Emacs is slow.
0las, when you type emacs in a shell to edit a file %or when PEE3TDR is invo$ed by an e"ternal program', a new instance of Emacs is started. /ou can avoid this by using emacsclient, which instead opens a new frame connected to an e"isting instance of Emacs: +. *n your e"isting instance of Emacs, type M-x ser'er-start. 8r add (ser'erstart) to your 4emacs file to ma$e it do that automatically at startup. ,. To edit a file, type emacsclient -t F3NE1AME at a prompt. /ou can also change your PEE3TDR to emacsclient -t if you1re using programs that automatically invo$e PEE3TDR. %emacsclient -t opens a new frame on the terminal5 alternatively, emacsclient -c opens a new K frame.' 2. 6hen you1re done editing, type C-x C-c, which closes the frame. <or more information, see (info "(emacs)Emacs /er'er").
+B
6e1ve also seen a prefi" argument used to modify the following command %the numeric argument, if provided, is ignored': &reate or switch to shell buffer named "shell" C-u M-x shell &reate or switch to shell buffer with specified name
M-x shell
*f you ever get confused, the documentation for any command %accessible with C-h f or C-h #' describes the effect of the prefi" argument, if any. .ee (info "(emacs)Arguments") for more information.
Ma/or modes
Every buffer has an associated major mode, which alters certain behaviors, $ey bindings, and te"t display in that buffer. The idea is to customi4e the appearance and features available based on the contents of the buffer. Emacs ships with do4ens of ma!or modes for editing widely used programming languages, mar$up languages, and configuration file formats. These ma!or modes tell Emacs how to:
*ndent your code correctly %usually, simply pressing TAQ once will ma$e Emacs indent the current line correctly'. )o synta" highlighting *dentify the boundaries of functions *nvo$e interpreters, compilers, or debuggers for your code.
.ome commands we1ve seen, li$e M-x dired, M-x com$ile, and M-x shell, in fact use their own special ma!or modes to ma$e their buffers provide certain features %such as highlighting compile errors and ma$ing them clic$able'. The current ma!or mode is displayed in the mode line. The last thing on the mode line should be one or more items in parentheses, li$e (0%thon 1arro ). The first of these is the name of the ma!or mode. /ou can switch modes in an e"isting buffer by using M-x and the name of the mode:
+D
Mode for editing Cava files Mode for editing >ython files M-x text-mode Mode for editing te"t files M-x fundamental-mode Mode with no speciali4ations at all
M-x Ra'a-mode M-x $%thon-mode
Emacs is very good at determining the right mode to use when you open a file, so you1ll rarely have to use the above commands. These are e"amples of the commands provided by language ma!or modes: 3anguage 3isp >ython 9TM3 .ome special commands available Manipulate s7e"ps in various ways5 e"ecute e"pressions *ndent, unindent bloc$s5 run code in >ython shell *nsert and close tags5 preview in browser
*n almost all cases, ma!or modes for unsupported formats are available as e"tension pac$ages. /ou can find many of them on Emacs6i$i. .ee (info "(emacs)MaRor Modes") for more information.
Minor modes
Every buffer can also have any number of minor modes, which are e"tra pieces of functionality you can enable, independently of each other and of the ma!or mode. Minor modes are listed in the mode line after the ma!or mode inside the parentheses. 9ere are a few commonly used ones:
M-x auto-fill-mode
6raps your lines automatically when they get longer than DA characters.
M-x fl%s$ell-mode
*f you have a buffer displayed in two windows side by side, follo -mode forces them to scroll together such that the te"t displayed in the second window comes right after the te"t in the first window, and moving your cursor off the bottom of the left window causes it to appear at the top of the right window:
+G
.ome minor modes are global, i.e. they affect the behavior of the entire editor, rather than !ust that of a specific buffer. *n the M7" prompt %and elsewhere', show completions as you type M-x is itchb-mode .how all buffer names when you switch buffers with C-x b
M-x icom$lete-mode
.ee (info "(emacs)Minor Modes") for more information. *f you need help with a particular mode, C-h m describes the active ma!or and minor modes. The mode description often lists important commands which are useful in that mode, which is helpful when you1re learning to use a new mode.
/ou can use most buffer editing and movement commands. /ou can move around in, $ill te"t from, and yan$ te"t to minibuffers. /ou can browse previous inputs to the current prompt using M-$ and M-n. Tab completion is often available. <or e"ample, the M-x prompt offers tab completion, so you needn1t worry about typing long command names li$e M-x dired-changeto- dired-mode when M-x dired-ch TAQ suffices.
+H
*f you e"ecuted a command and Emacs has modified your buffer, use C-2 to undo that change. *f you pressed a prefi" $ey %e.g. C-x' or you invo$ed a command which is now prompting you for input %e.g. Find file: =', type C-g, repeatedly if necessary, to cancel.
C-g
Migrating to Emacs
Emacs has a number of options for easing the transition from other editing environments.
,A
Emacs resources
The GNU Emacs Manual %C-h r' is the definitive guide if you want to learn more about Emacs. .ome Emacs features have their own, separate manuals %C-h i d'. The GNU Emacs <0I %C-h C-f' answers many =uestions that beginners have about how Emacs wor$s and how to set it up to do particular things. Emacs6i$i contains information about many downloadable Emacs e"tensions, as well as tips for using and customi4ing Emacs. /ou can as$ =uestions on the help7gnu7emacs mailing list.
,+