Inspired by Git as when you type "git commit", it opens an Emacs or Vim session for you. I'm writing an Erlang escript, and I want it to open an Emacs session at the end of the execution of the escript. I've tried
os:cmd("emacs -nw file.txt")
but it doesn't seem to work. Evaluating the above command within the Erlang shell yields
"emacs: standard input is not a tty\n"
One way to do this is to keep an Emacs running with server mode (put (server-mode) in your ~/.emacs), and call emacsclient instead of emacs from Erlang. That will open the file in the existing Emacs session. emacsclient exits and returns control to your Erlang program once you hit C-x # in Emacs.
Related
Is it possible to send commands to Gnu Emacs from another program?
It would e.g. be nice if I could tell emacs go to a certain line in the current text document.
First, M-x server-start in Emacs (or put (server-start) in your init.el file).
Then you should be able to send Emacs commands with emacsclient - for example to go to line #10 in whatever buffer has focus:
emacsclient --eval "(with-current-buffer (window-buffer) (goto-line 10))"
(Thanks to comments below for pointing out errors! Always test 😅)
Invoking 'shell' by M-x shell,
and plan to start a tmux session
it report errors
$ tmux
open terminal failed: terminal does not support clear
What's the problem?
If invoking `ansi-term', the operations of yanking and pasting are invalid.
So, shell perform better than ansi-term in routines.
How could activate tmux within a shell
tmux needs a real terminal, and shell doesn't provide that (as implied by the error you get). So you need to use term with tmux.
Or you can use built in functionality of Emacs to have features of tmux. E.g. C-x 2 and C-x 3 will split the frame into windows so you can have multiple terminals in a frame (an Emacs frame is what most applications would call a window). Start an emacs server and emacsclient to have sessions that you can connect to and keep running after you close the frame.
Copy (M-w) and paste (S-<insert>) should work by default. If you want to play with bindings, the key map is called term-raw-map and the commands are kill-ring-save and term-paste.
Also learn the difference between term-line-mode (C-c C-j) and term-char-mode (C-c C-k). Briefly, line mode behaves more like shell, and char mode behaves more like a real terminal, with most Emacs keybindings unavailable. I personally keep term buffers in char mode almost always and add some keybindings to term-raw-map so I can run certain Emacs commands.
My environment:
OS: Linux CentOS 7 (x86_64)
PostgreSQL version: 10.5
Emacs 26.1
I use Emacs as the external editor in postgresql (set in my EDITOR environment variable). So whenever I type in psql shell, \e it opens Emacs where I can write/modify queries, views, functions, etc.
If I understand correctly, once Emacs is open, when I enter C-x C-s, that is, I save and then I quit C-x C-c, the content edited in Emacs is transferred to the query buffer to be parsed and executed (assuming it contains semicolon at the end). So basically each time I have to run \e then edit, then save and quit the editor to get the job done.
Now, given that I use Emacs for multiple programming languages, I've rather a big init file. As a result, it takes several seconds to start Emacs (both in -nw and GUI mode). Obviously this is quite annoying given the number of times that I have to open and quit the editor while I'm using \e in psql.
So my question is: Is there any way to let the external editor remain open and continue working with the same editor for further queries and somehow decide to transfer the result to query buffer without needing to quit the editor?
Run emacsclient instead of emacs. I have a script at ~/bin/editor:
#!/bin/sh
exec emacsclient -c -a '' "$#"
And then I set export EDITOR=$HOME/bin/editor in my ~/.bashrc.
The upshot is you only start Emacs once, and every time you run $EDITOR, it just attaches to the same Emacs session.
Also, I do
(global-set-key (kbd "C-x C-c") #'delete-frame)
(global-set-key (kbd "C-x C-S-c") #'save-buffers-kill-emacs)
so C-x C-c just deletes the frame instead of killing Emacs.
Just save the query to a temporary file, for example /tmp/q.sql and run \i /tmp/q.sql from psql multiple times from a second terminal.
Emacs M-x compile does not see any aliases set in .bashrc. If I use M-x shell then type the alias, it is fine. I tried sourcing .bashrc from /etc/profile, from ~/.profile, ~/bash_env, anything I can think of to no avail.
I am on Emacs 23 and Ubuntu 11. I start emacs using /usr/bin/emacs %F, from a desktop button.
Emacs inherits its environment from the parent process. How are you invoking Emacs - from the command line, or some other way?
What happens if you:
M-x compile RET C-a C-k bash -i -c your_alias RET
Invoking bash as an interactive shell (-i option) should read your .bashrc aliases.
Edit: I think both M-x shell-command and M-x compile execute commands in an inferior shell via call-process. Try the following in your .emacs (or just evaluate):
(setq shell-file-name "bash")
(setq shell-command-switch "-ic")
I notice that after evaluation of the above, .bashrc aliases are picked up for use by both M-x shell-command and M-x compile, i.e
M-x compile RET your_alias RET
should then work.
My environment: Emacs 24.1 (pretest rc1), OSX 10.7.3
Keith Flower's answer works but can result in some slowdowns due to .bashrc being unnecessarily loaded in other places (presumably many many times, my computer is not exactly under-powered but emacs was almost unusable when trying to use autocomplete.el).
An alternative way is to locally modify shell-command-switch only for the functions where it is needed. This can be done using emacs' "advice" feature to create a wrapper around those functions. Here's an example that modifies compile:
;; Define + active modification to compile that locally sets
;; shell-command-switch to "-ic".
(defadvice compile (around use-bashrc activate)
"Load .bashrc in any calls to bash (e.g. so we can use aliases)"
(let ((shell-command-switch "-ic"))
ad-do-it))
You need to write similar "advice" for each function that you want to use .bashrc (e.g. I also needed to define the same advice for recompile), just copy the above and replace compile in the above with another function name.
You may like emac's bash-completion :
https://github.com/szermatt/emacs-bash-completion
You'll be able to use tab completion of your aliases in the compilation minibuffer and in shell-mode.
Enjoy !
(they speak about it here Bash autocompletion in Emacs shell-mode )
I think compilation commands are not interpreted through a shell: they are juste exec'ed by emacs (which means aliases, shell functions and other shell-specific things are not taken into account).
Try to wrap you compilation command into a shell-script which would source the correct environment.
You can do this either with a full-fledged shell-script in the form
#!/bin/bash
source "~/.bashrc"
my_command
or directly in emacs with a compilation command of the form
bash -c "source ~/.bashrc; my_command"
See Is there a way to get my emacs to recognize my bash aliases and custom functions when I run a shell command? for a fix which doesn't run all your .bashrc and doesn't create these error messages:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
Is there a way to start different emacs mode using command line? For example, is it possible to run emacs as follows?
emacs --org-mode # to start orgmode
emacs --python-mode # to start python mode
I can just run emacs to input 'M-x org-mode' thereafter, but I wonder if I can start different modes.
You can call functions with the -f argument, so to start org-mode use:
emacs -f org-mode