Читать книгу Linux Bible - Christopher Negus - Страница 98

TIP

Оглавление

If you prefer the vi command for editing shell command lines, you can easily make that happen. Add the following line to the .bashrc file in your home directory:

  set -o vi

The next time you open a shell, you can use vi commands to edit your command lines.

To do the editing, you can use a combination of control keys, meta keys, and arrow keys. For example, Ctrl+F means to hold down the Ctrl key, and type f. Alt+F means to hold down the Alt key, and type f. (Instead of the Alt key, your keyboard may use a Meta key or the Esc key. On a Windows keyboard, you can use the Windows key.)

To try out a bit of command-line editing, enter the following:

 $ ls /usr/bin | sort -f | less

This command lists the contents of the /usr/bin directory, sorts the contents in alphabetical order (regardless of case), and pipes the output to less. The less command displays the first page of output, after which you can go through the rest of the output a line (press Enter) or a page (press spacebar) at a time. Simply press q when you are finished. Now, suppose that you want to change /usr/bin to /bin. You can use the following steps to change the command:

1 Press the up arrow (↑) key. This displays the most recent command from your shell history.

2 Press Ctrl+A. This moves the cursor to the beginning of the command line.

3 Press Ctrl+F or the right arrow (→) key. Repeat this command a few times to position the cursor under the first slash (/).

4 Press Ctrl+D. Type this command four times to delete /usr from the line.

5 Press Enter. This executes the command line.

As you edit a command line, at any point you can type regular characters to add those characters to the command line. The characters appear at the location of your text cursor. You can use right → and left ← arrows to move the cursor from one end to the other on the command line. You can also press the up ↑ and down ↓ arrow keys to step through previous commands in the history list to select a command line for editing. (See the section “Command-line recall” for details on how to recall commands from the history list.) You can use many keystrokes to edit your command lines. Table 3.1 lists the keystrokes that you can use to move around the command line.

TABLE 3.1 Keystrokes for Navigating Command Lines

Keystroke Full Name Meaning
Ctrl+F Character forward Go forward one character.
Ctrl+B Character backward Go backward one character.
Alt+F Word forward Go forward one word.
Alt+B Word backward Go backward one word.
Ctrl+A Beginning of line Go to the beginning of the current line.
Ctrl+E End of line Go to the end of the line.
Ctrl+L Clear screen Clear screen and leave line at the top of the screen.

The keystrokes in Table 3.2 can be used to edit command lines.

TABLE 3.2 Keystrokes for Editing Command Lines

Keystroke Full Name Meaning
Ctrl+D Delete current Delete the current character.
Backspace Delete previous Delete the previous character.
Ctrl+T Transpose character Switch positions of current and previous characters.
Alt+T Transpose words Switch positions of current and previous words.
Alt+U Uppercase word Change the current word to uppercase.
Alt+L Lowercase word Change the current word to lowercase.
Alt+C Capitalize word Change the current word to an initial capital.
Ctrl+V Insert special character Add a special character. For example, to add a Tab character, press Ctrl+V+Tab.

Use the keystrokes in Table 3.3 to cut and paste text on a command line.

TABLE 3.3 Keystrokes for Cutting and Pasting Text from within Command Lines

Keystroke Full Name Meaning
Ctrl+K Cut end of line Cut text to the end of the line.
Ctrl+U Cut beginning of line Cut text to the beginning of the line.
Ctrl+W Cut previous word Cut the word located behind the cursor.
Alt+D Cut next word Cut the word following the cursor.
Ctrl+Y Paste recent text Paste most recently cut text.
Alt+Y Paste earlier text Rotate back to previously cut text and paste it.
Ctrl+C Delete whole line Delete the entire line.
Linux Bible

Подняться наверх