13 votes

Get output of external command in the buffer

Actually the shortest way of doing it is to use shell-command with a prefix argument. This is mapped to C-u M-!. The C-u prefix argument changes the standard M-! to insert the output in the current ...
dfeich's user avatar
  • 1,904
11 votes
Accepted

How do i change the default shell for shell-command

You need to change the option shell-file-name. (setenv "SHELL" "/bin/bash") This doesn't work since Emacs is already running thus it's too late, Emacs initializes shell-file-name according to ...
xuchunyang's user avatar
  • 14.5k
11 votes
Accepted

Run elisp when `async-shell-command` is done

You can specify the output buffer for async-shell-command. The shell runs as a process of the output buffer. You can get that process with get-buffer-process. Define your own process sentinel for the ...
Tobias's user avatar
  • 33.2k
8 votes
Accepted

Why is there a newline in the result of `shell-command-to-string`?

Shell commands usually terminate their output with a newline. shell-command-to-string doesn't add a newline, it merely stores the contents of the output of the shell command — including the final ...
jch's user avatar
  • 5,730
7 votes
Accepted

Run elisp from command line in running emacs

You can either wrap your two forms in a progn: emacsclient --no-wait --eval '(progn (message "hi") (setq abc 123))' Or you can send them separately: emacsclient --no-wait --eval '(message "hi")' '(...
Dan's user avatar
  • 33k
7 votes
Accepted

How to run sudo commands using shell-command?

When default-directory points to a sudo-ized path, shell-command uses this environment. Try (let ((default-directory "/sudo::")) (shell-command "ls")) The password will be asked interactively.
Michael Albinus's user avatar
7 votes
Accepted

How to run a previous unique command in an Emacs shell (without repeats)?

The shell you get from M-x shell is based on comint, so you can set comint-input-ignoredups to t in order to get the behaviour you want. The help even mentions bash: comint-input-ignoredups is a ...
Omar's user avatar
  • 4,812
6 votes
Accepted

When does shell-command-on-region actually pass the region to the command?

Why is the region passed to the command in the first example and not in the second and third one? The region is passed in all three cases; but if the command you pass it to does nothing with its ...
phils's user avatar
  • 51.1k
6 votes
Accepted

async-shell-command: run COMMAND without displaying the output

Try this: (add-to-list 'display-buffer-alist '("*Async Shell Command*" display-buffer-no-window (nil))) The buffer is still created and it still gets the output of the command (or the error output): ...
NickD's user avatar
  • 29.8k
5 votes
Accepted

How to mix eshell and elisp code

For something simple, shell-command-to-string may suffice. For something more complicated where the output needs to be modified, searched, etc., then something more elaborate may needed e.g., set-...
lawlist's user avatar
  • 19.1k
5 votes
Accepted

How to open system's command line at the directory of the current buffer?

You need to use start cmd to get a new terminal window. Use M-& (async-shell-command) to avoid blocking Emacs. This creates a useless *Async Shell Command* buffer, you can use the start-process-...
npostavs's user avatar
  • 9,203
5 votes

Why is there a newline in the result of `shell-command-to-string`?

