Using Common Editors If you've ever had to edit text for any length of time, you've probably got a favorite text editor that you know inside and out.Unless you're a longtime Unix user, your favorite editor probably isn't from the vi or EMACS families, the two most common types of Unix editors.There's probably a great Unix GUI text editor that closely matches your favorite, but what if you need to edit a file on a system with no display? Or over a slow remote connection? Or (horror!) what if your favorite text editor isn't installed yet and you need to change its Makefile?Knowing how to use vi on a Unix system is similar to knowing how to start a fire by rubbing two sticks together. Even if you don't do it very often, it can really save you in a pinch.We'll also take a quick look at EMACS, a friendlier editor installed on most Unix systems.Even though EMACS is far superior to vi, we don't waste any time on the eternal "my editor is better than yours" debate. If you're interested, check the Wikipedia ( http://en.wikipedia.org/wiki/Editor_war) entry!vi The vi editor is one of the first screen-oriented text editors; before vi, most text editors were line oriented, displaying only one line at a time for editing. Bill Joy created vi in 1976; he's done a little work for Sun since then on a minor project called Java. Tips- If for some reason vi isn't available on your system, you can try one of the vi clones, such as vim or elvis. In fact, on most Linux systems, vi is just a link to vim.
- vim sports some other modern features, like syntax highlighting. Even if vi is available, you might want to try typing vim to see if something better is available.
- vim has extensive builtin help that you can access using the :help command.
To edit files with vi
To exit vi Do any of the following:- :qQuit.
- :q!Quit even if the file has been edited.
- :xSave the current file and exit.
- :x!Save the current file even if it's readonly, and then exit.
To switch to vi's command mode
- Press the Esc key to switch to vi's command mode.The vi editor is modal, meaning that you can be entering text or entering commands. You need to switch between input mode and command mode manually. When you first start vi, you're automatically in command mode.
Tips- To signal the end of a multiple-character command, press Enter.
- If you're already in command mode when you press Esc, you'll stay in command mode.
- You must be in command mode to use any of the commands listed here.
To switch to vi's input mode Do any of the following:- iEnter input mode at the current cursor position.
- o (lowercase o)Insert a blank line below the current cursor position, and enter input mode at the beginning of that new line.
- O (uppercase O)Insert a blank line above the current cursor position, and enter input mode at the beginning of that new line.
To move around in vi Do any of the following:- jMove the cursor down one line.
- kMove the cursor up one line.
- lMove the cursor left one character.
- hMove the cursor right one character.
- :<digits>Move the cursor to the start of the line specified by <digits>. For example, entering :1 will move you to the first line of a file, and :99 will move you to line 99 (or the end of the file if it doesn't have more than 99 lines).
- [[Move the cursor to the start of the first line of the current buffer.
- ]]Move the cursor to the start of the last line of the current buffer.
- ^Move the cursor to the start of the current line.
- $Move the cursor to the end of the current line.
To change buffers in vi Do either of the following:- :nSwitch to the next file.
- :NSwitch to the previous file.
To search in vi Do any of the following:- /<pattern>Search forward for <pattern> and move the cursor to the start of the matched text. For example, /abc will move the cursor to the a in the next instance of abc within the text.
- /Search forward for the next instance of the last pattern you searched for. This is a multiple-character command, with the search pattern omitted, so you have to press Enter right after entering the / character.
- ?<pattern>Search backward for <pattern> and move the cursor to the start of the matched text. For example, /abc will move the cursor to the a in the previous instance of abc within the text.
- ?Search backward for the previous instance of the last pattern you searched for. This is a multiple-character command, with the search pattern omitted, so you have to press Enter right after entering the ? character.
EMACS EMACS started life as a set of macros for another ancient text editor before evolving into a full-blown text editor with extensions for almost anything you can think of. It's practically an operating system disguised as a text editor. Its name stands for Editor MACroS.EMACS isn't a modal editor like vi, meaning that you can be entering text or commands without having to switch between modes. EMACS commands involve one or more keys used with one of the modifier keys on your keyboard, the most common being Ctrl and Meta.Waityou've never seen a keyboard with a Meta key, right? Neither have I; they seem to have disappeared into the mists of time. In EMACS, the Meta key is either the Esc key (when running the textmode version) or the Alt key (when running a GUI version). If the Alt key works as Meta, the Esc key will also work, so if you've learned to use the textonly EMACS, you can keep using your EMACS skills when running a GUI version.In EMACS documentation, the Ctrl key is referred to as C-, so C-G would be the same as Ctrl-G. The Meta key is referred to as M-, so M-X would be the same as Esc-X (or Alt-X). Note that using the Esc key as Meta works a little differently. It's not a "special" key like Ctrl or Shift, so you don't hold it down. Press Esc once, let go, then press the next character in the command sequence. Commands that need uppercase characters are shown using the actual character instead of specifying the Shift key.Some commands require a sequence of keys, such as Ctrl-H t, which starts the interactive EMACS tutorial. When you see C-H t, it means to press Ctrl-H, let go, then press T (which gives you a lowercase t because you haven't pressed Shift).I'm going to refer to EMACS commands using this notation so that it matches up with what you'll see in the actual EMACS documentation and builtin online help. Tips- You can press Tab any time EMACS asks you for a filename, buffer name, command name, and so on, and it will provide a list of possible entries.
- If you enter some text before pressing Tab, the completion list will only include things that matched the text you entered. For example, if you press C-X b to switch buffers, and then type hel Tab, only buffers starting with hel will show up in the list, such as hello_world.c or helen-of-troy.
- EMACS has a builtin tutorial that you can pull up from the Help menu, or by pressing C-H t.
To edit files with EMACS Do any of the following:
To exit EMACS
- C-X C-C (Ctrl-X and then Ctrl-C)Exit EMACS. If you've made changes to a buffer without saving it, EMACS will prompt you to save it first by pressing Y or N. If you don't save (by pressing N), you will have to type yes (the entire word) to exit without saving your changes, or no to stay in EMACS.
To save files with EMACS Do either of the following:- C-X C-SSave the current buffer to its default filename.
- C-X C-WWrite the current buffer to a file; EMACS will prompt you for the filename in the mini-buffer at the bottom of the screen. Type a filename and press Enter to save the file.
To move around in EMACS Do any of the following:- C-NDown (next line).
- C-PUp (previous line).
- C-BLeft (back).
- C-FRight (forward).
- M-X goto-line <digits> EnterMove the cursor to the start of the line specified by <digits>. For example, pressing M-X, typing goto-line 1, and then pressing Enter will move you to the first line of a file.
- M-<Move the cursor to the start of the first line of the current buffer.
- M->Move the cursor to the start of the last line of the current buffer.
- C-VMove down one screen.
- C-AMove the cursor to the start of the current line.
- C-EMove the cursor to the end of the current line.
- M-VMove up one screen.
Tip- The cursor keys, Page Up, Page Down, and so on, will also work as expected, if your terminal maps them to something EMACS can understand.
To change buffers in EMACS Do any of the following:- C-X b EnterSwitch to the default buffer. If you haven't switched buffers yet, this will be the next file or buffer. If you've already switched buffers, the default will be the previous buffer.
- C-X b TabList the names of all of the current buffers. A buffer's name is the same as its filename. EMACS's own temporary buffers have names like *scratch* so that you can easily identify them.
- C-X b <name> EnterSwitch to the buffer named <name>. For example, C-X b *scratch* Enter will switch to the scratch buffer.
To search in EMACS Do either of the following:- C-SInteractively search forward for the next characters you type. To search again for the same character sequence, press C-S again.
- C-RInteractively search backward for the next characters you type. To search backward again, press C-R again.
 |