I want to run a shell command within Emacs and capture the full output to a variable. Is there a way to do this? For example, I would like to be able to set hello-string to "hello" in the following manner:
(setq hello-string (capture-stdout-of-shell-command "/bin/echo hello"))
Does the function capture-stdout-of-shell-command exist, and if so what is its real name?
Does shell-command-to-string meet your purpose?
For example:
(shell-command-to-string "/bin/echo hello")
Hope this helps.
I have a suggestion to made that extends Ise Wisteria's answer. Try using something like this:
(setq my_shell_output
(substring
(shell-command-to-string "/bin/echo hello")
0 -1))
This should set the string "hello" as the value of my_shell_output, but cleanly. Using (substring) eliminates the trailing \n that tends to occur when emacs calls out to a shell command. It bothers me in emacs running on Windows, and probably occurs on other platforms as well.
Related
I'm a recent convert to Evil-mode from Vim and I'm trying to make the environment more familiar. One of the things I miss is the find command in Vim. I'm trying to set up something similar in Emacs by wrapping the find-file command in a function. So far I have this:
(defun find nil
"Shadow vim find command, with helm."
(interactive)
(find-file))
When I run the command it yells at me, Wrong number of arguments {doc string} 0 I've tried adding arguments and had no success. The really confusing bit is that I shadowed a helm function the same way and it worked, like this:
(defun buflist nil
"List buffers in helm."
(interactive)
(helm-buffers-list))
What's different? How do I fix this?
find-file takes a file name as argument, you will want to get familiar with C-h f to lookup up function documentation.
interactive can take arguments, for example,
(defun find (filename)
(interactive "F")
(find-file filename))
find-file needs arguments, you can't call it just like
(find-file)
The debugger shows what arguments are needed:
(filename &optional wildcards)
You can also invoke help to see them: C-hf.
Another option is to use call-interactively:
(call-interactively 'find-file)
I use this valgrind.el to run valgrind inside emacs. But the newest version of emacs has deprecated compile-internal. I don't know nearly enough about elisp to figure out how to convert the compile-internal call to a compilation-start call. This is what the original function call in question looks like:
(compile-internal command "No more errors" "valgrind")
I found this bit online that indicates possible usage of compilation-start:
(compilation-start command mode
#'(lambda (mode-name) (concat "*" buf-name "*")))
Any help would be appreciated!
I am not sure about what you tried and what the results were.
As per the documentation, I would replace the compile internal line by:
(compilation-start command nil (lambda (mode-name) "*valgrind*"))
I'm trying to implement a keylogger in Emacs (for my own, non-nefarious purposes).
It seems that I can reliably capture the last command through real-last-command in the pre-command-hook
So, I can do something like:
(setq keylog-list nil)
(defun my-keylogger-function ()
(setq keylog-list (cons real-last-command keylog-list)))
(add-hook 'pre-command-hook 'my-keylogger-function)
After a few movement commands, we get
keylog-list's value is
(describe-variable left-char left-char previous-line previous-line left-char eval-last-sexp)
However, I'm interested in capturing the arguments to these commands as well (e.g. the arguments to left-char, which will by default be 1 but may be different if prefix arguments are used.
Is there a way to access the args as well? Something like real-last-command-arglist?
A keylogger is also built into emacs: (open-dribble-file).
Why do you log the last (i.e. previous) command? If you log this-command instead, you can log current-prefix-arg, which corresponds to the prefix argument used.
I am quite new to Emacs.
When running Emacs' python interpretor, it does
>>> print(24)
print(24)
24
Is there a way I can prevent the re-printing of my input and make it as below?
>>> print(24)
24
Thank you so much :)
The trick here is that the buffer you're running the python process in doesn't have comint-process-echoes set.
There are a couple of other questions that are relevant to your problem.
How to turn off the echoing
How to set emacs so it always turns off echoing
But the basic gist is you need to customize the value of comint-process-echoes. If you are new to emacs, you might not know that most customizations are done using emacs lisp, where setting a variable looks something like this:
(setq variable-name new-value)
In this case, the variable we want is comint-process-echoes so the lisp we want to evaluate is:
(setq comint-process-echoes t)
Where t is lisp-speak for "true."
So, to borrow the advice of the first link above, to actually tell emacs to evaluate this lisp code, use the M-: (meta+colon) command. From the python shell buffer, type meta+colon, then type (setq comint-process-echoes t) then hit return. Your problem should be solved.
A follow up on this question - if I have two python shells running in emacs, how can I designate which shell commands from each python script buffer is sent to? I suspect it has something to do with python-buffer and python-set-proc but setting these variables to the name of the shell is apparently not the solution.
Edit: actually since I am using python-mode rather than loveshack python, it probably does not have to do with python-buffer and python-set-proc.
You can set a new value to python-buffer.
(defun my-python-set-proc (buffer-name)
(interactive "B")
(setf python-buffer (get-buffer buffer-name))
(python-set-proc))
And then M-xmy-python-set-procRET*Python*RET,
or M-xmy-python-set-procRET*Python*<2>RET.
In any Python buffer, you can use the variable py-which-bufname to control which Python shell your code gets sent to when executing it. The variable is buffer-local, so in order to set it to a custom value you'll need to change it from within the buffer by pressing M-: and entering the following
(setq py-which-bufname "My-Custom-Bufname")
This also makes it easy to quickly create new process buffers: If you set py-which-bufname to a name for which there is no corresponding process buffer, you can just issue C-c ! to quickly create one.
HTH
py-shell-name sets the default, which might be overwritten by command
Should the buffer code contain a shebang specifying pythonVERSION , than this takes precedence over default setting.
You may enforce executing buffer through specific pythonVERSION by calling a command of class py-execute-buffer-pythonVERSION
See menu PyExec, entry Execute buffer ...