As already explained, the newline is coming from the shell command output. You can eliminate that by making the shell command format its output with printf (which is portable and reliable when it ...
phils's user avatar
  • 51.1k
5 votes

How to make "shell-command-on-region" display output as a message on a non-zero exit code?

I would suggest considering to report a bug. Personally I'm fine with it, since it's easy to fix via C-/ (undo). To do what you want right now, my first thought is to advice shell-command-on-region, ...
xuchunyang's user avatar
  • 14.5k
5 votes

Split command line into a list of arguments

Does this do what you want? (split-string-and-unquote "program arg1 arg2 \"long argument with spaces\" \"arg\"3") There seemed to be an error in your string with an unmatched \" at the end that ...
John Kitchin's user avatar
  • 11.9k
5 votes
Accepted

Almost working smoothly attempt to get terminal Emacs to copy to clipboard

I'm also a loyal emacs-nw user, and have struggled with this over the recent emacs versions. What works for me, and without any need to C-g, follows. Note that it has a safety check that is linux-...
user1404316's user avatar
5 votes
Accepted

Send content of region or current line to a tmp file, then execute shell script with filename as argument

If your ~/dotfiles/rundo.sh accepts stdin as input, like many other commands such as grep/wc/bash/python, to run a command with the region as stdin, simply run M-| ~/dotfiles/rundo.sh (M-| runs shell-...
xuchunyang's user avatar
  • 14.5k
5 votes

How to pipe large files through fold before opening?

Have emacs read from stdin device and insert contents into *scratch* buffer. $ fold -w80 myfile | emacs --insert /path/to/stdin Note: You will need to update /path/to/stdin path e.g. $ fold -w80 ...
Melioratus's user avatar
  • 4,644
4 votes
Accepted

Set default-directory relative to the place an Elisp function is defined

You can define a variable in the script that records the directory: (defvar my-shell-directory (if load-file-name (file-name-directory load-file-name) default-directory)) You can then ...
Lindydancer's user avatar
  • 6,160
4 votes
Accepted

Trying to run a linux program in Emacs: command not found

Let me start by guessing that you start emacs from your graphical environment, and not from a shell. If you start it from the shell, emacs will inherit PATH from the shell, and you should be able to ...
Jon Kåre Hellan's user avatar
4 votes
Accepted

Is there a package to send a piece of text to shell and get the result back?

Possibly, but there's no need to install a package for this; it's built in. The command shell-command-on-region (bound to M-| by default) prompts for and then executes a shell command, sending it ...
db48x's user avatar
  • 18k
4 votes

Command to Run (Eval) "Hello World" in ELISP on Eshell?

The one word you request can also be just a dot .. After running that command you get: ~/Documents $ . hello.el Hello World ~/Documents $ If this is too strange for you you can also use: ~/...
Tobias's user avatar
  • 33.2k
4 votes
Accepted

Asynchronous version of shell-command-on-region?

Something in this spirit should do the trick. I tried it with espeak under Ubuntu; I guess it would work with say as well. (defun my-read-words-on-region () "Send the region to `espeak'." (...
Nsukami _'s user avatar
  • 6,521
4 votes

Differences between cmdproxy.exe and cmd.exe

I just found this: Proxy shell designed for use with Emacs on Windows 95 and NT. [...] The main function is simply to process the "-c string" option in the way /bin/sh does, since the standard ...
antonio's user avatar
  • 1,822
4 votes
Accepted

Run current line or selection in shell then insert result in Emacs buffer (Acme workflow)

M-| (M-x shell-command-on-region) runs a shell command using the region as stdin, display the output in the echo area, with a prefix arg, replace the region with the output. Unlike your shell in a ...
xuchunyang's user avatar
  • 14.5k
4 votes

Use basename of the file

Write your own function to prepare the command string as you want it and then pass it to shell-command for execution: (defun pandoc-it () (interactive) (let* ((basename (file-name-base (buffer-...
NickD's user avatar
  • 29.8k
4 votes

Running and viewing results from bash scripts

Did you try the shell-command command? If you don’t know what it does, run C-h f shell-command to see the help for it: shell-command is an interactive and byte-compiled function defined in simple.el....
db48x's user avatar
  • 18k
3 votes

Command to Run (Eval) "Hello World" in ELISP on Eshell?

You asked: A file (message "Hello World") saved as 'hello.el'. Now, how to run (evaluate) this file on Eshell? ~/Documents $ ....... hello.el I just fill in the command at the dots: ~/...
Tobias's user avatar
  • 33.2k
3 votes

Get output of external command in the buffer

Here's a very simple function that will insert the shell command's output at point it the current buffer. Note that it makes no effort to do error checking, but you can adapt it to do so if needed: (...
Dan's user avatar
  • 33k
3 votes
Accepted

evaluate sh source code, output is not displayed well

Your file is simply wrapping try calling M-x toggle-truncate-lines
Jules's user avatar
  • 1,225

Only top scored, non community-wiki answers of a minimum length are eligible