Being Productive With Emacs - Massachusetts ?· “Emacs is the extensible, customizable, self­documenting…

Embed Size (px)

Text of Being Productive With Emacs - Massachusetts ?· “Emacs is the extensible, customizable,...

  • BeingProductiveWithEmacsPart1

    PhilSungsipbiapemacs@mit.edu

    http://stuff.mit.edu/iap/emacsSpecialthankstoPiawNaandArthurGleckler

  • Emacsistheextensible,customizable,selfdocumentingrealtimedisplayeditor.

  • ThemanyfacesofEmacs

    Emacseditssourcecode

  • ThemanyfacesofEmacs

    EmacsisahexeditorMxhexlfindfile

  • ThemanyfacesofEmacs

    EmacsdoesdiffsMxediffbuffers

  • ThemanyfacesofEmacs

    EmacsisafilemanagerMxdired

  • ThemanyfacesofEmacs

    EmacsisashellMxshell

  • ThemanyfacesofEmacs

    Emacsisamail/newsclientMxgnus

  • ThemanyfacesofEmacs

    EmacsplaystetrisMxtetris

  • WhyEmacs? Providesanintegratedenvironment

    Sameeditingcommandsavailableeverywhere Largesetoftoolsavailableatalltimes Movetextbetweentaskseasily

  • WhyEmacs? Easytoextend

    Elispforcustomizingoraddingnewfeatures ExtensioncodehasthefullpowerofEmacs Dynamicenvironment:norestartingorrecompiling

    Portable

  • Today'sgoal:gettheflavorofEmacs GettingstartedwithEmacs Editingtips Demosofusefulfeatures CommonEmacsconcepts

    Later... Advancedcustomization ProgrammingandextendingEmacswithElisp

    Examples based onGNU Emacs 22

  • Prerequisites(sortof) Emacsbasicconcepts

    Files,buffers,windows,frames Keyboardcommands

    Keycommands,prefixkeys,Mx,theminibuffer "Cx"meansCtrl+x"Mx"meansMeta+xorAlt+x

    Basictasks Openingandsavingfiles,exitingEmacs

    Takethetutorialtobrushup:Cht

  • It'sallabouttextmanipulation Textinfiles

    grocerylists,HTML,code,... Textoutsideoffiles

    shell,debugger,... Textasametaphor

    dired,gnus,...

  • Textasametaphor:dired

    Aftereditingnamesinthisbuffer,CxCsrenamesthe

    modifiedfiles

    M-x wdired-change-to-wdired-modeafter opening any directory

  • Movingaroundinbuffers Bycharacterorline

    Cp

    Cn

    Cb Cf

  • Movingaroundinbuffers Beginning,endofline

    Ca,Ce Byword

    Mf,Mb Bysentence

    Ma,Me

    Byscreen Cv,Mv

    Beginning,endofbuffer M

    Gotoline# Mgg

  • Movingaroundinbuffers Movemultiplelinesforward,backward

    Example:Cu10Cp(back10lines) Cuprefixgeneralizestoothercommands

    Searchfortext Cs,Cr

    Exchangepoint(cursor)andmark CxCx

  • Killing("cutting")text Killline

    Ck

    Killmanylines Cu10Ck(10lines) CuCk(4lines) CuCuCk(16lines)

  • Killing("cutting")text Killregion

    Cw

    Savewithoutkilling Mw

    Killsentence Mk

    Kill("zap")tonextoccurrenceofcharacter MzCHAR

  • Yanking("pasting")text Yank

    Cy

    Yankearlierkilledtext My(onceormoreafterCy)

    Thekillring Almostallcommandswhichdeletetextsaveitfor

    possiblelaterretrieval

  • Themark

    Whenyou... Cspc

    M Searchfortext Yanktext Insertabuffer

    themarkissetto... whereyouare whereyouwere whereyoustarted startofinsertedtext startofinsertedtext

    Remembersapreviouscursorposition CxCxtoswappoint(cursor)andmark

  • Themark Themarkring

    Movetoapreviousmark:CuCSPC Markandpointarealsousedtodelineate'the

    region' Manycommandsoperateonthetextintheregion Setregionbysettingmark,thenmovingpoint

  • Undo Undopreviousactions

    C/orC_orCxu Undowithincurrentregion

    CuC/

  • Theundomodel,illustrated

    A B C D

  • Theundomodel,illustrated

    A B C D

    These states are accessible with 'undo'

  • Theundomodel,illustrated

    A B C D

    Undosomeofyouractions...

    These states are accessible with 'undo'

    These states are accessible with 'redo'

    Thisishowmosteditorsotherthanemacswork:

  • Theundomodel,illustrated

    A B C D

    Nowdosomethingelse...

    C'

    Thisishowmosteditorsotherthanemacswork:

    These states are accessible with 'undo'

    These states are no longer accessible!

  • Theundomodel,illustrated

    A B C D

    Howemacshandlesthissituation

    C'

    The list of states is 'folded' so that all previous actions,

    including undos, are undoable

  • Incrementalsearch Searchfortext(likeFirefox's"findasyoutype")

    Cstext Csagaintofindnextoccurrence RETtostopatfoundoccurrence Cgtocancelandgobacktostartofsearch

    Crforreversesearch Manyoptionsavailableinsidesearch;ChkCstolearnmore

  • Searchhistory Searchforpreviouslysearchedstring

    CsCs

    Browseandeditpreviousqueries CsthenMp,Mn

  • Incrementalsearch Searchforregularexpressions

    CMsregexp

    Regexpdescribestheformofwhattolookfor SyntaxmaybeslightlydifferentfromotherREsyou

    mayhaveused EmacsREsareasupersetofPerlREs

    Mxrebuildercanhelpyoutestcomplexregexps

  • Searchingandreplacing Searchandreplace,askingforconfirmation

    M%orMxqueryreplace DisplayalllinesmatchingRE

    Mxoccur

  • REsearchandreplacement Mxreplaceregexp

    Replacementtextcandependonfoundtext! Replacementtextgetsthesesubstitutions:

    \&(thematchedstring) \1,\2,etc.(referencestopartsofmatchedstring) \#(numberofmatchedlinessofar) \?(promptuserforwhattoenter) \,(lispexpression...)

  • REreplacementexample

    BillGatesSteveJobsEricSchmidtLarryEllison

    GATES,BillJOBS,SteveSCHMIDT,EricELLISON,Larry

    Mxreplaceregexp\(\w+\)\(\w+\)

    with\,(upcase\2),\1

    More tips at http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html

  • Integrationwithusefultools Shell

    Mxshell

    Compile(invokemake) Mxcompile

    Debug Mxgdb

  • Integrationwithusefultools Grep

    Mxgrep,Mxrgrep Manpagereader

    Mxwoman

    Invokeshellcommands Mxshellcommand,Mxshellcommandonregion

  • ...andthensome Calculator

    Mxcalc

    Calendar Mxcalendar

    Mooncalendar Mxphasesofmoon

  • Morehelpfulfeatures TRAMP:openremotefilesoverSSH

    CxCf/user@host:~/remote/file

    VC:automaticallydealwithCVS,SVN,etc. Mxvcnextactiontocommitmodifiedfile Mxvcdifftoviewchangestocurrentfile

    etags:namesearch/completionforsourcecode

  • Emacsserver UseasingleEmacssessionforallediting Dothisonce:Mxserverstart

    orput(serverstart)inyour.emacsfile Toeditafile:

    prompt%emacsclientfile

    FileopensinexistingEmacsframe Cx#whendoneediting

  • Macros Remembersafixedsequenceofkeysforlater

    repetition Startrecordingmacro:Cx( Stoprecordingmacro:Cx) Replaymacro:Cxe

  • Macroexample

    BillGatesSteveJobsEricSchmidtLarryEllison

    GATES,BillJOBS,SteveSCHMIDT,EricELLISON,Larry

    Definemacro:MdCdMu,[SPC]CyCnCa

    "Remove first word and space, uppercase next word, insert comma and space afterward, reinsert first

    word, move to beginning of next line"

    Runmacrorepeatedly:Cxee...

  • Narrowing Restrictsview/editinginabuffertoacertain

    region CxnnorMxnarrowtoregionto

    narrowtoregion CxnworMxwidentorestore('widen')

  • Registers Storecurrentwindowconfiguration

    CxrwREGISTER

    Restorewindowconfiguration CxrjREGISTER

    Registerscanalsostorepositions,text,numbers,filenames...

    REGISTER may be any letter or number

  • Prefixarguments Sometimesusedtoindicaterepetition

    Cu10Cf(forward10characters) CuCo (make4newlines)

    Sometimesmodifyfollowingcommand C/(undo)vs.CuC/ (undowithinregion) Mxshell vs.CuMxshell

    Acommand'sdocumentation(ChforChk)describestheeffectoftheprefixargument,ifany

  • Majormodes Altersbehavior,keybindings,andtextdisplay Switchmodeinexistingbuffer:

    Mxjavamode Mxpythonmode Mxfundamentalmode

    Or,useanothercommandtocreatebuffer: Mxshell Mxdired

  • Languagemajormodefeatures Languagespecificindentation,syntaxcoloring Languagespecificfeatures:

    Lisp:commandsformanipulatingsexpressions Python:commandsfor(un)indentingblocks HTML:insert/closetags;previewinwebbrowser Modescandefineorredefinekeys

  • Minormodes Extrafunctionalityyoucanturnonoroff

    Anynumberofminormodesmaybeactiveatonce Independentofmajormodefunctionality

    Mxautofillmode Mxflyspellmode Mxfollowmode

  • Globalminormodes Offercompletionsforbuffers,commands,etc.

    Mxicompletemode

    ShowallbuffernamesonCxb: Mxiswitchbmode

  • Minibufferinput CommonfeatureswheneverEmacsprompts

    youtoentersomething Mostbufferediting,movementcommandswork BrowsepreviousinputswithMn,Mp Tabcompletionisoftenavailable

    Mxevalexpression,Mxfindfile,Mxswitchtobuffer,...

  • GettinghelpwithEmacs Helpwithkey

    Chk

    Helpwithfunction Chf

    Helpwithmode Chm

    Showkeybindings Chb

    Helpabouthelp ChCh

  • GettinghelpwithEmacs Apropos(searchforcommand)

    Cha

    Helpwithprefixkey Ch(afterprefixkey)

    Manuals Mxinfo,thenselectemacsorefaq

  • Intheeventofanemergency Cancelcommand

    Cg

    Undo! C/orC_

    WhatdidIjustdo? Mxviewlossage

  • Commonproblems Deletenotdeleting?

    Mxnormaleraseisbackspacemode

    KeyswithMnotworking? UseESCinstead ESCxinsteadofMx ESCCtinsteadofCMt

  • MigratingtoEmacs FromWindowsapplications

    Mxcuamode:recoversCz,Cx,Cc,Cvfortheirusualpurposes

    Fromvi/vim Mxvipermode

  • Resources EmacsonAthena

    http://web.mit.edu/olh/Emacs/ Emacsreferencecard

    http://web.mit.edu/olh/Emacs/Refcard.pdf

    http://web.mit.edu/olh/Emacs/http://web.mit.edu/olh/Emacs/Refcard.pdf

  • InvokingorinstallingEmacs emacs21onAthena:athena%emacs emacs22onUbuntu/Debian:aptgetinstallemacssnapshotgtk

    emacs22onGentoo:emergeemacscvs emacsonWindows:

    http://ourcomments.org/Emacs/EmacsW32Util.html

  • Bonus:BeingUnproductiveWithEmacs Mxtetr