Running shell commands from Emacs

龙志勇
2023-12-01

Emacsrun a shell interactively with input and output to an Emacs buffernamed '*shell*' or run a shell inside a terminal emulator windows.

M-!cmd RET         Run the shell command line cmd and display the output (shell-command).

M-|cmd RET          Run the shell command line cmd with region contents as input;optionallyreplace the region with the output

                              (shell-command-on-region).

M-xshell               Run the shell command line cmd asynchronously, and displaythe output(async-shell-command).

M-xterm               Run a subshell with input and output through an Emacs buffer.Fullterminal emulation is available.


1.Multiple subshell

Tomake multiple subshells, you can invoke M-x shell with a prefix argument(e.g.C-u M-x shell), which will read a buffer name and create (orreuse) a subshell in that buffer. You can also rename the '*shell*' bufferusing M-x rename-uniquely, then create a new '*shell*' buffer usingplain M-x shell. Subshells in diffent buffers run independently andin parallel.


2.Shell history ring

M-p

C-UP          Fetch the next earlier old shell command.

M-n

C-DOWN    Fetch the next later old shell command.

M-r            Begin an incremental regexp search of old shellcommands.

C-cC-x       Fetch the next subsequent command from the history.

C-c.           Fetch one argument from an old shell command.

C-cC-l       Display the buffer's history of shell commands in anotherwindow(comint-dynamic-list-input-ring).


3.Emacs terminal Emulator

Torun a subshell in a terminal emulator, use M-x term. This create (orreuses) a buffer named '*terminal*', and runs a subshell with input comingfrom your keyboard, and output going to that buffer. Theterminal emulator uses Term mode, which has two input modes. In linemode, Term basically acts like Shell mode. In char mode, each characteris sent directly to the inferior subshell, as "terminal input". Any"echoing" of your input is the responsibility of thesubshell. The soleexception is the terminal escape character, which by default is C-c.Any "terminal output" from the subshell goes into thebuffer, advancingpoint. Tomake multiple terminal emulators, rename the buffer '*terminal*' to somethingdiffent using M-x rename-uniquely just as with Shell mode.


4.Term mode

Toswitch between line and char mode, use these commands:

C-cC-j            Switch to line mode. Do nothing if already in line mode.

C-cC-k           Switch to char mode. Do nothing if already in char mode.

C-cC-c            Send a literal C-c to the sub-shell.

C-cchar          This is equivalent to C-x char in normal Emacs. For example, C-co invokes the global binding of C-x o.


5.Remote host shell

Youcan login to remote computer, using whatever commands you would froma regular terminal, from a Term window. Aprogram that asks you for a password will normally suppres echoing ofthe password, so the password will not show up in the buffer. This willhappen just as if you were using a real terminal, if the buffer ischar mode. If it is in line mode, the password is temporarilyvisible, butwill be erased when you hit return.(This happens automatically; thereis no special password processing.) Whenyou log in to a diffent machine, you need to specify the type of terminalyou're using, by setting the TERM environment variable in the environmentfor the remote login command. (If you use bash, you do that bywriting the variable assignment before the remote login command, withoutseparating comma.) Terminal types 'ansi' or 'vt100' will work onmost systems.


NOTE: This article comes from GNU Emacs Manual.


 类似资料:

相关阅读

相关文章

相关问答