[BACK]Return to tutorial CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/tutorial, Revision 1.7

1.7     ! lum         1: $Id: tutorial,v 1.6 2011/09/02 02:37:52 lum Exp $
1.1       deraadt     2:
1.5       kjell       3: The mg Tutorial
                      4: ---------------
1.1       deraadt     5:
1.5       kjell       6: The mg editor is a public domain editor intended to loosely resemble GNU Emacs,
                      7: while still retaining fast speed and a small memory footprint.
1.1       deraadt     8:
1.5       kjell       9: Most mg commands involve using the Control (sometimes labelled "Ctrl") or the
                     10: Meta (sometimes labelled "Alt") key. We will use the following conventions in
                     11: this tutorial:
1.1       deraadt    12:
1.5       kjell      13:   C-<chr>   means hold down the Control key while typing the character <chr>.
                     14:   M-<chr>   means hold down the Meta key while typing the character <chr>.
1.1       deraadt    15:
1.5       kjell      16: If you don't have a Meta key, you can use Esc instead. Press and release the
                     17: Esc key and type <chr>. This is equivalent to M-<chr>.
1.1       deraadt    18:
1.5       kjell      19: The first thing to learn is how to move up and down a document. To move your
                     20: cursor down, use the down-arrow cursor key or C-n (Control and n)
1.1       deraadt    21:
1.5       kjell      22: >> Now type C-n multiple times and move your cursor past this line.
1.1       deraadt    23:
1.5       kjell      24: Congratulations. You have now learned how to move your cursor down. Note how
                     25: mg has redrawn your screen so that the cursor is now in the middle of the
                     26: screen. This is a feature of mg, which allows you to see the lines before and
                     27: after the current cursor position.
1.1       deraadt    28:
1.5       kjell      29: To move your cursor up, you can use the up-arrow cursor key or C-p (Control and
                     30: p).
1.1       deraadt    31:
1.5       kjell      32: >> Try using C-p and C-n to move up and down and then move past this line.
1.1       deraadt    33:
1.5       kjell      34: The next commands to learn are how to move your cursor left and right. To do
                     35: this, you can use the left-arrow and right-arrow cursor keys. Alternatively,
                     36: you can use C-b and C-f to do this.
1.1       deraadt    37:
1.5       kjell      38: >> Practise using the arrow keys or C-b and C-f on this line.
1.1       deraadt    39:
1.5       kjell      40: To make it easy to remember these commands, it helps to remember by letter:
                     41: P - Previous line, N - Next line, B - Backwards and F - Forward.
1.1       deraadt    42:
1.5       kjell      43: Now that you've learned how to move single characters at a time, next we learn
                     44: how to move one word at a time. To do this, you can use M-f (Meta and f) or
                     45: M-b (Meta and b) to move forwards and backwards, one word at a time.
1.1       deraadt    46:
1.5       kjell      47: >> Try moving one word at a time by using M-f and M-b on this line.
1.1       deraadt    48:
1.5       kjell      49: Notice how the Ctrl and Meta key combinations perform related functions. C-f
                     50: moves one letter forward, whereas M-f moves one word forward. There are many
                     51: key combinations in mg, where C-<chr> will perform one function and M-<chr>
                     52: will perform a similar related function.
1.1       deraadt    53:
1.5       kjell      54: Next, we will learn how to refresh and redraw the screen.
1.1       deraadt    55:
1.5       kjell      56: >> Now move the cursor down to this line and then type C-l (that's Control and
                     57:    lowercase L) to refresh the screen.
1.1       deraadt    58:
1.5       kjell      59: Note that C-l refreshes the screen and centers it on the line you typed it on.
1.1       deraadt    60:
1.5       kjell      61: To move to the beginning or end of a line, you can use the Home and End keys,
                     62: or you can use C-a and C-e to move to the beginning and end.
1.1       deraadt    63:
1.5       kjell      64: >> Use C-a and C-e to move to the beginning and end of this line.
1.1       deraadt    65:
1.5       kjell      66: The next commands we will learn is how to move up and down, one page at a time.
                     67: To do this, you can use the Page Up (sometimes labelled PgUp) and Page Down
                     68: (sometimes labelled PgDn) keys. You can also use C-v and M-v to do this. C-v
                     69: moves the cursor down one page and M-v moves it up one page.
1.2       mickey     70:
1.5       kjell      71: >> Try using M-v and C-v to move up and down, one page at a time.
1.1       deraadt    72:
1.5       kjell      73: The final two motion commands we will learn are M-< (Meta-Less than) and
1.7     ! lum        74: M-> (Meta-Greater than) which move you to the beginning and end of a file,
        !            75: respectively. You may not want to try that now as you will probably lose your
        !            76: place in this tutorial. Note that on most terminals, < is above the , key, so
        !            77: you'll need to press the Shift key to type <.
1.1       deraadt    78:
1.5       kjell      79: Movement Summary
1.1       deraadt    80: -----------------
                     81:
1.5       kjell      82: The following is a summary of the movement commands we've learned so far:
1.1       deraadt    83:
1.5       kjell      84:        C-f     Move forward one character (can also use right arrow key)
                     85:        C-b     Move backward one character (can also use left arrow key)
                     86:        C-p     Move up one line (can also use up arrow key)
                     87:        C-n     Move down one line (can also use down arrow key)
                     88:        M-f     Move forward one word
                     89:        M-b     Move backward one word
                     90:        C-a     Move to beginning of line (can also use Home key)
                     91:        C-e     Move to end of line (can also use End key)
                     92:        C-v     Move forward one page (can also use PgDn/Page Down key)
                     93:        M-v     Move backward one page (can also use PgUp/Page Up key)
                     94:        M-<     Move to beginning of file
                     95:        M->     Move to end of file.
                     96:
                     97: Now that you've mastered the basics of moving around in mg, you can cause mg
                     98: to execute these commands multiple times. The way to do this is to type
                     99: C-u followed by some digits followed by a movement command.
                    100:
                    101: >> Type C-u 5 C-f to move forward 5 characters.
                    102:
                    103: In general, C-u allows you to execute any command multiple times, not just
                    104: cursor motion commands. The only exception to this rule are C-v and M-v.
                    105: When using these two commands with an argument, they move the cursor by that
                    106: many lines instead of pages.
1.1       deraadt   107:
1.5       kjell     108: Cancelling mg commands
1.1       deraadt   109: ----------------------
                    110:
1.5       kjell     111: If you have started typing out a command that you didn't mean to finish, you
                    112: can use the C-g command to cancel the command immediately.
1.1       deraadt   113:
1.5       kjell     114: >> For example, type C-u 50 and then type C-g to cancel the C-u command.
                    115: >> Type Esc and then C-g to cancel the Esc key.
1.1       deraadt   116:
1.5       kjell     117: In general, you can use C-g to stop any mg commands. You may type it multiple
                    118: times if you wish. You should see the word "Quit" appear in the bottom of the
                    119: screen when you type C-g indicating that a command was cancelled.
1.1       deraadt   120:
1.5       kjell     121: In general, when in doubt, use C-g to get out of trouble.
1.1       deraadt   122:
1.5       kjell     123: Windows
                    124: -------
1.1       deraadt   125:
1.5       kjell     126: The mg editor can support several windows at the same time, each one displaying
                    127: different text. To split a screen into two horizontal windows use C-x 2 to do
                    128: this. To return to one window, use C-x 1 to close the other windows and only
                    129: keep the current window.
                    130:
                    131: >> Use C-x 2 to split the screen into two windows.
                    132:
                    133: >> Use C-x o to move from one window to the other. You can scroll up and down
                    134:    in each window using the cursor keys or C-n and C-p keys.
                    135:
                    136: >> Use C-x 1 to restore back to one window.
                    137:
                    138: Inserting/Deleting Text
                    139: -----------------------
                    140:
                    141: To insert text anywhere, simply move your cursor to the appropriate position
                    142: and begin typing. To delete characters, use the backspace key. If you use
