The combination M-!
allows you to launch shell commands. You could use it to launch a separate urxvt
.
M-! urxvt RET
I just tried it with xterm
(I don't have urxvt
) and it did open in the same directory as the file in the buffer.
If you want to define a shortcut add something similar in your init file:
(global-set-key (kbd "C-c s") (kbd "M-! urxvt RET"))
In my case I bound the shortcut to: Ctrl+C - S.
Below, I analyse your script. But having done that, I think the answer to the question you should have asked is:
- Run
emacs --daemon
from your .profile
. This creates a background instance of Emacs.
- Run
emacsclient FILENAME
to open a file in Emacs. This creates a new window (either an X11 window, or a window using the current terminal).
- Load the
EmacsHints
file in your Emacs startup file: put (find-file "/home/Harry/emacs/EmacsHints.txt")
in your .emacs
.
We can't tell for sure why you're getting t: Permission denied
, since you didn't post the script that generated this error (the script you posted has a comment on line 4). But you're creating a temporary file in the current directory; that's a bad idea in all circumstances, and probably isn't working when you don't have permission to write the current directory.
If you need to create a temporary file, use mktemp
. However, here, temporary files are an unnecessary complication: use command substitution to store the output of the commands into a variable.
#!/bin/sh
ps_lines=$(ps aux | grep EmacsHints)
line_count=$(echo "$ps_lines" | wc -l)
T=$(echo "$line_count" | awk '$1')
if [ "$T" = "2" ]; then …
All of this in turn is awfully complicated — just pipe the output of grep
into wc
, and the awk step isn't doing anything useful.
if [ "$(ps aux | grep EmacsHints | wc -l)" = 2 ]; then …
Furthermore, your test is unreliable: when the EmacsHints
file is open, sometimes ps
will return one line containing EmacsHints
, sometimes two: it depends on the timing of the ps
and grep
processes. Instead of building your own (which isn't working), use tools dedicated for this purpose: pidof
or pgrep
.
#!/bin/sh
if pgrep -f 'EmacsHints\.txt' >/dev/null; then …
Voilà! Simpler, and it actually works.
Well, it mostly works. If you open the EmacsHints
file in Emacs without specifying it on the command line, this won't be detected. I'd offer a better solution, but I don't understand what you're trying to accomplish. If you always want to have the EmacsHints
file open in Emacs, open it from your .emacs
.
Emacs is a bit slow to start, but many users (including me) set it up to run when you log in and then never exit it. Run emacs --daemon
from your .profile
. To open a file in the existing Emacs instance, call emacsclient
.
Regarding “standard input is not a tty”, you can run Emacs in the background only if it's opening a GUI window. If you're running Emacs in the terminal, it has to be in the foreground, otherwise it can't access the terminal. If you want to run Emacs in the background only in X:
if [ -n "$DISPLAY" ]; then
emacs "$1" &
else
emacs "$1"
fi
By the way, in a shell script, always put double quotes around variable and command substitutions: "$1"
, "$foo"
, "$(somecommand)"
, etc. When the substitution isn't inside double quotes, the value is interpreted not as a string but as a list of glob patterns. This issue typically manifests itself with scripts that fail on file names containing spaces. If you don't understand what all that means, just use double quotes.
Or you could just use emacsclient
.
Best Answer
I think you want to determine if a command is run in a terminal.
If you want this to always happen when you run Emacs, put it in a script and invoke that script instead. You can call the script
/usr/local/bin/emacs
(assuming Linux) if you want it to be calledemacs
and invoked in preference to the “real”emacs
executable in/usr/bin
.Note that to edit files as root, you should use
sudoedit
(benefits: the editor runs as you so you get all your settings; the edited file is put into place atomically when you finish editing, reducing the chance of a mishap). You can also edit files as root directly inside Emacs by opening/sudo::/path/to/file
.