User Tools

Site Tools


emacs_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
emacs_tutorial [2021/03/21 06:11] – [Search & Replace] hc9emacs_tutorial [2021/03/21 06:38] (current) – [Basic Commands] hc9
Line 31: Line 31:
 | C-l | Redraw and center screen at cursor | | C-l | Redraw and center screen at cursor |
 | M-< | Go to start of buffer | | M-< | Go to start of buffer |
-| M-> | Go to end of buffer |+| M-<nowiki>></nowiki> | Go to end of buffer |
 | Backspace | Delete previous character | | Backspace | Delete previous character |
  
Line 86: Line 86:
 ===== Emacs Modes ===== ===== Emacs Modes =====
  
-Emacs buffers are associated with one so-called //major mode// and one or more //minor modes//. Major modes typically denote a type of file, and are associated with special command sequences and syntax highlighting just for that file type. Minor modes change the behavior of an associated major mode in small ways. For example, the major mode for editing text is called text-mode, while the one for editing HTML is called html-mode. Both text and html modes have a minor mode called refill-mode that automatically formats paragraphs of text as you type (similar to the behavior you expect from a word processor). You can switch modes by just typing "M-x modename", so "M-x html-modewould switch you into html-mode. Emacs can usually figure out the mode to use by the file extension, so if you visit (load with "C-x C-f") a file ending in .html or .htm, html-mode will be selected for you, and if you visit a file ending in .txt, text mode will be selected.+Emacs buffers are associated with one so-called //major mode// and one or more //minor modes//. Major modes typically denote a type of file, and are associated with special command sequences and syntax highlighting just for that file type. Minor modes change the behavior of an associated major mode in small ways. For example, the major mode for editing text is called text-mode, while the one for editing HTML is called html-mode. Both text and html modes have a minor mode called refill-mode that automatically formats paragraphs of text as you type (similar to the behavior you expect from a word processor). You can switch modes by just typing ''M-x modename'', so ''M-x html-mode'' would switch you into html-mode. Emacs can usually figure out the mode to use by the file extension, so if you visit (load with ''C-x C-f'') a file ending in .html or .htm, html-mode will be selected for you, and if you visit a file ending in .txt, text mode will be selected.
  
-As an example, say you want to create a new text document. You type "C-x C-f foo.txtand Emacs creates an empty buffer for you named "foo.txt". Notice the modeline displays "(Text)" to indicate the major mode. Now type "M-x refiand hit the Tab key. Emacs will complete "refill-mode". Hit enter, now the modeline displays "(Text Refill)" to indicate the major and minor modes in effect. Experiment with refill mode by typing some text and watch as Emacs wraps the lines for you. Now go back and add or delete some text and notice how the paragraph re-formats itself. Modes toggle on and off, so a second "M-x refill-modewill turn refilling off. Type some text and notice Emacs no longer wraps lines or formats paragraphs for you. The mode for buffers not associated with any special file type is called fundamental-mode. This is the mode your scratch buffer is put in at startup.+As an example, say you want to create a new text document. You type ''C-x C-f foo.txt'' and Emacs creates an empty buffer for you named "foo.txt". Notice the modeline displays "(Text)" to indicate the major mode. Now type ''M-x refi'' and hit the Tab key. Emacs will complete "refill-mode". Hit enter, now the modeline displays "(Text Refill)" to indicate the major and minor modes in effect. Experiment with refill mode by typing some text and watch as Emacs wraps the lines for you. Now go back and add or delete some text and notice how the paragraph re-formats itself. Modes toggle on and off, so a second ''M-x refill-mode'' will turn refilling off. Type some text and notice Emacs no longer wraps lines or formats paragraphs for you. The mode for buffers not associated with any special file type is called fundamental-mode. This is the mode your scratch buffer is put in at startup.
  
