Книга: Learning GNU Emacs, 3rd Edition

5.1.1 Running One Command at a Time

5.1.1 Running One Command at a Time

To run a command while you're in an Emacs session, type M-!. Emacs asks for the command you want to run. Type the command and press Enter. Emacs then opens a window called *Shell Command Output* where it displays the results of your command.

Type: M-!


Emacs prompts you for a command to execute.

Type: diff joyce joyce2


Emacs executes the diff command and puts the output into a *Shell Command Output* buffer.

Because the output from the diff command is in a buffer, you can edit it, save it, or do anything else you would like with it. Of course, if the operating system has no diff command or cannot access it for some reason, this command fails.

An interesting twist to the shell command facility is that you can use a region of a buffer rather than a traditional file as input to the command. For example, let's say we want to sort a phone list. First, we put the cursor somewhere in the list (say, on the first character of Liam), then we give the mark-paragraph command (M-h). This command defines the phone list as a region, with the cursor at the beginning of the paragraph and the mark at the end.

In the following example, the shaded area shows the extent of the region we want to sort. After selecting a region, we press M-| (for shell-command-on-region); Emacs prompts for the shell command to run.

Type: M-h M-|


Emacs prompts you for a command to execute (Windows).

Now we give the command sort without specifying any input file. Emacs is taking care of the input for us.

Type: sort Enter


Emacs runs a sort on the region (Windows).

Emacs has sorted the phone list (i.e., everything within the region).

A useful variation for M-! puts the output directly into the current buffer, rather than into a *Shell Command Output* buffer. To do so, precede the command with C-u: for example, C-u M-! runs a shell command and puts the output in the current buffer.

Type: C-u M-! ls -la Enter


Emacs runs ls and inserts the result at your current location (Mac OS X).

Оглавление книги

Оглавление статьи/книги

Генерация: 1.121. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз