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