-There are lots of Emacs major modes, many of which are useful for programmers. There is c-mode, perl-mode, and c++-mode, for example. Each mode has its own key bindings and syntax highlighting rules. You can see the details of the currently selected major mode by typing "C-h m". To enable syntax highlighting on a source-code file, you can type "M-x global-font-lock-mode(on some operating systems, packaged versions of Emacs will enable font lock [syntax highlighting] for you automatically).+There are lots of Emacs major modes, many of which are useful for programmers. There is c-mode, perl-mode, and c++-mode, for example. Each mode has its own key bindings and syntax highlighting rules. You can see the details of the currently selected major mode by typing ''C-h m''. To enable syntax highlighting on a source-code file, you can type ''M-x global-font-lock-mode'' (on some operating systems, packaged versions of Emacs will enable font lock [syntax highlighting] for you automatically).
  
-As an example of special key bindings, in html-mode, the key sequence "C-c C-c hwill insert a properly formatted hyperlink into the text, prompting you for the URL first. In other modes this key sequence will have no effect.+As an example of special key bindings, in html-mode, the key sequence ''C-c C-c h'' will insert a properly formatted hyperlink into the text, prompting you for the URL first. In other modes this key sequence will have no effect.
  
 Here are some of the most useful mode commands: Here are some of the most useful mode commands:
Line 103: Line 103:
 ===== Buffers ===== ===== Buffers =====
  
-When you visit a file with "C-x C-f", Emacs loads the file into a new buffer created just for that file. Existing buffers are not destroyed, but persist and can be switched to with "C-x b". Emacs will prompt you for a buffer name to switch to; tab-completion works here as it does in other places. One tip that I wish I had known when I first started using Emacs is to use a special mode called //iswitch mode// to help manage buffers. Since it's not uncommon to have dozens of buffers in an Emacs editing session, you can easily forget buffer names. With iswitch mode enabled, a "C-x bdisplays a list of buffers in the echo area that changes in real-time as you type characters. You don't even have to type the first few letters of a buffer name, any substring of a buffer name will do. To enable iswitch mode, type the extended command "M-x iswitchb-mode(we'll see later how to permanently enable modes like this in a startup file). To see a list of all buffers, type "C-x C-b". The buffer list will appear in a new window. Here is a list of some useful buffer commands:+When you visit a file with ''C-x C-f'', Emacs loads the file into a new buffer created just for that file. Existing buffers are not destroyed, but persist and can be switched to with ''C-x b''. Emacs will prompt you for a buffer name to switch to; tab-completion works here as it does in other places. One tip that I wish I had known when I first started using Emacs is to use a special mode called //iswitch mode// to help manage buffers. Since it's not uncommon to have dozens of buffers in an Emacs editing session, you can easily forget buffer names. With iswitch mode enabled, a ''C-x b'' displays a list of buffers in the echo area that changes in real-time as you type characters. You don't even have to type the first few letters of a buffer name, any substring of a buffer name will do. To enable iswitch mode, type the extended command ''M-x iswitchb-mode'' (we'll see later how to permanently enable modes like this in a startup file). To see a list of all buffers, type ''C-x C-b''. The buffer list will appear in a new window. Here is a list of some useful buffer commands:
  
 | C-x b | Switch to another buffer | | C-x b | Switch to another buffer |
Line 112: Line 112:
 ===== Windows ===== ===== Windows =====
  
-The Emacs screen area can be divided into multiple //windows//. Each window contains one buffer, so the contents of a window can change depending on the buffer it contains. The most common way to create new windows is to split the screen into two regions with "C-x 2or "C-x 3". The first splits the screen in half horizontally, the second splits it vertically. Any Emacs window can be split multiple times, so if you have a large display, you could have lots of windows open. You can cycle through visible windows with "C-x o(think of the "o" as meaning "other window"). When you do have another window open, it's sometimes useful to scroll the other window without leaving your current one. You can do this with "C-M-v". This is particularly useful for when Emacs pops up a completion or help buffer in a new window that you would like to scroll through.+The Emacs screen area can be divided into multiple //windows//. Each window contains one buffer, so the contents of a window can change depending on the buffer it contains. The most common way to create new windows is to split the screen into two regions with ''C-x 2'' or ''C-x 3''. The first splits the screen in half horizontally, the second splits it vertically. Any Emacs window can be split multiple times, so if you have a large display, you could have lots of windows open. You can cycle through visible windows with ''C-x o'' (think of the "o" as meaning "other window"). When you do have another window open, it's sometimes useful to scroll the other window without leaving your current one. You can do this with ''C-M-v''. This is particularly useful for when Emacs pops up a completion or help buffer in a new window that you would like to scroll through.
  
