Tải bản đầy đủ - 0 (trang)
Chapter 5. Emacs as a Work Environment

Chapter 5. Emacs as a Work Environment

Tải bản đầy đủ - 0trang

5.1.ExecutingCommandsinShellBuffers

OneofthemostimportantfeaturesofEmacsisitsabilitytorun

acommandshellinabuffer.Onceyouhavestartedashell

buffer,youcandoallofyournormalcommand-lineworkwithin

Emacs.Whatdoesthisbuyyou?

Youdon'thavetoleaveEmacstogetacommandprompt.If

youwanttoprintorcompileafilethatyou'reediting,you

candoitimmediately.

YoucanuseEmacseditingfeaturestowriteyour

commands.

YoucanuseEmacseditingfeaturesto"backup"through

yourcommandlist,copyanoldcommand,modifyit,and

executeitagain.

Youcansaveyourshellbuffer,keepingatranscriptofyour

editingsessionwhichautomaticallyincludestheoutputfrom

everycommandthatyouran.Fordebuggingor

rememberingcommandsyouruninfrequently,thiscanbe

invaluable.

Youcancopyoutputfromcommandsintoafileorinto

anothercommand.

Youcansavecomplexcommandsinafileandinsertthefile

attheprompt,ratherthanretypingthecommand.

AsyougetusedtoworkingwithinEmacs,youwillundoubtedly

discovermoreandmorewaystoputshellmodetouse.

Inthissection,wediscussshellmode.Laterinthischapter,we



discussdirectoryediting,printing,andcalendaranddiary

featuresfordoingsimpletimemanagementinEmacs.Right

now,we'llstartwithasimplevariationonshellmode,afeature

thatletsyouexecutecommandsoneatatime.



5.1.1RunningOneCommandataTime

Torunacommandwhileyou'reinanEmacssession,typeM-!.

Emacsasksforthecommandyouwanttorun.Typethe

commandandpressEnter.Emacsthenopensawindowcalled

*ShellCommandOutput*whereitdisplaystheresultsofyour

command.

Type:M-!



Emacspromptsyouforacommandtoexecute.



Type:diffjoycejoyce2



Emacsexecutesthediffcommandandputstheoutputintoa*ShellCommandOutput*

buffer.



Becausetheoutputfromthediffcommandisinabuffer,you

caneditit,saveit,ordoanythingelseyouwouldlikewithit.Of

course,iftheoperatingsystemhasnodiffcommandorcannot

accessitforsomereason,thiscommandfails.

Aninterestingtwisttotheshellcommandfacilityisthatyoucan

usearegionofabufferratherthanatraditionalfileasinputto

thecommand.Forexample,let'ssaywewanttosortaphone

list.First,weputthecursorsomewhereinthelist(say,onthe

firstcharacterofLiam),thenwegivethemark-paragraph

command(M-h).Thiscommanddefinesthephonelistasa

region,withthecursoratthebeginningoftheparagraphand

themarkattheend.

Inthefollowingexample,theshadedareashowstheextentof

theregionwewanttosort.Afterselectingaregion,wepress

M-|(forshell-command-on-region);Emacspromptsforthe

shellcommandtorun.

Type:M-hM-|



Emacspromptsyouforacommandtoexecute(Windows).



Nowwegivethecommandsortwithoutspecifyinganyinput

file.Emacsistakingcareoftheinputforus.

Type:sortEnter



Emacsrunsasortontheregion(Windows).



Emacshassortedthephonelist(i.e.,everythingwithinthe

region).

AusefulvariationforM-!putstheoutputdirectlyintothe

currentbuffer,ratherthanintoa*ShellCommandOutput*

buffer.Todoso,precedethecommandwithC-u:forexample,

C-uM-!runsashellcommandandputstheoutputinthe

currentbuffer.

Type:C-uM-!ls-laEnter



Emacsrunslsandinsertstheresultatyourcurrentlocation(MacOSX).



5.1.2UsingShellMode

Nowwe'rereadytodiscussshellmode,theinteractivefacility

forrunningcommands.Tostartashellbuffer,typeM-xshell

Enter.Thiscreatesabuffernamed*shell*.Youseethe

