Академический Документы
Профессиональный Документы
Культура Документы
1 / 72
of Political Science
2012
Emacs
2 / 72
Outline
3 / 72
Outline
4 / 72
All Platforms (Windows, Mac, Linux, Unix, Atari,...) Across Time: Will Always Exist (because eager fanatics maintain it) Free & Open Source
5 / 72
Emacs has major modes for most languages and types of A les (C, C++, Lisp, R, SAS, Stata, L TEX, English, . . .). Incredibly powerful text management tools
compare documents or buers Regular expression search and replace (even across many les) Copy, paste, insert columns
Enormous power to edit very large les At some point in the future, you may nd that the only editor that is capable for a particular project is Emacs. Prepare for that time by using Emacs for other projects as well!
6 / 72
7 / 72
p r o g r a m m i n g . One can a l s o s e a m l e s s l y i n t e g r a t e w i t h a r e v i s i o n c o n t r o l system , i n o r d e r t o maintain a l o g of changes i n your programs and data , a s w e l l a s t o a l l o w f o r t h e r e t r i e v a l of past v e r s i o n s of the code. I n a d d i t i o n , i t a l l o w s you t o k e e p a r e c o r d o f y o u r s e s s i o n , w h i c h can a l s o be u s e d f o r e r r o r r e c o v e r y through the use of the t r a n s c r i p t mode.
8 / 72
Outline
9 / 72
10 / 72
Terminology
Frame: That whole thing. Window: The content display area inside a frame Buer: A chunk of content, the collection of letters and words that can be shown inside a window. Check for yourself.
Chose File -> Split Window. You can have 2 windows showing same buer Choose File -> New Frame Choose Menu Buers to select a buer to display in the currently focused Window
11 / 72
We want the editor to customize itself to the language we are working with. A display of R code should dier from Lisp or Java Emacs will guess the mode you want from the le extension (if you open a le) Or you can specify the mode while inside the session Type M-x ( meta (usually the Alt key) and the letter x at the same time) and then enter a valid mode. (Hit Alt with x, release both keys. Then type a command and hit return)
12 / 72
13 / 72
Outline
14 / 72
15 / 72
16 / 72
Dont Listen To People Who Want You To Memorize C-x C-g C-h whatever...
You can learn key-stroke combinations later, if you need them. But you dont need most of them now, because Emacs now has menus and buttons. Whats easier to remember?
Click the Edit Menu, Choose select all, or Type C-x h (Control-x, release both keys, then the letter h ) I use the rst way. (Why h for select all?)
17 / 72
Dont Listen To People Who Want You To Memorize C-x C-g C-h whatever... ...
I usually just hit page down for a moment. Yesterday I had R output that was 150,000 lines and M-> would have been useful.
18 / 72
Example of Previous
Here is an example from Experienced Programmers Introduction to Emacs http://weatherall.4all2u.com/work/emacs.htm
Now t o a more c o n v e n t i o n a l l e a r n i n g o r d e r Ca move t o b e g i n n i n g o f l i n e Ce move t o end o f l i n e M< move t o t h e b e g i n n i n g o f t h e f i l e M > move t o t h e end o f t h e t u t o r i a l . Ck delete r e s t of l i n e < Delete > d e l e t e t h e c h a r a c t e r j u s t b e f o r e t h e cursor Cd d e l e t e the next ch arac t er a f t e r the cursor
19 / 72
20 / 72
21 / 72
22 / 72
23 / 72
We dont have to sacrice on any of the other very powerful features of Emacs Emacs enthusiasts are able to remember hundreds of key combinations, but I cant.
24 / 72
Some enthusiasts write Lisp code for a living, but I think Lisp stands for lots of irritating, stupid parentheses.
25 / 72
26 / 72
Potential problem to keep in mind: Emacs has many keystroke combinations that use C and M and anything we do to override them may cause trouble. But most of these bugs have been solved.
27 / 72
28 / 72
That rectangular block can be copied, cut, or pasted. Sometimes, this can be very handy, especially when revising text that includes tables or columns.
29 / 72
Customizations I Recommend
When Emacs starts, it reads a series of conguration les
Some are system-wide Some are user specic
On systems I administer, I generally customize the system-wide settings with my personal favorite settings http://pj.freefaculty.org/Software/Emacs/ 50emacs-ess-ku.el On systems I dont administer, I urge my users to install my conguration settings le. They they can share in my hard-found victory The user settings can be stored in the users home folder, either in
.emacs, or .emacs.d/init.el
30 / 72
; ; If you are using this because you use Emacs to use ESS ( Emacs ; ; Speaks S t a t i s t i c s ) with R , here are my special features . ;; ;; ;; ;; ;; 1. 2. if 3. to I n d e n t a t i o n policy follows P r o g r a m m i n g R E x t e n s i o n s Manual Shift + Enter will send the current line to R , and it will start R it is not running . R will start in the current working directory , without stopping ask the user about a working d i r e c t or y .
31 / 72
; ; Section I . Windows OS w o r k - a r o u n d s ( i f ( eq system type ' windows nt ) ( setq usefiledialog n i l ) ) ; ; There ' s a problem with file s el e c t i o n dialogs on Windows
; ; Section II . Keyboard and mouse c u s t o m i z a t i o n ; ; IIA : make mouse s e l e ct i o n work in the usual Mac / Windows way ; ;( require ' p c - s el e c t ) ( s e t q s h i f t s e l e c t m o d e t ) ; is default in Emacs 23+ , replaces p c - s e l e c t ( transient mark mode t ) ; h ig h l i g h t text s el e c t i o n ( d e l e t e s e l e c t i o n m o d e t ) ; delete seleted text when typing ; ; IIB : keyboard c u s t o m i z a t i o n ( cuamode t ) ; windows style binding C-x , C-v , C-c , C-z , cut paste ( s e t q c u a a u t o t a b i f y r e c t a n g l e s n i l ) ; ; Don ' t tabify after r e c t a n g le commands ( s e t q cua keep region after copy t ) ; ; S e l ec t i o n remains after C-c
32 / 72
; ; Section III . P r o g r a m m i n g c o n v e n i e n c e s : ( show paren mode t ) ; light-up matching parens ( global font lock mode t ) ; turn on syntax h i g h l i g h t i n g ( s e t q text mode hook ( q u o t e ( t u r n o n a u t o f i l l text mode hook identify ) ) )
; ; Section IV . ESS Emacs S t a t i s t i c s ; ;( setq i n f e r i o r - e s s - o w n - f r a m e t ) ( s e t q inferior ess same window n i l ) ; ; create a new frame for each help instance ; ; ( setq e s s - h e l p - o w n - f r a m e t ) ; ; If you want all help buffers to go into one frame do : ( s e t q ess help own frame ' one ) ; ; m i n i b u f f e r tips : will work in future ; ;( require ' e s s - el d o c ) ; ; html help ; ( setq i n f e r i o r - e s s - r - h e l p - c o m m a n d " help (\"% s \" , h e l p _ t y pe =\" html \") \ n ")
33 / 72
))
; ; PJ 2 0 1 2 - 0 3 - 2 1 Follow advice in P r o g r a m m i n g R E x t e n s i o n s : Use ; ; i n d e n t a t i o n in C ++ style ( add hook ' ess mode hook ( lambda ( ) ( e s s s e t s t y l e ' C++ ' q u i e t ) ( add hook ' l o c a l w r i t e f i l e h o o k s ( lambda ( ) ( essnuketrailingwhitespace ) ) ) ) ) ; ;( setq e s s - n u k e - t r a i l i n g - w h i t e s p a c e - p ' ask ) ; ; or even
34 / 72
; ; Section V . C u s t om i z e the use of Frames . Try to make new content ; ; appear in wholly new frames on screen . ;; ; ; V . A : D i s c o u r a g e Emacs from s p l i t t in g " frames " , e n c o u r a ge it to ; ; pop up new frames for new content . see : ; ; http : / / www . gnu . org / software / emacs / elisp / h t m l _n o d e / C h o o s i n g - W i n d o w . html ( s e t q pop up frames t ) ( s e t q specialdisplaypopupframe t ) ( s e t q s p l i t w i n d o w p r e f e r r e d f u n c t i o n n i l ) ; d i s c o u r a g e h o r i z o n t a l splits ( s e t q pop up windows n i l ) ; ; V . B : Use " framepop " to pop up small frame messages . Some users don ' t ; ; have " framepop ". I try to make this check to see if the feature is ; ; i n st a l l e d . ; ; Note that " p o p - u p - f r a m e s " is d if f e r e n t from " framepop ". ; ; Crazy ! The framepop package can " catch " some special small buffers
35 / 72
'
noerror )
; ; Even if you don ' t have framepop , it is OK to leave these . ;; ( s e t q framepop frame parameters ' ( ( name . n i l ) ; use buffer name ( unsplittable . t) ; always include this ( menu bar lines . 0 ) ; no menu bar ( minibuffer . nil ) ; or m i n u b u f f e r ( l e f t . 1) ; top left corner of screen , ( top . 30) ; away from my main frame ( width . 71) ; narrower , so it fits nicely ( b a c k g r o u n d c o l o r . M i s t y R o s e ) ; for October . ( toolbarlines . 0) ( minibuffer ) ) ) ( s e t q framepop min frame size 2 0 ) ( s e t q framepop use advice ( q u o t e a u t o m a t i c ) ) ( s e t q framepopautoresize t ) ; ; Stops framepop from fiddling with a few specific buffer types . ( setq specialdisplaybuffernames
36 / 72
* H e l p * * s h e l l * * C o m p l e t i o n s * * g r e p * * t e x s h e l l * ) )
; ; V . C : Make files opened from the menu bar appear in their own ; ; frames . This o v e r r i d e s the default menu bar settings . Opening an ; ; ex isting file and creating new one in a new frame are the exact ; ; same o p e r a t i o n s . adapted from Emacs menu-bar . el ( defun menufindexisting ( ) E d i t t h e e x i s t i n g f i l e FILENAME . ( interactive ) ( l e t * ( ( mustmatch ( n o t ( and ( f b o u n d p ' x uses old gtk dialog ) ( x uses old gtk dialog ) ) ) ) ( f i l e n a m e ( c a r ( f i n d f i l e r e a d a r g s F i n d f i l e : mustmatch ) ) ) ) ( i f mustmatch ( findfileotherframe filename ) ( findfile filename ) ) ) ) ( d e f i n e k e y menu bar file menu [ n e w f i l e ] ' ( menu item Open/ C r e a t e f i n d f i l e o t h e r f r a m e : e n a b l e ( menu bar non minibuffer window p ) : h e l p C r e a t e a new f i l e ) ) ( d e f i n e k e y menu bar file menu [ o p e n f i l e ] ' ( menu item , ( p u r e c o p y Open F i l e . . . ) m e n u f i n d e x i s t i n g : e n a b l e ( menu bar non minibuffer window p ) : h e l p , ( p u r e c o p y Read an e x i s t i n g f i l e i n t o an Emacs b u f f e r ) ) ) ; ;( d e f i n e - k e y m e n u - b a r - f i l e - m e n u [ o p e n -f i l e ] ; ; ' ( m en u - i t e m " Open File ..." f i n d - f i l e - o t h e r - f r a m e
37 / 72
; ; V . D Open d i re c t o r y list in new frame . ( d e f i n e k e y menu bar file menu [ d i r e d ] ' ( menu item Open D i r e c t o r y . . . d i r e d o t h e r f r a m e : h e l p Read a d i r e c t o r y ; o p e r a t e on i t s f i l e s ( D i r e d ) : e n a b l e ( n o t ( window minibuffer p ( frame selected window menu updating frame )))))
; ; Section VI : M i s c e l l a n e o u s c o n v e n i e n c e ; ; Remove Emacs " splash screen " ; ; http : / / fuhm . l i v e j o u r n a l . com / ( d e f a d v i c e command line normalize file name ( before killstupidstartupscreen activate ) ( setq inhibitstartupscreen t ) ) ( setq inhibitsplashscreen t )
; ; Show file name in title bar ; ; http : / / www . t h e t e c h r e p o . com / m a i n - a r t i c l e s / 549 ( s e t q f r a m e t i t l e f o r m a t %b Emacs )
38 / 72
; ; Make Emacs scroll smoothly with down arrow key . ; ;2011-10-14 ; ; testing faq 5.45 http : / / www . gnu . org / s / emacs / e m a c s- f a q . html # Modifying-pull_002ddown-menus ( s e t q s c r o l l c o n s e r v a t i v e l y mostpositivefixnum )
; ; If you want to adjust the size of the frames , u n c o m me n t this , adjust values ; ;( setq d e f a u l t - f r a m e - a l i s t ' (( width . 90) ( height . 65) ) )
; ; Re member password when c o n n e c t e d to remote sites via Tramp ; ; http : / / s t a c k o v e r f l o w . com / q u e s ti o n s / 840279 / passwords-in-emacs-tramp-mode-editing ; ; Emacs " tramp " service ( ssh c o n n e c t i o n ) c o n s t a n t l y ; ; asks for the log in password without this ( s e t q passwordcacheexpiry n i l )
; ; PJ 2 0 1 1 - 0 5 - 1 5
39 / 72
; ; Section : Emacs shells work better ; ; http : / / snarfed . org / w h y _ i _ r u n _ s h e l l s _ i n s i d e _ e m a c s ( s e t q ansi color for comint mode ' f i l t e r ) ( s e t q comint prompt read only t ) ( s e t q comint scroll to bottom on input t ) ( s e t q comint scroll to bottom on output t ) ( s e t q comint move point for output t )
;; ;; ;; ;; ;; ;; ;; ============================================================= From Marc Schwartz Set keys for ' windmove ' , built into Emacs = = = = = = = = = = = = = = = = = = Ke yboard a c c e l e r a t o r M - d i r e c t i o n will " scroll " through buffers . Easier to remember than other ways . =============================================================
' meta )
( windmovedefaultkeybindings
40 / 72
266 267
( g l o b a l s e t k e y ( kbd M < r i g h t > ) ' windmove right ) ( g l o b a l s e t k e y ( kbd M < l e f t > ) ' w i n d m o v e l e f t )
41 / 72
As far as I can tell, the rst two became defaults in Emacs-23, so Im just making sure they are still set that way.
42 / 72
Shortcuts I do remember
There are some keyboard shortcuts I do remember
C-s: I search frequently, this is easier than the menu Edit -> Search (C-r searches in reverse) M-%: Query replace C-M-%: Regular-Expression Query replace (cant live without them) M-x: execute commands. C-g : get out of jail free. If you mistakenly start something in the minibuer, C-g gets out of it. M-q : re-shapes an ill-formed paragraph M-; : for a selected region, will comment and uncomment all lines.
But for things that I only do once every 6 months, it is easier to nd them in a menu or type them by name.
43 / 72
Using M-x
M-x tells the minibuer to get ready for a command TAB completion works inside there, so if you remember the rst few letters of a command, this is an easy way to do things. Example: indent code in a region
Highlight some text and type: Mx indentregion After that, the indentation occurs, but Emacs minibuer reminds me I could have used a short cut: You can r u n t h e command ' i n d e n t r e g i o n ' w i t h CM\
44 / 72
Example: accidentally open a read only le. Some le saved from R sessions are marked read only automatically. Tedious! I want to edit!. Emacs can try to make it writable.
Run Mx togglereadonly Emacs minibuer pops up a reminder You can r u n t h e command t o g g l e r e a d o n l y w i t h Cx Cq
(thanks very much. Which is more memorable. toggle-read-only or C-x C-q?)
45 / 72
46 / 72
47 / 72
2 things happen.
The dierent parts of the 2 buers are color highlighted A small window pops up in which I can Navigate the dierences.
Type n to step to the next dierence between the buers, or p for previous.
Dont forget to hit q to close the compare setup, and then it makes you type yes in full to escape.
Emacs ESS
48 / 72
Outline
Emacs ESS
49 / 72
1. ESS menu 2. Blue R starter (same as M-x R) 3. Send one line to R 4. Send selection to R 5. Send current function to R.
Emacs ESS
50 / 72
Hit the big blue R button Hopefully, R starts inside a buer, which Emacs refers to in the buer list as *R* If Emacs cant nd R in your system, you need to do some conguration work so that the R/bin folder is added to your system path (I have instructions for that in the crmda computing documentation)
Emacs ESS
51 / 72
The bottom part is the ESS R inferior mode, a terminal in which R is running
Emacs ESS
52 / 72
Emacs ESS
53 / 72
Dont Start Emacs from an applications menu: It wont know where to look for les. Make a directory structure, and run Emacs from within it. (In Linux, just type emacs newFile.R to start). On many OS, the easiest way to do that is to copy an R le into the desired directory, and then open that le with Emacs. Open an R le, Emacs automatically knows to turn on R-mode (ESS Menu & Buttons)
Emacs ESS
54 / 72
I want separate Frames! Spawning more Frames is very un-Emacs-like because it goes against the decades-old tradition of Emacs (during which time one could only have one Frame on one terminal). Much of my Emacs init le is aimed at forcing Emacs to Start Frames with new content, rather than simply starting new buers that over-write windows Im using. This is an area where reasonable people can disagree, Im just telling you what I like. I dont want Emacs to be my window manager, I want the OS to handle that.
Emacs ESS
55 / 72
Sometimes I use M-x R, just to prove I still can. (There was a time when Emacs for Windows had no button bar, so the M-x R was the only way.)
After starting R within Emacs, run getwd() to make sure the working directory is correct. The function evaluator is really handy. While revising a function in code, hit that button and Emacs sends the whole function to *R*.
Emacs ESS
56 / 72
ESS Menu
The ESS menu teaches you the keyboard shortcuts. You can decide if you like them. Eval versus Eval-and-go.
Eval commands send instructions to *R*. Eval-and-go send the instructions and transfer the focus to *R*
Emacs ESS
57 / 72
Roxygen
Roxygen is a framework for generating R documentation les In the olden days (last year), an R package would have separate les for R functions and the help les that went with them. Problem: programmers found it tedious to maintain the separate help les The package roxygen2 (by Hadley Wickham, Peter Danenberg, and Manuel Eugster) addresses that by
creating a language for writing documentation inside R code les providing functions to translate the result into documentation les
Emacs ESS
58 / 72
Roxygen ...
Even if you arent writing a package, the Roxygen style might be a nice way to prepare your documents. Suppose a function is declared like so
myFabFunc < f u n c t i o n ( x , y , z , a ) { x * y * z * a }
Click on the rst line and from the ESS menu, choose Roxygen and Update/generate template. Observe a skeleton is created in which the function can be described
Emacs ESS
59 / 72
Roxygen ...
## ' .. content for \ description {} (no empty lines ) .. ## ' ## ' .. content for \ details {} .. ## ' @title ## ' @param x ## ' @param y ## ' @param z ## ' @param a ## ' @return ## ' @author pauljohn myFabFunc < f u n c t i o n ( x , y , z , a ) { x * y * z * a }
Emacs ESS
60 / 72
Roxygen ...
If we then ll in the sections, we will have pretty good documentation for the function and it will be package-ready (more or less).
Emacs ESS
61 / 72
Emacs ESS
62 / 72
Emacs ESS
63 / 72
Emacs will open the help le and the menu at the top will have an ESS-help menu In the help le, move the cursor to one of the examples at the end of the le. Click the ESS-help menu and choose Eval Line or one of the other options.
Emacs ESS
64 / 72
As long as you have the R code and the transcript le, you have all the records you need for future reference. What if you are working on an R code le, but you type some commands into the *R* buer interactively?
Emacs ESS
65 / 72
Obviously, something I typed interactively in the *R* buer did not get copied into the code le properly. Solution strategy.
Open the *.Rout le in Emacs. Emacs recognizes that this is a transcript le
Emacs ESS
66 / 72
C-x h )
2 In the ESS-trans menu, choose Clean .
Viola! That will erase all the output from that le, and leave behind only the commands that were run.
Along the same lines, from a reviewer of these slides, I received an email about an ESS feature. Since ESS 12.03, there is an R automatic output scrub and paste. The key sequence is Cu Cu Cy.
Emacs ESS
67 / 72
Cu Cu Cy
Note, this requires the traditional C-y ( yank ) to paste, not the CUA C-v.
Emacs Conclusion
68 / 72
Outline
Emacs Conclusion
69 / 72
Emacs in Retrospect
I think the documentation and publicity that goes with Emacs does it great dis-service.
Bill Venables, is credited with the quip: The rst ve years of Emacs are the worst; after that, its just dicult.
When I rst tried Emacs, I looked through the tutorial and I thought this really is trouble. Ive told people that Emacs is like Democracy, adapting the comment that Winston Churchill made to the House of Commons, 1947-11-11, Democracy is the worst form of government, except for all those other forms that have been tried from time to time. Almost all of the really smart people I know use Emacs.
Emacs Conclusion
70 / 72
Of course not. But Ill get more hits in Google than with my original title, Emacs Learning Curve 77% Less Steep Than Previously Believed I honestly believe that if people are willing to try Emacs my way, they can use it comfortably and benet from many powerful features. It is not necessary to memorize a raft of key sequences, even though many Emacs experts do so.
Emacs Conclusion
71 / 72
Emacs is Extensible
If a person is willing to use Emacs with the init le I provide, using Emacs can be a very reasonable experience.
Emacs Conclusion
72 / 72
Useful Websites