emacs lecture

Upload: achiaa-osei-donkor

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Emacs Lecture

    1/15

    EMACSBy Justin Higgins

  • 8/10/2019 Emacs Lecture

    2/15

    What is a text editor?

    If youve ever used Microsoft word you knowwhat a text editor is (albeit a bloated one).

    When you write a program in this class you will

    use a text editor to write the file. In this file youwill write a series of commands which uponexecution will be performed.

    In our case: we will use the text editor to write

    IDL programs. Naturally the file you create willhave a series of IDL commands (theseAREdifferent from UNIX commands)

  • 8/10/2019 Emacs Lecture

    3/15

    Emacs vs VIM

    There are primarily two text editors programmersuse: Emacs and VIM

    Both have incredible functionality with VIM

    focusing more onfewer keystrokesand Emacsfocusing on customization.

    We will teach you Emacs, although it makes nodifference in the future if you become proficient

    in one or the other.

    Beware! You are choosing a side in the holiest ofholy geek wars.

  • 8/10/2019 Emacs Lecture

    4/15

    Open Emacs

    To open emacs in a terminal simply typeemacs.

    To directly open a file with emacs from the

    terminal type emacs file_name If you do this on the ugastro server a new

    window will open up with the GUI emacs.

    If you have a Mac you can open Emacs interminal the same way, but you will not getthe same GUI (it will be built in the terminal).

  • 8/10/2019 Emacs Lecture

    5/15

    Emacs basics

    Most emacs commands use a Ctrl-xplus some

    characters to perform a command:

    Examples:

    To open a file: Ctrl-x Ctrl-f (then type the file

    path)

    To save a file: Ctrl-x Ctrl-s To close a file: Ctrl-x Ctrl-c

  • 8/10/2019 Emacs Lecture

    6/15

    Editing

    To edit a file simply type in the main window.

    Search forward: Ctrl-s (type the search string)

    type Ctrl-s repeatedly to step through searches

    Search reverse: Ctrl-r

    Beginning of line: Ctrl-a

    End of line: Ctrl-e

    Beginning of file: Esc-

  • 8/10/2019 Emacs Lecture

    7/15

    Ooops! I made a poopsy!

    To undo an action: Ctrl-_ (or Ctrl-x u)

    To cancel a command you are typing: Ctrl-g

    Example:

    (I want to save a file but I accidentally typed:Ctrl-x Ctrl-c which will close it if I hit enter!)

    Ctrl-gType multiply times if not stopping. this will work

    anywhere within emacs

  • 8/10/2019 Emacs Lecture

    8/15

    Intermediate

    Goal: Once you are comfortable you should

    very seldom use your mouse/track pad or

    have to type the arrow key repeatedly.

    Go to a line: M-x goto-line

    Query replace: Esc-% (type search

    stringentertype replace string.enter, skip

    a change with n and execute one with

    spacebar)

  • 8/10/2019 Emacs Lecture

    9/15

    Markers

    You can set a marker where you would like

    some text operations executed. Do this by

    typing: Ctrl-space (you will see mark set on

    the bottom)

    Now wherever you move youre the cursor

    next, will define the mark up region (the

    region you want to edit).

  • 8/10/2019 Emacs Lecture

    10/15

    Using markers (dont inhale)

    Remove or kill a line: Ctrl-k

    Remove a region of text:

    Set marker Move cursor to bottom of remove region

    Ctrl-w

    IMPORTANT: After you remove a line or regionthat text is temporarily stored and can be

    pasted by typing Ctrl-y (for yank)

  • 8/10/2019 Emacs Lecture

    11/15

    Buffers

    You can have multiple bufferswithin one windowof emacs. That is OS will only show that oneterminal window of emacs is open, but in thatwindow can be MANY files.

    Say you open a file (file1) and then want to openup another file (file2) whilst leaving file1 open.Simply type Ctrl-x Ctrl-f and the path for file2 andyouve now opened up file2 in a new buffer.

    To switch back to file1, type Ctrl-x bfile1. If you forget which files are open in buffers: Ctrl-x

    Ctrl-b displays available buffers

  • 8/10/2019 Emacs Lecture

    12/15

    Viewing multiple buffers

    simultaneously This is perhaps one of the most useful tricks you will use in

    emacs.

    You will often want to compare two or more of yourprograms. Instead of opening up multiple emacs windowsyou split one emacs window into many. In each of thesewindows you can display a buffer.

    To split the window horizontally: Ctrl-x 2

    To split the window vertically: Ctrl-x 3

    To close the window (YOU ARE IN): Ctrl-x 0 To close all the windows EXCEPT the one you are in: Ctrl-x 1

    To switch between windows: Ctrl-x o

  • 8/10/2019 Emacs Lecture

    13/15

    Advanced: Macros

    Eventually you become so 1337, so Super

    Saiyan that even the shortcuts arent fast

    enough for you. Luckily you may define your

    own shortcuts with macros.

    Begin defining a macro: Ctrl-x (

    Define one iteration of the operation

    End macro definition: Ctrl-x )

    Execute the macro: Ctrl-x e

  • 8/10/2019 Emacs Lecture

    14/15

    Iterative operations

    To execute a command iteratively: Ctrl-u #

    command (where # is the number of

    executions)

    Example:

    To indent a region 4 spaces:

    Define region.

    Ctrl-u 4 Ctrl-x

  • 8/10/2019 Emacs Lecture

    15/15

    Last remarks

    You will only master these by practice. If youtake the time to learn the commands theprogramming bottleneck is simply the

    execution of your code (and not your typingspeed).

    Of course, it is easy to get distracted withemacs shortcuts. Remember they are thereto simplify a taskthat is, if it is too muchwork to use a shortcut DONT USE IT!