promptforyourshellwithinthisbuffer.(Thisdefaultstoyour

usualshell;youcansubstituteanothershelltouseinEmacs.

See"Whichshell?"laterinthischapter.)



Figure5-1.ShellbuffersforLinux,MacOSX,and

Windows



Forthemostpart,shellmodeisexactlylikethenormal

commandinterface,exceptthatyoucanuseEmacstoeditthe

commandsasyoutypethem.Youcancopycommandsfrom

oneplacetoanother,copytheresultsintoafile,savethewhole

shellbuffertoafile,andsoon.NoteinFigure5-1thatEmacs

hasaddedafewitemstothemenubar(Complete,In/Out,and

Signals).

Afewtricksareworthknowing,though.Forexample,you

normallyinterruptacommandbytypingC-c.IfyoutypeC-cin

shellmode,EmacsthinksthattheC-cispartofacommand

meantforit,becausemanyEmacscommandsstartwithC-c.



Therefore,youhavetotypeC-cC-ctoterminatethecurrent

job.Likewise,underUnix,youtypeC-cC-ztostopajob,

insteadofC-z,andC-cC-dinsteadofC-d,andsoon.(C-cC-d

isnotstrictlynecessarybecauseEmacsunderstandsC-din

context.Ifyou'reattheendofthebuffer,C-dmeans"endof

file";ifyou'reanywhereelse,itdeletesacharacter.)

Alternatively,youcanselectoptionsfromtheSignalsmenu

ratherthanusingcontrolcharacters,ifdesired(forexample,

selectingEOFinsteadoftypingC-d).

Shellmodealsoprovidesafewconvenientshortcuts.The

commandM-pretrievesthelastshellcommandyoutyped,no

matterhowfarbackinthebufferitis.TypingsuccessiveM-p's

bringsbackearliercommands.

Type:M-p



M-pretrievesthelastcommand,evenifitisn'tonthescreen(MacOSX).



Inthisexample,thepreviouscommandwasmore

dickensxmas.tex.It'snolongeronthescreen;itsoutputhas

pusheditoffthetop.M-p(forcomint-previous-input)

retrievesthecommand,butdoesn'texecuteit;youcaneditthe

commandbeforepressingEnter.Tofindsubsequent

commands,typeM-n.



Ifthesecommandssoundfamiliartoyou,theyshould.Theyare

historycommands,whichareidenticaltotheminibufferhistory

commandswediscussedinChapter3.TheIn/Outmenuis

devotedtoworkingwithcommandhistory.

EnterandTabhavespecialfunctionsinshellmode.Pressing

Enterexecutesthecommandonthelinewherethecursoris,

evenifyoumovethecursoruptothelineofanearlier

commandyouwanttoexecuteagain.WhenyoupressEnter,

Emacscopiesthecommandtotheendofthebufferand

executesit.Ofcourse,youcanmodifythecommandbefore

pressingEnter.

PressingTabputstheEmacscompletionfeatureintoaction;

usecompletionforoperatingsystemcommands,filenames,and

variables.Notethatthecompletionofsystemcommandsworks

bestonUniximplementationslikeLinuxandMacOSX;Emacs

doesn'tseemtofindallthepossibleWindowscommands,for

example.

Ifyoutypeacommandthatproducesalotofoutput,cluttering

upyoursession,there'saneasywaytogetridofit.TypeC-c

C-o(forcomint-kill-output).

Type:C-cC-o



C-cC-oautomaticallydeletestheoutputfromthelastcommand(MacOSX).



Thepreviouscommand(ls-la)remainsonthescreen,butits

output,alonglistoffiles,isdeleted.C-cC-ocandeleteoutput

fromonlythemostrecentcommand;itcan'tdeleteoutputfrom

yourpreviouscommands.

AnotherusefulcommandforshellmodeisC-cC-r(forcomintshow-output).Thiscommandisusefulifacommandproduces

alotofoutputandcausesthefirstfewlinesofoutputtoscroll

offthescreen.C-cC-rrepositionsthewindowsothefirstline

ofoutputfromyourlastcommandisatthetopofthewindow.

Ifyouwanttoseetheendoftheoutputinstead,typeC-cC-e

(forcomint-show-maximum-output);thiscommandmoves

thelastlineoftheinputtothebottomofthewindow.

Whenyou'rewritingabook,movingbyparagraphsmakes

sense,butwhenyou'reusingashell,movingbyoutputgroupis

morehelpful.Anoutputgroupconsistsofacommandandits

output.Tomovetothepreviousoutputgroup,typeC-cC-p.To

movetothenextoutputgroup,typeC-cC-n.

Anadvantageofshellmodeisthatyoucanstartacommand

andtheneditanotherbufferwhilethecommandruns.Theshell

bufferdoesn'tneedtobeonscreen;justtypeM-xshelltoget

thebufferbackagain.

Youcanhavemultipleshellbuffersrunningatonce;justuse

thecommandM-xrename-uniquelytorenameyourshell

buffer.Youcanstartanothershellbuffer,andanother,and

anotherasmanyasyouneedtojuggleallyourtasks.



5.1.2.1Whichshell?

Normally,Emacsusesyourdefaultshellinshellmode.Under

Windowsthat'scmd.exe(thefamiliarC:\>promptoraclose



relative).[1]ButUnixhasawidevarietyofavailableshells,