-You can close a window with "C-x 0or "C-x 1". The first closes the window you are currently in, the second closes all the other windows, but leaves the window you are currently in open for you. Closing a window does not destroy the buffer it contains, so you can think of a window as a view into a buffer (in fact you can have multiple windows visiting different parts of the same buffer). Here is a list of the most useful window commands:+You can close a window with ''C-x 0'' or ''C-x 1''. The first closes the window you are currently in, the second closes all the other windows, but leaves the window you are currently in open for you. Closing a window does not destroy the buffer it contains, so you can think of a window as a view into a buffer (in fact you can have multiple windows visiting different parts of the same buffer). Here is a list of the most useful window commands:
  
 | C-x 0 | Close this window | | C-x 0 | Close this window |
Line 125: Line 125:
 ===== Dired Mode ===== ===== Dired Mode =====
  
-If you type the name of a directory after a "C-x C-fcommand, Emacs will display the directory contents in a new buffer. By default, Emacs displays the file name, permissions, owner/group, size and timestamp of each file on a line by itself. You can visit a file by just moving the cursor to it and hitting "Enter". Dired mode is unlike other modes in that single keys are used to effect commands. Here is a list of the most useful dired commands:+If you type the name of a directory after a ''C-x C-f'' command, Emacs will display the directory contents in a new buffer. By default, Emacs displays the file name, permissions, owner/group, size and timestamp of each file on a line by itself. You can visit a file by just moving the cursor to it and hitting ''Enter''. Dired mode is unlike other modes in that single keys are used to effect commands. Here is a list of the most useful dired commands:
  
 | Left, right arrow keys or p, n | Previous and next file, respectively | | Left, right arrow keys or p, n | Previous and next file, respectively |
Line 146: Line 146:
 ===== Shell Modes ===== ===== Shell Modes =====
  
-It is possible to run a shell from within Emacs; there are a few different ways to do so. The easiest way is to type "M-x shell". This creates a a buffer called *shell* with a shell prompt. Shell commands you type are sent to a system shell and the output displayed in the shell buffer. One thing you'll notice in shell mode is that the arrow keys move you around the buffer - they don't access command history as they do in most shells. To access command history, use "M-pand "M-nfor previous and next, respectively.+It is possible to run a shell from within Emacs; there are a few different ways to do so. The easiest way is to type ''M-x shell''. This creates a a buffer called *shell* with a shell prompt. Shell commands you type are sent to a system shell and the output displayed in the shell buffer. One thing you'll notice in shell mode is that the arrow keys move you around the buffer - they don't access command history as they do in most shells. To access command history, use ''M-p'' and ''M-n'' for previous and next, respectively.
  
-One of the drawbacks of shell-mode is that it is not suitable for full-screen applications (like less, lynx, mutt or pine), to run these inside of Emacs, you need to use a terminal mode. Type "M-x ansi-term", and specify which shell you would like to run (your default will be the shell you are using, e.g. "/usr/pkg/bin/bash").+One of the drawbacks of shell-mode is that it is not suitable for full-screen applications (like less, lynx, mutt or pine), to run these inside of Emacs, you need to use a terminal mode. Type ''M-x ansi-term'', and specify which shell you would like to run (your default will be the shell you are using, e.g. ''/usr/pkg/bin/bash'').
  