1.7     ! lum       143: M-<backspace> (Meta and backspace key), you will delete one word instead
1.5       kjell     144: of one character at a time.
                    145:
                    146: To delete characters to the right of the cursor, you can use C-d to delete
                    147: characters to the right of the current position.  If you use M-d instead of
                    148: C-d, you can delete one word at a time instead of one character at a time.
                    149:
                    150: >> Try inserting and deleting characters and words on this line.
                    151:
                    152: Note that if you type too many characters on a single line, the line will
                    153: scroll off the screen and you will see a $ on the line to indicate that the
                    154: line is too long to fit on the screen at one time.
                    155:
                    156: To delete a line at a time, you can use C-k to kill the line from the current
                    157: cursor position to the end of the line. You can type C-k multiple times to
                    158: kill many lines.
                    159:
                    160: You can issue insert or delete commands multiple times using C-u. For example,
                    161: C-u 10 e will type out eeeeeeeeee, C-u 4 M-d will delete four words to the
                    162: right of the cursor and so on.
                    163:
                    164: To undo any operation, you can use C-_ (That's control-underscore).
                    165:
                    166: Now if you kill something that you didn't mean to, you can yank it back from
                    167: the dead by using C-y. In general, when you kill something bigger than a single
                    168: character, mg saves it in a buffer somewhere and you can restore it by using
                    169: C-y. This is useful for moving text around. You can kill text in one place,
                    170: move your cursor to the new location and then use C-y to paste it there.
                    171:
                    172: Search for Text
                    173: ---------------
                    174:
                    175: To search for text, type C-s followed by the text you wish to search for. Note
                    176: that as you start typing the characters, mg automatically searches as you type
                    177: the characters.
                    178:
                    179: To continue searching the text you're looking for, type C-s to find the next
                    180: instance. To search in reverse, type C-r instead of C-s. If you type C-s or
                    181: C-r twice, it will simply search for the last text that you searched for.
                    182:
                    183: To stop searching for text, simply use the cursor keys (or C-f, C-b etc.) or
                    184: C-g to stop the search operation.
                    185:
                    186: >> Use C-s foo to search for "foo" in the text. You can use C-s again to
                    187:    find other instances of foo in the file.
                    188:
                    189: Note that if a word cannot be found, it will say Failing I-search: at the
                    190: bottom of the screen. Typing C-s again will wrap the search around from the
                    191: top of the file and begin searching from there.
                    192:
                    193: Replace Text
                    194: ------------
                    195:
                    196: To replace text, use M-%. You will be prompted for the text to search for and
                    197: the text to replace it with. You will then be taken to the first instance of
                    198: text from the current position. At this point you can do one of the following:
                    199:
                    200:        y - Replace the text at this instance and search for more items
                    201:        n - Skip this instance and search for more items
                    202:        . or Enter - Stop replacing text (You can also use C-g)
                    203:        ! - Replace all the instances without prompting at each one.
                    204:
                    205: >> Try replacing "frobnitz" with "zutwalt" on this line.
                    206:
                    207: Cut/Copy/Paste Text
                    208: -------------------
                    209:
                    210: As explained above, you can cut regions using C-k to kill multiple lines. To
                    211: paste the text that you just cut, simply move your cursor to the point and
                    212: then type C-y to restore the text. You may type C-y multiple times to restore
                    213: the text. Hence, to copy text, you can use C-k to kill all the lines, use C-y
                    214: to restore it immediately, then move to the region you want to copy it to and
                    215: then type C-y again to restore the last cut text block again.
                    216:
                    217: Another way to cut or copy chunks of text is to first position your cursor at
1.7     ! lum       218: the starting point of the chunk of text. Then type C-<space> to mark this as
        !           219: the starting point to cut or copy. Then move the cursor to the end point of the
1.5       kjell     220: text chunk that you wish to manipulate. Then type C-w to cut the region, or
                    221: M-w to copy the region. If you wish to cancel marking a block of text, simply
                    222: type C-g to cancel the operation.
                    223:
                    224: To paste the region that you've cut or copied above, simply move your cursor
                    225: to the desired location and then type C-y to paste it.
                    226:
                    227: Status Line
                    228: -----------
                    229:
                    230: At the bottom of your screen is a reverse highlighted line. This is the status
                    231: line and lets you know some useful information about the file you're editing.
                    232:
                    233: On the status line, you should see "Mg: tutorial". This lets you know that
                    234: you're editing a file named "tutorial". If you've edited this file and not
                    235: saved it, it should have a "**" to the left of those words. If this file is
                    236: read-only, you should see a "%%" to the left of those words.
                    237:
                    238: To the right of the status line, you should see L followed by digits and C
                    239: followed by some more digits. These indicate the line number and column number
                    240: of the file that your cursor is currently on. If you move the cursor around,
                    241: you should see the line and column number change.
                    242:
                    243: In the middle of the screen, you should see the word "(fundamental)" which
                    244: indicates that the current editing mode is "fundamental-mode". The mg editor
                    245: also supports a c-mode that is more suited to editing C code. There are also
1.6       lum       246: some other useful editing modes for different situations. See the man page
1.5       kjell     247: for mg(1) to learn about the various editing modes.
                    248:
                    249: Opening and Saving Files
                    250: ------------------------
                    251:
                    252: To open a file, you can use C-x C-f. You will then be prompted for a file name.
                    253: If you type a file name that doesn't already exist, a new file will be opened
                    254: for you. If the file name already exists, then it will be opened for you and
                    255: you can begin editing it. Note that you do not need to type the whole file
                    256: name for an existing file. You can type part of the file name and then press
                    257: the TAB key. If there is only file name that matches, mg will fill in the rest
                    258: of the file name for you. If there are multiple files, mg will display that
                    259: the choice is ambiguous. If you type the TAB key again, mg will show you all
                    260: the available choices for file names.
                    261:
                    262: NOTE: If you type C-x f instead of C-x C-f, you can use C-g to cancel the
                    263: Set-Fill-Column command. You can also use C-g to cancel the C-x C-f command
                    264: if you don't wish to open a new file.
                    265:
                    266: To save the file once you've edited it, use C-x C-s to save the file. When
                    267: mg is done saving the file, you should see the words "Wrote /path/to/file"
                    268: in the bottom of your screen. In general, it is a good idea to save quite
                    269: often. When you save a file, mg saves a backup of the file with a tilde (~)
                    270: character at the end.
1.1       deraadt   271:
1.5       kjell     272: Buffers
                    273: -------
1.1       deraadt   274:
1.5       kjell     275: The mg editor is capable of editing multiple files at the same time. When you
                    276: open a second file with C-x C-f, the first file is still being edited by mg.
                    277: You can list all the buffers that are opened by mg by typing C-x C-b. The
                    278: screen should divide into two and the top window will list the buffers that
                    279: are currently open. Use C-x o to switch to the top window (we already learned
                    280: this key combination above in the Windows section) and then use the arrow keys
                    281: to move to the buffer you wish to switch to and then type the Enter key to
                    282: select that buffer. Then use C-x 1 to switch back to only one window.
                    283:
                    284: You may also move back to the last opened buffer by using C-x b to toggle back
                    285: and forth between two buffers. Note the difference between C-x b and C-x C-b.
                    286:
                    287: >> Use C-x C-f to open a new file
                    288: >> Use C-x b to switch back and forth between that buffer and this one.
                    289:
                    290: To edit files in multiple windows, use C-x 2 to split the screen into two
                    291: windows. Then use C-x C-f to open a new file in one of the two windows. You
                    292: can then switch between the two windows using C-x o. You can switch between
                    293: buffers in any window using C-x b. To go back to one window, use C-x 1.
                    294:
                    295: To kill any buffer, use C-x k. You will be prompted for the buffer to kill.
                    296: By default, the current buffer is selected as the one to kill. You may also
                    297: type another buffer name or use C-g to cancel the operation.
1.1       deraadt   298:
1.5       kjell     299: Extended Commands
                    300: -----------------
1.1       deraadt   301:
1.5       kjell     302: The mg editor has several extended commands, more than what can be covered
                    303: by the Control and Meta keys. The mg editor gets around this by using what is
                    304: called the X (eXtend) command. There are two forms of this:
                    305:
                    306:        C-x     Character eXtension. Followed by one character.
                    307:        M-x     Named character eXtension. Followed by a long command.
                    308:
                    309: You've already seen C-x C-f and C-x C-s to open and save a file. There are
                    310: other longer commands. For instance, you can also open a file by typing
                    311: M-x open-file Enter. When you type a command using M-x, mg prompts you for
                    312: the command at the bottom of the screen. You can type out the whole command
                    313: if you wish, or you can type out part of the command and then use the TAB key
                    314: for autocompleting the command.
1.1       deraadt   315:
1.5       kjell     316: For instance, to replace text, you can type M-x repl TAB enter to execute
                    317: the replace-text command. To cancel this command, type C-g.
1.1       deraadt   318:
1.6       lum       319: To see a list of all available mg(1) commands, consult the man page.
1.1       deraadt   320:
1.5       kjell     321: Exiting mg
                    322: ----------
1.1       deraadt   323:
1.5       kjell     324: To exit mg temporarily and return to the shell, use C-z. This will take you
                    325: back to the command shell. To return back to mg, type fg in the shell and you
                    326: will be returned to your mg session.
1.1       deraadt   327:
1.5       kjell     328: To exit mg permanently, type C-x C-c. If you have any unsaved buffers, you
                    329: will be asked if you wish to save them or not.
1.1       deraadt   330:
1.5       kjell     331: Conclusion
                    332: ----------
1.1       deraadt   333:
1.5       kjell     334: This tutorial is meant to get new users up and running with mg. There is more
1.6       lum       335: information available via the mg(1) man page. If you have any suggestions for
1.5       kjell     336: improvement, please don't hesitate to drop a message or (better still) submit
1.6       lum       337: a diff to tech@openbsd.org.
1.1       deraadt   338:
1.5       kjell     339: History
1.1       deraadt   340: -------
                    341:
1.5       kjell     342: mg is a public-domain text editor. It was originally based on
                    343: MicroEMACS, but has since moved to more closely resemble GNU Emacs while
                    344: still maintaining a small memory footprint and fast speed.
                    345:
                    346: * Nov 16, 1986: First release to mod.sources
                    347: * Mar 3, 1987: First Release (mg1a) via comp.sources.unix
                    348: * May 26, 1988: Second release: (mg2a) via comp.sources.misc
                    349: * Jan 26, 1992: Linux port released by Charles Hedrick. This version
                    350:   later makes its way onto tsx-11, Infomagic, and various other Linux
                    351:   repositories.
                    352: * Feb 25, 2000: First import into the OpenBSD tree, where it is
                    353:   currently maintained
                    354:
                    355: The mg editor was originally named MicroGNUEmacs. The name was changed
                    356: to "mg" at the request of Richard Stallman, as this software is
                    357: entirely unrelated to the GNU project.
1.1       deraadt   358:
1.5       kjell     359: Author Info
                    360: -----------
1.1       deraadt   361:
1.5       kjell     362: Original Author of this document: Mayukh Bose,
1.7     ! lum       363: Date last updated: 2011-09-10
1.1       deraadt   364:
1.5       kjell     365: Copyright
1.1       deraadt   366: ---------
                    367:
1.5       kjell     368: None. This document is in the public domain.
1.1       deraadt   369: