1eb2e2a49SkjellThe mg Tutorial 2eb2e2a49Skjell--------------- 3be803e14Sderaadt 4eb2e2a49SkjellThe mg editor is a public domain editor intended to loosely resemble GNU Emacs, 5eb2e2a49Skjellwhile still retaining fast speed and a small memory footprint. 6be803e14Sderaadt 7eb2e2a49SkjellMost mg commands involve using the Control (sometimes labelled "Ctrl") or the 8eb2e2a49SkjellMeta (sometimes labelled "Alt") key. We will use the following conventions in 9eb2e2a49Skjellthis tutorial: 10be803e14Sderaadt 11eb2e2a49Skjell C-<chr> means hold down the Control key while typing the character <chr>. 12eb2e2a49Skjell M-<chr> means hold down the Meta key while typing the character <chr>. 13be803e14Sderaadt 14eb2e2a49SkjellIf you don't have a Meta key, you can use Esc instead. Press and release the 15eb2e2a49SkjellEsc key and type <chr>. This is equivalent to M-<chr>. 16be803e14Sderaadt 17eb2e2a49SkjellThe first thing to learn is how to move up and down a document. To move your 187e81ce85Slumcursor down a line, use the down-arrow cursor key or C-n (Control and n). 19be803e14Sderaadt 20eb2e2a49Skjell>> Now type C-n multiple times and move your cursor past this line. 21be803e14Sderaadt 227e81ce85SlumCongratulations. You have now learned how to move your cursor down. To move 237e81ce85Slumyour cursor up one line, you can use the up-arrow cursor key or C-p (Control 247e81ce85Slumand p). 25be803e14Sderaadt 26eb2e2a49Skjell>> Try using C-p and C-n to move up and down and then move past this line. 27be803e14Sderaadt 28eb2e2a49SkjellThe next commands to learn are how to move your cursor left and right. To do 29eb2e2a49Skjellthis, you can use the left-arrow and right-arrow cursor keys. Alternatively, 30eb2e2a49Skjellyou can use C-b and C-f to do this. 31be803e14Sderaadt 32eb2e2a49Skjell>> Practise using the arrow keys or C-b and C-f on this line. 33be803e14Sderaadt 34eb2e2a49SkjellTo make it easy to remember these commands, it helps to remember by letter: 35eb2e2a49SkjellP - Previous line, N - Next line, B - Backwards and F - Forward. 36be803e14Sderaadt 37eb2e2a49SkjellNow that you've learned how to move single characters at a time, next we learn 38eb2e2a49Skjellhow to move one word at a time. To do this, you can use M-f (Meta and f) or 39eb2e2a49SkjellM-b (Meta and b) to move forwards and backwards, one word at a time. 40be803e14Sderaadt 41eb2e2a49Skjell>> Try moving one word at a time by using M-f and M-b on this line. 42be803e14Sderaadt 43eb2e2a49SkjellNotice how the Ctrl and Meta key combinations perform related functions. C-f 44eb2e2a49Skjellmoves one letter forward, whereas M-f moves one word forward. There are many 45eb2e2a49Skjellkey combinations in mg, where C-<chr> will perform one function and M-<chr> 46eb2e2a49Skjellwill perform a similar related function. 47be803e14Sderaadt 487e81ce85SlumMost probably by now you have moved passed the bottom of the text which was 497e81ce85Sluminitially shown to you when you opened this document. mg redraws your terminal 507e81ce85Slumscreen so that the cursor is in the middle. This is a feature of mg, which 517e81ce85Slumallows you to see the lines before and after the current cursor position. The 527e81ce85Slumsame effect can be achieved manually. 53be803e14Sderaadt 54eb2e2a49Skjell>> Now move the cursor down to this line and then type C-l (that's Control and 557e81ce85Slum lowercase L). 56be803e14Sderaadt 57eb2e2a49SkjellNote that C-l refreshes the screen and centers it on the line you typed it on. 58be803e14Sderaadt 59eb2e2a49SkjellTo move to the beginning or end of a line, you can use the Home and End keys, 60eb2e2a49Skjellor you can use C-a and C-e to move to the beginning and end. 61be803e14Sderaadt 62eb2e2a49Skjell>> Use C-a and C-e to move to the beginning and end of this line. 63be803e14Sderaadt 64eb2e2a49SkjellThe next commands we will learn is how to move up and down, one page at a time. 65eb2e2a49SkjellTo do this, you can use the Page Up (sometimes labelled PgUp) and Page Down 66eb2e2a49Skjell(sometimes labelled PgDn) keys. You can also use C-v and M-v to do this. C-v 67eb2e2a49Skjellmoves the cursor down one page and M-v moves it up one page. 68be803e14Sderaadt 69eb2e2a49Skjell>> Try using M-v and C-v to move up and down, one page at a time. 70be803e14Sderaadt 71eb2e2a49SkjellThe final two motion commands we will learn are M-< (Meta-Less than) and 728ee02a2dSlumM-> (Meta-Greater than) which move you to the beginning and end of a file, 738ee02a2dSlumrespectively. You may not want to try that now as you will probably lose your 748ee02a2dSlumplace in this tutorial. Note that on most terminals, < is above the , key, so 758ee02a2dSlumyou'll need to press the Shift key to type <. 76be803e14Sderaadt 77eb2e2a49SkjellMovement Summary 78be803e14Sderaadt----------------- 79be803e14Sderaadt 80eb2e2a49SkjellThe following is a summary of the movement commands we've learned so far: 81be803e14Sderaadt 822b13f599Slum C-f Move forward one character (can also use right arrow key). 832b13f599Slum C-b Move backward one character (can also use left arrow key). 842b13f599Slum C-p Move up one line (can also use up arrow key). 852b13f599Slum C-n Move down one line (can also use down arrow key). 862b13f599Slum M-f Move forward one word. 872b13f599Slum M-b Move backward one word. 882b13f599Slum C-a Move to beginning of line (can also use Home key). 892b13f599Slum C-e Move to end of line (can also use End key). 902b13f599Slum C-v Move forward one page (can also use PgDn/Page Down key). 912b13f599Slum M-v Move backward one page (can also use PgUp/Page Up key). 922b13f599Slum M-< Move to beginning of file. 93eb2e2a49Skjell M-> Move to end of file. 94be803e14Sderaadt 95eb2e2a49SkjellNow that you've mastered the basics of moving around in mg, you can cause mg 962b13f599Slumto execute these commands multiple times. The way to do this is to type C-u 972b13f599Slumfollowed by some digits followed by a movement command. 98be803e14Sderaadt 99eb2e2a49Skjell>> Type C-u 5 C-f to move forward 5 characters. 100be803e14Sderaadt 101eb2e2a49SkjellIn general, C-u allows you to execute any command multiple times, not just 102eb2e2a49Skjellcursor motion commands. The only exception to this rule are C-v and M-v. 103eb2e2a49SkjellWhen using these two commands with an argument, they move the cursor by that 104eb2e2a49Skjellmany lines instead of pages. 105be803e14Sderaadt 1062b13f599SlumCancelling mg Commands 107be803e14Sderaadt---------------------- 108be803e14Sderaadt 109eb2e2a49SkjellIf you have started typing out a command that you didn't mean to finish, you 110eb2e2a49Skjellcan use the C-g command to cancel the command immediately. 111be803e14Sderaadt 112eb2e2a49Skjell>> For example, type C-u 50 and then type C-g to cancel the C-u command. 113eb2e2a49Skjell>> Type Esc and then C-g to cancel the Esc key. 114be803e14Sderaadt 115eb2e2a49SkjellIn general, you can use C-g to stop any mg commands. You may type it multiple 116eb2e2a49Skjelltimes if you wish. You should see the word "Quit" appear in the bottom of the 117eb2e2a49Skjellscreen when you type C-g indicating that a command was cancelled. 118be803e14Sderaadt 119eb2e2a49SkjellIn general, when in doubt, use C-g to get out of trouble. 120be803e14Sderaadt 121eb2e2a49SkjellInserting/Deleting Text 122eb2e2a49Skjell----------------------- 123be803e14Sderaadt 124eb2e2a49SkjellTo insert text anywhere, simply move your cursor to the appropriate position 125eb2e2a49Skjelland begin typing. To delete characters, use the backspace key. If you use 1268ee02a2dSlumM-<backspace> (Meta and backspace key), you will delete one word instead 127eb2e2a49Skjellof one character at a time. 128be803e14Sderaadt 129eb2e2a49SkjellTo delete characters to the right of the cursor, you can use C-d to delete 130eb2e2a49Skjellcharacters to the right of the current position. If you use M-d instead of 131eb2e2a49SkjellC-d, you can delete one word at a time instead of one character at a time. 132be803e14Sderaadt 133eb2e2a49Skjell>> Try inserting and deleting characters and words on this line. 134be803e14Sderaadt 135eb2e2a49SkjellNote that if you type too many characters on a single line, the line will 136eb2e2a49Skjellscroll off the screen and you will see a $ on the line to indicate that the 137eb2e2a49Skjellline is too long to fit on the screen at one time. 138be803e14Sderaadt 139eb2e2a49SkjellTo delete a line at a time, you can use C-k to kill the line from the current 140eb2e2a49Skjellcursor position to the end of the line. You can type C-k multiple times to 141eb2e2a49Skjellkill many lines. 142be803e14Sderaadt 143eb2e2a49SkjellYou can issue insert or delete commands multiple times using C-u. For example, 144eb2e2a49SkjellC-u 10 e will type out eeeeeeeeee, C-u 4 M-d will delete four words to the 145eb2e2a49Skjellright of the cursor and so on. 146be803e14Sderaadt 147d2c9ceb5SlumTo undo any operation, you can use C-_ (that's control-underscore). 148be803e14Sderaadt 149eb2e2a49SkjellNow if you kill something that you didn't mean to, you can yank it back from 150eb2e2a49Skjellthe dead by using C-y. In general, when you kill something bigger than a single 151eb2e2a49Skjellcharacter, mg saves it in a buffer somewhere and you can restore it by using 152eb2e2a49SkjellC-y. This is useful for moving text around. You can kill text in one place, 153eb2e2a49Skjellmove your cursor to the new location and then use C-y to paste it there. 154be803e14Sderaadt 155eb2e2a49SkjellSearch for Text 156eb2e2a49Skjell--------------- 157be803e14Sderaadt 158eb2e2a49SkjellTo search for text, type C-s followed by the text you wish to search for. Note 159eb2e2a49Skjellthat as you start typing the characters, mg automatically searches as you type 160eb2e2a49Skjellthe characters. 161be803e14Sderaadt 162eb2e2a49SkjellTo continue searching the text you're looking for, type C-s to find the next 163eb2e2a49Skjellinstance. To search in reverse, type C-r instead of C-s. If you type C-s or 164eb2e2a49SkjellC-r twice, it will simply search for the last text that you searched for. 165be803e14Sderaadt 166eb2e2a49SkjellTo stop searching for text, simply use the cursor keys (or C-f, C-b etc.) or 167eb2e2a49SkjellC-g to stop the search operation. 168be803e14Sderaadt 169eb2e2a49Skjell>> Use C-s foo to search for "foo" in the text. You can use C-s again to 170eb2e2a49Skjell find other instances of foo in the file. 171be803e14Sderaadt 172eb2e2a49SkjellNote that if a word cannot be found, it will say Failing I-search: at the 173eb2e2a49Skjellbottom of the screen. Typing C-s again will wrap the search around from the 174eb2e2a49Skjelltop of the file and begin searching from there. 175be803e14Sderaadt 176eb2e2a49SkjellReplace Text 177eb2e2a49Skjell------------ 178be803e14Sderaadt 179eb2e2a49SkjellTo replace text, use M-%. You will be prompted for the text to search for and 180eb2e2a49Skjellthe text to replace it with. You will then be taken to the first instance of 181eb2e2a49Skjelltext from the current position. At this point you can do one of the following: 182be803e14Sderaadt 1832b13f599Slum y - Replace the text at this instance and search for more items. 1842b13f599Slum n - Skip this instance and search for more items. 185d2c9ceb5Slum . or Enter - Stop replacing text (you can also use C-g). 186eb2e2a49Skjell ! - Replace all the instances without prompting at each one. 187be803e14Sderaadt 188eb2e2a49Skjell>> Try replacing "frobnitz" with "zutwalt" on this line. 189be803e14Sderaadt 190eb2e2a49SkjellCut/Copy/Paste Text 191eb2e2a49Skjell------------------- 192be803e14Sderaadt 193eb2e2a49SkjellAs explained above, you can cut regions using C-k to kill multiple lines. To 194eb2e2a49Skjellpaste the text that you just cut, simply move your cursor to the point and 195eb2e2a49Skjellthen type C-y to restore the text. You may type C-y multiple times to restore 196eb2e2a49Skjellthe text. Hence, to copy text, you can use C-k to kill all the lines, use C-y 197eb2e2a49Skjellto restore it immediately, then move to the region you want to copy it to and 198eb2e2a49Skjellthen type C-y again to restore the last cut text block again. 199be803e14Sderaadt 200eb2e2a49SkjellAnother way to cut or copy chunks of text is to first position your cursor at 2018ee02a2dSlumthe starting point of the chunk of text. Then type C-<space> to mark this as 2028ee02a2dSlumthe starting point to cut or copy. Then move the cursor to the end point of the 203eb2e2a49Skjelltext chunk that you wish to manipulate. Then type C-w to cut the region, or 204eb2e2a49SkjellM-w to copy the region. If you wish to cancel marking a block of text, simply 205eb2e2a49Skjelltype C-g to cancel the operation. 206be803e14Sderaadt 207eb2e2a49SkjellTo paste the region that you've cut or copied above, simply move your cursor 208eb2e2a49Skjellto the desired location and then type C-y to paste it. 209be803e14Sderaadt 210eb2e2a49SkjellStatus Line 211eb2e2a49Skjell----------- 212be803e14Sderaadt 213eb2e2a49SkjellAt the bottom of your screen is a reverse highlighted line. This is the status 214eb2e2a49Skjellline and lets you know some useful information about the file you're editing. 215be803e14Sderaadt 216eb2e2a49SkjellOn the status line, you should see "Mg: tutorial". This lets you know that 217eb2e2a49Skjellyou're editing a file named "tutorial". If you've edited this file and not 218eb2e2a49Skjellsaved it, it should have a "**" to the left of those words. If this file is 219eb2e2a49Skjellread-only, you should see a "%%" to the left of those words. 220be803e14Sderaadt 2213ba9a4f5SbcallahTo the right of the status line, you should see L followed by digits 2223ba9a4f5Sbcallahand, if column-number-mode is enabled, C followed by some more digits. 2233ba9a4f5SbcallahType M-x column-number-mode Enter to enable it if it is disabled (the 2243ba9a4f5Sbcallahdefault). These indicate the line number and column number of the file 2253ba9a4f5Sbcallahthat your cursor is currently on. If you move the cursor around, you 2263ba9a4f5Sbcallahshould see the line and column number change. 227be803e14Sderaadt 228eb2e2a49SkjellIn the middle of the screen, you should see the word "(fundamental)" which 229eb2e2a49Skjellindicates that the current editing mode is "fundamental-mode". The mg editor 230eb2e2a49Skjellalso supports a c-mode that is more suited to editing C code. There are also 23146165f78Slumsome other useful editing modes for different situations. See the man page 232eb2e2a49Skjellfor mg(1) to learn about the various editing modes. 233be803e14Sderaadt 234eb2e2a49SkjellOpening and Saving Files 235eb2e2a49Skjell------------------------ 236be803e14Sderaadt 237eb2e2a49SkjellTo open a file, you can use C-x C-f. You will then be prompted for a file name. 238eb2e2a49SkjellIf you type a file name that doesn't already exist, a new file will be opened 239eb2e2a49Skjellfor you. If the file name already exists, then it will be opened for you and 240eb2e2a49Skjellyou can begin editing it. Note that you do not need to type the whole file 241eb2e2a49Skjellname for an existing file. You can type part of the file name and then press 242eb2e2a49Skjellthe TAB key. If there is only file name that matches, mg will fill in the rest 243eb2e2a49Skjellof the file name for you. If there are multiple files, mg will display that 244eb2e2a49Skjellthe choice is ambiguous. If you type the TAB key again, mg will show you all 245eb2e2a49Skjellthe available choices for file names. 246be803e14Sderaadt 247eb2e2a49SkjellNOTE: If you type C-x f instead of C-x C-f, you can use C-g to cancel the 248eb2e2a49SkjellSet-Fill-Column command. You can also use C-g to cancel the C-x C-f command 249eb2e2a49Skjellif you don't wish to open a new file. 250be803e14Sderaadt 251eb2e2a49SkjellTo save the file once you've edited it, use C-x C-s to save the file. When 252eb2e2a49Skjellmg is done saving the file, you should see the words "Wrote /path/to/file" 253eb2e2a49Skjellin the bottom of your screen. In general, it is a good idea to save quite 254eb2e2a49Skjelloften. When you save a file, mg saves a backup of the file with a tilde (~) 255eb2e2a49Skjellcharacter at the end. 256be803e14Sderaadt 257*31cab6d6SjsgIf you decide to open a directory instead of a file, mg will transition into a 258813cca85Slummode called dired. Dired fills a buffer with the contents of the selected 259813cca85Slumdirectory, one file or sub-directory's details per line. Some basic file 260813cca85Slummanagement functions can be performed on the files and sub-directories in the 261813cca85Slumbuffer. For example, with the cursor over a specific file: 262813cca85Slum 263813cca85Slum Pressing c will give you the opportunity to copy the file. 264813cca85Slum Pressing d will mark the file for deletion. 265813cca85Slum Pressing x will unlink files previously marked for deletion. 266813cca85Slum Pressing Return will open the highlighted file into it's own buffer for 267813cca85Slum editing. 268813cca85Slum 269813cca85SlumThere are more dired commands, see the man page for further information. 270813cca85Slum 27186c0e11fSlumWorking with Buffers and Windows 27286c0e11fSlum-------------------------------- 27390d518a5Slum 27486c0e11fSlumOnce a file is loaded into mg, it is often referred to as a buffer. 27586c0e11fSlum 27686c0e11fSlumThe mg editor is capable of editing multiple buffers at the same time. When you 27786c0e11fSlumopen a second file with C-x C-f, the first buffer is still being edited by mg. 27886c0e11fSlum 27986c0e11fSlumBoth buffers can be viewed simultaneously because mg can support several windows 28086c0e11fSlumat the same time, each one displaying different text. To split a screen into two 28186c0e11fSlumhorizontal windows use C-x 2. To return to one window, use C-x 1 to close the 28286c0e11fSlumother windows and only keep the current window. 28390d518a5Slum 28490d518a5Slum>> Use C-x 2 to split the screen into two windows. 28590d518a5Slum 28690d518a5Slum>> Use C-x o to move from one window to the other. You can scroll up and down 28790d518a5Slum in each window using the cursor keys or C-n and C-p keys. 28890d518a5Slum 28990d518a5Slum>> Use C-x 1 to restore back to one window. 29090d518a5Slum 291eb2e2a49SkjellYou can list all the buffers that are opened by mg by typing C-x C-b. The 292eb2e2a49Skjellscreen should divide into two and the top window will list the buffers that 29386c0e11fSlumare currently open. Use C-x o to switch to the top window, then use the arrow 29486c0e11fSlumkeys to move to the buffer you wish to switch to, and then type the Enter key to 295eb2e2a49Skjellselect that buffer. Then use C-x 1 to switch back to only one window. 296be803e14Sderaadt 297eb2e2a49SkjellYou may also move back to the last opened buffer by using C-x b to toggle back 298eb2e2a49Skjelland forth between two buffers. Note the difference between C-x b and C-x C-b. 299be803e14Sderaadt 300eb2e2a49Skjell>> Use C-x C-f to open a new file 301eb2e2a49Skjell>> Use C-x b to switch back and forth between that buffer and this one. 302be803e14Sderaadt 303eb2e2a49SkjellTo kill any buffer, use C-x k. You will be prompted for the buffer to kill. 304eb2e2a49SkjellBy default, the current buffer is selected as the one to kill. You may also 305eb2e2a49Skjelltype another buffer name or use C-g to cancel the operation. 306be803e14Sderaadt 307eb2e2a49SkjellExtended Commands 308be803e14Sderaadt----------------- 309be803e14Sderaadt 310eb2e2a49SkjellThe mg editor has several extended commands, more than what can be covered 311eb2e2a49Skjellby the Control and Meta keys. The mg editor gets around this by using what is 312eb2e2a49Skjellcalled the X (eXtend) command. There are two forms of this: 313be803e14Sderaadt 314eb2e2a49Skjell C-x Character eXtension. Followed by one character. 315eb2e2a49Skjell M-x Named character eXtension. Followed by a long command. 316be803e14Sderaadt 317eb2e2a49SkjellYou've already seen C-x C-f and C-x C-s to open and save a file. There are 318eb2e2a49Skjellother longer commands. For instance, you can also open a file by typing 31951585f3eStbM-x find-file Enter. When you type a command using M-x, mg prompts you for 320eb2e2a49Skjellthe command at the bottom of the screen. You can type out the whole command 321eb2e2a49Skjellif you wish, or you can type out part of the command and then use the TAB key 322eb2e2a49Skjellfor autocompleting the command. 323eb2e2a49Skjell 324eb2e2a49SkjellFor instance, to replace text, you can type M-x repl TAB enter to execute 325eb2e2a49Skjellthe replace-text command. To cancel this command, type C-g. 326eb2e2a49Skjell 32746165f78SlumTo see a list of all available mg(1) commands, consult the man page. 328eb2e2a49Skjell 329eb2e2a49SkjellExiting mg 330be803e14Sderaadt---------- 331be803e14Sderaadt 332eb2e2a49SkjellTo exit mg temporarily and return to the shell, use C-z. This will take you 333eb2e2a49Skjellback to the command shell. To return back to mg, type fg in the shell and you 334eb2e2a49Skjellwill be returned to your mg session. 335be803e14Sderaadt 336eb2e2a49SkjellTo exit mg permanently, type C-x C-c. If you have any unsaved buffers, you 337eb2e2a49Skjellwill be asked if you wish to save them or not. 338be803e14Sderaadt 339eb2e2a49SkjellConclusion 340eb2e2a49Skjell---------- 341be803e14Sderaadt 342eb2e2a49SkjellThis tutorial is meant to get new users up and running with mg. There is more 34346165f78Sluminformation available via the mg(1) man page. If you have any suggestions for 344eb2e2a49Skjellimprovement, please don't hesitate to drop a message or (better still) submit 34546165f78Sluma diff to tech@openbsd.org. 346eb2e2a49Skjell 347eb2e2a49SkjellAuthor Info 348eb2e2a49Skjell----------- 349be803e14Sderaadt 350eb2e2a49SkjellOriginal Author of this document: Mayukh Bose, 3513ba9a4f5SbcallahDate last updated: 2018-05-27 352be803e14Sderaadt 353eb2e2a49SkjellCopyright 354eb2e2a49Skjell--------- 355be803e14Sderaadt 356eb2e2a49SkjellNone. This document is in the public domain. 357