-On platforms without an underlying shell (e.g., Windows), Emacs has a terminal emulator written entirely in Emacs Lisp. To enter it, type "M-x eshell". While not suitable for full-screen applications, it does emulate a limited subset of shell features rather nicely. In this shell mode, the arrow keys work as you would expect, and most shell builtins and shell aliases work as well. Some external commands such as grep work, although input/output redirection is not yet implemented.+On platforms without an underlying shell (e.g., Windows), Emacs has a terminal emulator written entirely in Emacs Lisp. To enter it, type ''M-x eshell''. While not suitable for full-screen applications, it does emulate a limited subset of shell features rather nicely. In this shell mode, the arrow keys work as you would expect, and most shell builtins and shell aliases work as well. Some external commands such as grep work, although input/output redirection is not yet implemented.
  
 Here are the commands we discussed above: Here are the commands we discussed above:
Line 160: Line 160:
 ===== Emacs Startup and Configuration ===== ===== Emacs Startup and Configuration =====
  
-When Emacs starts it will load a file named ".emacsin your home directory and execute the commands found in it. This file is written in Emacs Lisp, but you can use it without knowing any Lisp, by copying other people's sample files. It is mainly useful for making certain extended commands a permanent part of your Emacs experience, so you don't have to type them every time you start Emacs. In the example below, comment lines start with one or more semi-colons, so you would delete the semi-colons to enable the given feature.+When Emacs starts it will load a file named ''.emacs'' in your home directory and execute the commands found in it. This file is written in Emacs Lisp, but you can use it without knowing any Lisp, by copying other people's sample files. It is mainly useful for making certain extended commands a permanent part of your Emacs experience, so you don't have to type them every time you start Emacs. In the example below, comment lines start with one or more semi-colons, so you would delete the semi-colons to enable the given feature.
  
 <file config .emacs> <file config .emacs>
Line 204: Line 204:
 </file> </file>
  
-The above example gives you a good idea of what can be done in a ".emacsfile, for more in-depth configuration Emacs has a customization mode that can be accessed with the extended command "M-x customize". Running it will put you in a curses-style application where you can choose from customization menus and have your changes written to your ".emacsfile automatically.+The above example gives you a good idea of what can be done in a ''.emacs'' file, for more in-depth configuration Emacs has a customization mode that can be accessed with the extended command ''M-x customize''. Running it will put you in a curses-style application where you can choose from customization menus and have your changes written to your ''.emacs'' file automatically.
  
 ===== Browsing the Web in Emacs with w3m ===== ===== Browsing the Web in Emacs with w3m =====
  
-[[http://sourceforge.net/projects/w3m/files/|Emacs w3m mode]] will use the excellent [[http://w3m.sourceforge.net|w3m]] text-mode browser and allow you to surf the web in an Emacs buffer. To get started, add the following to your ".emacsfile:+[[http://sourceforge.net/projects/w3m/files/|Emacs w3m mode]] will use the excellent [[http://w3m.sourceforge.net|w3m]] text-mode browser and allow you to surf the web in an Emacs buffer. To get started, add the following to your ''.emacs'' file:
  
 <code> <code>
Line 214: Line 214:
 </code> </code>
  
-Then restart emacs, or place the cursor at the end of each line in turn and press "C-x e". Then you can open a new w3m buffer with "M-x w3m". The basic key bindings are as follows (note that these are taken from the w3m-mode help page, which can be accessed at any time in a new window with "C-h m"):+Then restart emacs, or place the cursor at the end of each line in turn and press ''C-x e''. Then you can open a new w3m buffer with ''M-x w3m''. The basic key bindings are as follows (note that these are taken from the w3m-mode help page, which can be accessed at any time in a new window with ''C-h m''):
  
 | M-x w3m | Start browsing web with emacs-w3m. | | M-x w3m | Start browsing web with emacs-w3m. |
Line 249: Line 249:
 | M-a | Add the url under point to a new bookmark. | | M-a | Add the url under point to a new bookmark. |
  
-There are many more features in w3m-mode, the mode help text details them all with keybindings. To end your session and close all w3m buffers, just press "Qand answer "ywhen prompted.+There are many more features in w3m-mode, the mode help text details them all with keybindings. To end your session and close all w3m buffers, just press ''Q'' and answer ''y'' when prompted.
  
 ===== Where to Go From Here ===== ===== Where to Go From Here =====
emacs_tutorial.1616307107.txt.gz · Last modified: 2021/03/21 06:11 by hc9