Project30.Edit with nano


Project 30. Edit with nano

"What's the quickest route to take when I want to learn how to edit text files in Unix?"

This project introduces the nano text editor. It's very easy to learn and sufficiently powerful for you to use to write shell scripts and as your everyday text editor. Versions of OS X prior to 10.4 (Tiger) did not include nano. Earlier versions of OS X instead included an editor called pico, which lacked some basic features such as search and replace. See the sidebar "Get nano Infoand nano Itself" to learn how to install nano in pre-10.4 versions of Mac OS X.

Basics

Fire up nano, and we'll take a look at the basics. You should find a file called index.html in the directory Sites in your home directory. Make a copy of it to practice on, or use any other text file.

$ cd ~/Sites $ cp index.html index-new.html $ nano index-new.html


You'll see a terminal window similar to that shown in Figure 4.1. The top line shows the name of the file being edited; the version of nano is to the left; and to the right, you'll see the word Modified if you have modified the file. The bottom two lines are there to remind you of the basic commands, and the line above them is the status bar, showing important and informational messages. The caret symbol (^) means "press and hold Control," so to see the help screen, for example, press ^g or Control-g.

Figure 4.1. The nano text editor displays relevant keystroke options at the bottom of its main window. The status bar just above the command menu provides user feedback.


Optional nano features can be toggled on and off to change certain aspects of the editor. To enable a feature, press the Escape key and then press the single letter that represents that feature. (Don't hold down the Escape key, as you would the Control key.) Do the same to disable an enabled feature. The most useful features are covered in later sections.

Features in nano are disabled by default, but can also be enabled using the command line: Include a feature's option in a command line that invokes nano, and the feature will be activated when nano launches. Check nano's man page for full details.

Tip

The Page Up and Page Down keys scroll Apple's Terminal. To get nano to scroll the document, use Shift-Page Up and Shift-Page Down. Similarly, use Shift-Home and Shift-End to move to the start and end of the current line.

If you use X11's xterm as your terminal, Page Up and Page Down work without the Shift key, but Home and End do not work at all.


Move Around

The cursor (arrow) keys work in the normal manner in nano and are the most common means of moving the text-insertion point. A nano feature also lets you position the cursor with your mouse, but unfortunately, it works in X11's xterm but not Apple's Terminal application. Even if it won't help you, activate it by typing Escape m; then type Escape m again to turn mouse support off. When you do either, note that the status line displays a message confirming that the setting has changed.

If you use Apple's Terminal, you can still get mouse support in nano (and other Unix programs as well). Launch the Terminal Inspector by choosing Terminal > Window Settings; select Emulation from the popup menu, and check Option-Click to Position Cursor. Option-clicking in nano causes Terminal to send nano a series of cursor-movement keystrokes that move the cursor to the point where you clicked.

Tip

The cursor can also be moved with Control sequences, as described in the nano help screen.


Get Help

Press Control-g to display a help screen. The help screen uses the caret symbol (^) to mean "press and hold the Control key" and M to mean "press the Escape key" (also referred to as the Meta key). Press Shift-Page Up and Shift-Page Down to move through the help screen, and press Control-x to exit it.

Edit File Contents

Basic text addition and removal are straightforward in nano. To insert text, place the cursor at the location where you want your addition to begin. The Delete key (often referred to as the Backspace key) deletes the character behind the cursor. Forward-Delete (the key above the left arrow on full-size Mac keyboards, emulated by pressing Fn-Delete on laptops) deletes the character under the cursor.

Get nano Infoand nano Itself

The official site for nano, www.nano-editor.org, offers much useful information.

Most important for users of pre-10.4 releases of Mac OS X, which didn't include nano, the site provides free downloads of the program.

Download the source by clicking the "Get Nano" link at the bottom of the page, then clicking the file nano-[versionnumber].tar.gz. Type the following lines in the Terminal window (the version number may differ from the one shown here).

$ tar xzf nano-¬     1.2.5.tar.gz $ cd nano-1.2.5/ $ ./configure   # ...much output here.... $ make $ sudo make install Password:   # ...output here...



Cut and Paste

In nano, cut and paste work on whole lines only. Use

  • Control-k to cut a line.

  • Control-u to paste a line.

  • Control-k Control-u to copy a line. (This, of course, cuts and repastes the line in question but also retains the cut text in a memory buffer so it can be pasted again.)

If you use Terminal, you can also employ the usual Mac method of selecting text and pressing Command-c to copy it and Command-v to paste it. It's not possible to cut text by using Command-x, however.

Save Your Edits

To save changes to the current file press Control-o and then Return. The status line displays the filename to save to; you may edit it before pressing Return, or type a name for a new file if you invoked nano with no filename.

Pressing Control-x exits nano, and if your document has unsaved modifications, you'll be prompted to save or discard the changes first.

Tip

After pressing Control-w, press Cursor Up to recall previous search terms and then Return to execute the search.


Search and Replace

You can use nano to search and search and replace. In addition, switching on features can make the search case sensitive, enable regular-expression matching, and reverse the direction of the search.

To search the current file for some particular text, press Control-w. You'll see the prompt Search: displayed in the status bar. At the prompt, type the term to search for, press Return to find the first occurrence of the term. Press Control-w and then Return to find subsequent matches.

Type Escape c to switch case-sensitive matching on and off. (The feature is off by default when searching is activated.) You'll notice that the status-bar prompt changes to reflect that searches are now case sensitive. It's important to understand that this feature can be toggled only after pressing Control-w to enter search mode and that it remains active across searches until switched off. At other times, you'll recall, typing Escape c toggles the cursor-position display in the nano status bar.

Similarly, type Escape b to reverse the direction of the search.

Tip

If you download nano using the instructions in the "Get nano Infoand nano Itself" sidebar on page 190, the executable will be installed in /usr/local/bin/nano and the man pages in /usr/local/man/. If /usr/local/bin is not in your search path, you will either need to add it or else invoke nano by using its full pathname. See Project 50 to learn about search paths.


Replace

To search and replace, press Control-\. This time, you'll see the prompt Search (to replace): displayed in the status bar. Type the search term and press Return. The prompt changes to Replace with:, at which point type the new string and press Return. The replacement is interactive, so you may accept or reject each occurrence as a candidate for replacement, or have all occurrences replaced. Just follow the instructions displayed on the bottom two lines.

You can also activate the replace feature from within search mode (after you press Control-w) by pressing Control-r.

Learn More

See projects 77 and 78 to learn more about extended regular expressions.


Use Regular Expressions

Normally, nano interprets search terms as simple text, not as patterns to match, but enabling its regexp feature lets you use regular expressions as search terms. Type Escape r from within search (or search-and-replace) mode. You'll notice that the status-bar prompt changes to reflect that the search term can now be a regular expression. This feature remains active across searches until switched off. Note that nano implements extended regular expressions, not just basic regular expressions.

Match Brackets

It's often useful, especially when writing code, to jump back and forth between pairs of brackets. Bracket-matching is built into nano. Place the cursor on any bracket character( [ { or <and type Escape ]. The cursor will move to the matching closing bracket. Type Escape ] again, and the cursor returns to the matching open bracket. Try it on the mouth of a "smiley":-)to see what happens when a bracket has no match.

Edit Multiple Files

Type Escape f to enable the multiple-buffers feature, which lets you open many files in a single nano session. You'll see a message in the status bar giving the current setting for this feature. Each file being edited is held in a separate buffer, and nano displays one buffer at a time. To load a second file, press Control-r; then type the name of the file to edit in the status bar and press Return. The old file will vanish, and the new file will be presented in the terminal window.

The old file is not lost; it's just no longer displayed. To display a hidden file type Escape > for the next buffer or Escape < for the previous buffer. You need not press the Shift key, so Escape , (comma) and Escape . (period) work too.

Note that if the multiple-buffers feature is not enabled, typing Control-r and then a filename reads the new file and inserts its contents into the current file.

Tip

When using nano 's tabbed completion feature, type ../ and then press Tab two times to list files in the parent directory.


Use Tabbed Completion

Use tabbed completion when entering a filename in the status bar, just as you would in the command line. Start typing a filename; then press Tab. If what you have typed is unambiguous, nano will complete the filename for you. If what you have typed is ambiguous, pressing Tab a second time will reveal a list of possible matches in the main window.

Use File Search

Whenever you enter a filename in the status bar, press Control-t to reveal a directory listing in the main window. You may now use the cursor keys to select a file to load and press Return to load it. Selecting a directory enters that directory and causes its contents to be displayed. To move to the parent directory, select the .. (dot-dot) entry and press Return.

Format Text

Several nano features help you format code and text. Type

  • Escape i to enable autoindenting. From now on, if you indent a line with spaces or tabs, subsequent lines will be indented automatically by the same amount.

  • Escape w to enable autowrap. Long lines will be broken automatically before they exceed the width of the terminal screen. nano inserts a line break and always does so at white space or punctuationnever midword.

Pressing Control-j tells nano to wrap a series of long lines. All consecutive lines above and below the cursor are considered for wrapping. When a blank line is encountered, wrapping stops.

nanorc Configuration File

When nano starts up, it reads a configuration file from your home directory. This file, which defines default settings for nano, must be named .nanorc and placed in your home directory. All the features that can be enabled on the command line or by escape sequences within nano can also be enabled in the configuration file.

To have multiple-buffers mode enabled by default, for example, add the line

set multibuffer


to ~/.nanorc

Other useful settings are

  • set fill n to wrap lines at column n

  • set tabsize n to set the width of each tab stop to n

  • syntax and color to define syntax highlighting colors (see the next section)

For full details, see the nanorc man page by typing

$ man 5 nanorc


Syntax Highlighting

Like all good text editors, nano is capable of syntax highlighting. We can tell it to display HTML tags such as <head> in blue, for example, and escaped characters such as &amp; in red. Enter the following in your nanorc file.

#HTML Syntax Highlighting syntax "HTML" "\.html$" color blue start="<" end=">" color red "&[^;      ]*;"


Note: On the fourth line, the text between [ and ] includes a space character and a tab character.

The first line is a comment and is ignored by nano. The second line says that the HTML syntax is applied to all files whose names end with .html. The third line says that everything between < and > (possibly across multiple lines) is to be colored blue, and the fourth line says that everything between & and ; (when they are not separated by white space or semicolons) is to be colored red.

You may specify any of the colors white, black, red, blue, green, yellow, magenta, and cyan, and prefix any color name with the word bright. The string to match-and-color is specified with an extended regular expression. See projects 77 and 78 to learn more about extended regular expressions.

Try loading an HTML file, such as ~/Sites/index.html, into nano, and you should see the syntax highlighting described above in action.

Download the nano source files (see the sidebar "Get nano Infoand nano Itself"), and you'll find a file called nanorc.sample. This file gives examples of what can be placed in the configuration file and how to write syntax highlighting instructions.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net