includingtheGNUProject'sbashandthezedshell,zsh.

Whatevershellyounormallyuse,that'swhatEmacsstarts

whenyouentershellmode.

[1]YoudohavechoicesunderWindowsaswell,thankstoCygwin(http://cygwin.com/).For

example,ifyouwantedtorunCygwin'sbash,you'llfindhelpfulinformationonhowtosetthatup

onNgaiKimHoong'spageonthattopicathttp://www.khngai.com/emacs/cygwin.php.



HowdoesEmacsknowwhichshelltostart?First,itlooksatthe

variableshell-file-name.ThenitlooksforaUnixenvironment

variablenamedESHELL.Finallyitlooksforanenvironment

variablenamedSHELL.Ifyouwanttorunanotherparticular

shell(forexample,thezedshell)whenyou'reinEmacs,you

canaddthefollowingcommandtoyour.emacsfile:

(setqshell-file-name"/bin/zsh")



WhenEmacsstartsaninteractiveshell,itrunsanadditional

initializationfileafteryourshell'snormalstartupfiles.The

nameofthisfileis.emacs_shell-name,whereshell-nameisthe

nameoftheshellyouwanttouseinEmacs.Itmustbelocated

inyourhomedirectory.Forexample,ifyouusetheCshell,you

canaddEmacs-onlystartupcommandsbyplacingtheminthe

file.emacs_csh.Let'ssaythatwhenyou'reinEmacs,youwant

tochangetheprompttoemacs:%andyouwantan

environmentvariablecalledWITHIN_EDITORtobesettoT.

Here'sthecontentsofyour.emacs_cshfile:

setprompt="emacs:%"

setenvWITHIN_EDITORT



Withinashellbuffer,Emacsalsosetstheenvironmentvariable



EMACStot,andsetsyourterminaltype(theTERMvariable)

toemacs.



5.1.2.2Makingpasswordsinvisibleinshellmode

Bydefault,shellmodedisplayseverythingyoutypeandthat

includespasswordsnotagoodsituationifsomeoneispeering

overyourshoulder.Thereisawayaroundthisproblem,

however.Beforeyoutypethepassword,typeM-xsendinvisible.Emacsasksforthenonechoedtext.Whenyoutypea

character,Emacsputsanasteriskintheminibuffer.PressEnter

andEmacsentersthepasswordwithoutdisplayingit.Tohave

Emacshidepasswordsasyoutypethem,addthefollowingtwo

linestoyour.emacsfile:

(add-hook'comint-output-filter-functions

'comint-watch-for-password-prompt)



Emacsasksfornonechoedtextintheminibufferwhenevera

passwordpromptappearsonthescreen,makingsurethatthe

passwordisneverdisplayed.Table5-1summarizesshellmode

commands.

Table5-1.Shellmodecommands

Keystrokes

(none)



C-cC-cSignals



C-d



Commandname

shell



Action

Entershellmode.



comintBREAK interrupt-subjob Interruptcurrentjob;equivalenttoC-c.

comint-delchar- SendEOFcharacterifatendofbuffer;

or-maybe-eof

deleteacharacterelsewhere.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 5. Emacs as a Work Environment

Tải bản đầy đủ ngay(0 tr)

×