There are lots of places in UNIX where programs call out to the program in $PAGER (usually less or some similar command) to display some output. It's certainly true that many of the most common uses have an Emacs replacement (in the case of man, for example), but I'd still like a general way to use Emacs as my system-wide pager. Ideally this would mean that calls to PAGER end up in an Emacs temporary buffer similar to *Help*, a read-only buffer you can navigate around and dismiss by pressing "q".
I usually run a shell through M-x shell, so my envisioned use case is that typing a command like "man foo" in the *shell* window will bring up the man page in another window, more or less exactly like how the built-in *Help* system works.
For general use of $PAGER, you might be interested in e-sink.
For the specific case of man pages, it's better to use Emacs's built-in man mode as you note. I have this in my .bashrc:
man ()
{
if [ "$TERM" == "eterm-color" ]; then
emacsclient -e "(man \"$1\")";
else
command man "$#";
fi
}
Since you use shell-mode rather than ansi-term-mode like I do you will either have to make this use emacsclient all the time, or do something like (setenv "WITHIN_EMACS" "1") in your .emacs file so you can switch on $WITHIN_EMACS instead.
Related
In emacsclient, is there a way to open a file from the command line such that its buffer will remain open on the server after I close the client?
Alternatively, is there a command I can run from in a client to tell it to effectively detach from a buffer, so that the buffer does not get killed when the client exits?
Normally when closing emacsclient either with C-x C-c or C-x #, the buffer(s) associated with that client get killed, which is usually convenient behavior, but sometimes I would like for buffers to stay alive after closing. So far the only way I have found to accomplish this is to run the client without specifying any files, then visit them with C-x C-f, but I'm wondering if there's a better way to do this.
You should be able to do this by using the -n option. That means that it won't wait for you to "finish" the buffer and it'll just stay in the buffer list. I use this with emacsclient myself.
So, one option is to use emacsclient's eval command line option to run a lisp command to find the file you want.
emacsclient -c -e '(find-file "my_file")'
Obviously this is a lot more to type than the command sequence emacsclient -c, C-x C-f, my_file, but it could pretty easily be wrapped in a script that takes an extra argument to tell it whether to just choose the file or use find-file.
Someone more well versed in elisp than I could probably just add the option directly into emacs.
According the info manual, if you never want to kill emacsclient buffers, when you're done with them, you can customise the server-kill-new-buffers variable (more information at C-h v server-kill-new-buffers).
For you use-case, depending on how often you want the buffers killed or not, you could set the above variable to nil and then manually kill the buffers that you do want killed.
I am trying to open two files, say 'hello.txt' and 'world.txt' in emacsclient from a terminal and I want them to be opened in two different windows(as in the emacs sense of the word) but in the same frame.
I invoke emacsclient like this:
emacsclient -nw hello.txt world.txt
What presently happens is that a single emacsclient frame shows a single window where hello.txt is displayed. The other file, is opened into a buffer which is not visible.
If I instead use emacs instead of emacsclient I get the intended result(i.e two files get opened within the same frame but in two windows). How can I make emacsclient behave the same way as emacs?
I am not asking for ways to make emacsclient spawn multiple frames, rather I was asking for some way to make emacsclient to open multiple files in split-windows inside the same frame.
It doesn't seem like you can do this directly using emacsclient and a file list
You can achieve the same effect, with a bit of a kludge, by passing lisp to emacsclient to do what you want, although it gets a bit verbose
emacsclient -t -e '(progn (find-file "file1")(find-file-other-window "file2"))'
You perhaps could wrap this is a little shell script ec2files.sh, that takes two parameters and interpolates them into that lisp form.
Or write a defun that you load in emacs init that takes 2 file arguments and opens them.
(defun example-split-window-2-files (f1 f2)
(find-file f1)
(find-file-other-window f2))
and then invoke that from emacsclient -e
emacsclient -t -e '(example-split-window-2-files "file1" "file2")'
The variable server-window is what you want to look at. You can set this variable to a function that chooses which window to open.
I have the following in my emacs config:
(setq server-window 'pop-to-buffer)
That ensures that when you open a (single) file it uses another window (creating it if necessary) to display the file. You will need to either find or write a function to pass to server-window that will keep creating new windows for you to display files in.
Section 37.1, "Invoking `emacsclient'", of the emacs 24.3.1 manual says:
You can also force emacsclient' to open a new frame on a graphical
display, or on a text terminal, using the-c' and `-t' options.
I would like to, after switching to the buffer where I usually run commands, navigate the history by searching it, rather than navigate one-command-at-a-time at the end of the buffer (e.g. C-p).
Basically I would like to "Reverse I-search" the command history at the end of the buffer, rather than search the buffer.
Did anyone code a working solution? Note that I noticed there is a Command History buffer available, but here it is just a bunch of text and it is not grouped well enough I think to use.
As in a terminal, you can use M-r to search backward. It works in comint-mode, but it also work elsewhere, like in M-x (M-xM-rpatternRET).
Yes, with Icicles.
In Icicle mode, command icicle-comint-search is bound to C-c ` in shell buffers. It gives you the behavior you are looking for. It is described here.
It uses only stuff that is in the currently visible history as candidates, however. If you want to access stuff from your history from previous sessions then use command comint-input-ring, bound to C-c TAB, instead. (This is explained in the same doc.)
I've found that terminal emacs does not render the correct colors unless I explicitly set TERM=xterm-256color. I use gnome-terminal, and from what I understand, TERM should be set to gnome-256color. Similarly, I tend to use tmux a lot, which advises against any TERM setting other than screen-256color. Unfortunately, both of those settings (within their respective context - gnome-terminal or tmux) result in emacs having wrong colors, whereas vim displays colors correctly. However, if I export TERM=xterm-256color, the colors work just fine in emacs.
Can anyone explain what's going on, or offer a solution?
Update
Here's what I'm dealing with:
I can get the colors to look correct in the terminal by adding the following to my init.el:
(defun terminal-init-gnome ()
"Terminal initialization function for gnome-terminal."
;; This is a dirty hack that I accidentally stumbled across:
;; initializing "rxvt" first and _then_ "xterm" seems
;; to make the colors work... although I have no idea why.
(tty-run-terminal-initialization (selected-frame) "rxvt")
(tty-run-terminal-initialization (selected-frame) "xterm"))
This feels really, really wrong though. There has to be a logical explanation for this...
P.S.
I have very little knowledge of terminfo and the precise role that $TERM plays in the process of color terminal behavior. If it's safe to always use xterm-256color (even when $TERM "should" be gnome-256color or screen-256color), I'll go with that.
Maybe I'm not understanding something, buy why don't you run emacs like this:
TERM=xterm-256color emacs -nw
This way Emacs has its own TERM setting that you know works. You can also make an alias or wrap this in shell-script.
Terminals are a special type of device. When a process sends special byte sequences (called control sequences) to the terminal, it performs some action (like cursor positioning, change colors, etc).
You can read the ANSI terminal codes to find more detail about control sequences.
But terminals come from 70s, when hardware was limited in its capabilities, and a terminal cannot provide info about its capabilities (ie. which sequences it supports).
$TERM was used to resolve this issue - it allows programs to know what to send to the terminal to get the job done. termcap and terminfo are databases that store info about terminal capabilities for many $TERM names. If your $TERM is not in the db, you must ask an administrator to add it.
All terminal emulators inherit these limitations from old hardware terminals. So they need a properly set $TERM, and the terminfo/termcap DB MUST have data for this terminal. When a virtual terminal starts it sets the $TERM variable for you (and inside programs like bash). If $TERM is not in the terminfo/termcap you can quickly define an alias from $TERM to xterm-256color (you can find examples in the termcap file on how to do that).
This behavior has to do with the logic EMACS uses to determine whether the terminal background is dark or light. Run M-x list-colors-display with TERM set to either xterm-256color or screen-256color and you'll see that the exact same colors are listed. As you pointed out in the comments, the difference in color schemes that you've observed is due to the frame background mode. To see this, with your TERM set to screen-256color, compare the colors in
emacs -Q -nw --eval "(setq frame-background-mode 'light)"
and
emacs -Q -nw --eval "(setq frame-background-mode 'dark)"
The function frame-set-background-mode (in frame.el) checks to see whether the terminal type matches "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)" if it can't deduce the background color otherwise.
Within a running session, you can change the color scheme to 'light by evaluating
(let ((frame-background-mode 'light)) (frame-set-background-mode nil))
I am not that familiar with how emacs handles different terminals exactly. But looking at lisp/term directory in emacs sources, I found out that the existence of a function terminal-init-xxx allows you to add support for different terminals. For example, I've got:
(defun terminal-init-screen ()
"Terminal initialization function for screen."
;; Use the xterm color initialization code.
(xterm-register-default-colors)
(tty-set-up-initial-frame-faces))
in my .emacs, which adds support for screen-256color. You may try defining a similar function for gnome by renaming the above function to terminal-init-gnome.
NOTE: If you are interested, you can try to track down the calls from tty-run-terminal-initialization code. It first gets the terminal type using tty-type function, then looks at certain locations to load a relevant terminal file, then tries to locate the matching terminal-init-xxx function, and finally calls it. It may help you figure out the correct name for gnome-terminal.
It looks like unless your TERM indicates that your terminal has 256 colors, emacs will only use 8. Changing TERM to gnome-256color allowed the color registration functions to work.
There is a way to cheat, after all. When I run gnome-terminal, my terminal is set to xterm by default. Instead of changing TERM variable, it is possible to redirect xterm to another terminal, say, gnome-256color. Simply create the directory $(HOME)/.terminfo/x, then run ln -s /usr/share/terminfo/g/gnome-256color ~/.terminfo/x/xterm. I think this is better than setting TERM manually in .bashrc, because it only redirects a particular terminal to something else. A console login would still leave TERM as linux, and not xterm-256color.
Add this to your ~/.emacs:
(add-to-list 'term-file-aliases
'("st-256color" . "xterm-256color"))
It tells emacs that if it sees TERM=st-256color then it should initialize the terminal as if it had seen TERM=xterm-256color.
Longer answer:
Emacs is showing strange colors because it thinks your terminal can only support 8 colors. In Emacs, run M-x list-colors-display to see the colors it thinks are available. The correct number of colors is detected during terminal-specific initialization. It says, in part:
Each terminal type can have its own Lisp library that Emacs loads when run on that type of terminal.
On my machine, the terminal-specific initialization files are in /usr/local/share/emacs/25.*/lisp/term. It has files for xterm, rxvt, screen, etc. but nothing for st. We need to help Emacs find the right initialization file. The documentation further says:
If there is an entry matching TERM in the term-file-aliases association list, Emacs uses the associated value in place of TERM
So that association list is a recommended way to handle unknown terminals. It works without you having to manually override the TERM environment variable.
On ubuntu 10.04 I too had noticed that running emacs -nw inside byobu/tmux/screen was using different colours from emacs -nw in the regular gnome-terminal.
I found that this is because byobu was setting TERM to screen-bce. Then setting TERM to xterm (for me, in the normal gnome-terminal TERM=xterm) gave me the same syntax highlighting when not running through byobu/screen.
So still not sure what the proper solution is.
See also this post:
Emacs Python-mode syntax highlighting
I installed the new release of emacs 23.1 and the very first difference I saw is that after M-x find-grep it takes 5-7 sec to show the standard command "find . -type f -print0 | xargs -0 -e grep -n ".
In release notes for 23.1 there is something about "Smarter minibuffer completion". Can I disable this feature and return to the old implementation when the command appeared immediately?
ANSWER: It is necessary to put somewhere in .emacs
(setq grep-highlight-matches nil)
to avoid a call (grep-probe) which takes a long time
Does it get faster after the first time you run it, or is it consistently slow?
It looks like find-grep (which is just an alias for grep-find, nice!) runs grep-compute-defaults to set up a bunch of meta-information (location of grep, how to invoke find, etc); I wonder if that's the slowness you're seeing? It looks like you may be able to save the value of grep-host-defaults-alist and side-step this process? (With the caveat that if anything ever changes, etc...)
Answering your second question about minibuffer completion, this setting will get you back to the completion used in Emacs 22:
(setq completion-styles '(emacs22))
However, that doesn't address the slowness like you'd hope. The slowness probably has something to do with your system. Maybe the package had to be read in and the disk was busy, or your system was loaded or ... 'find-grep works very speedily for me (and I'd bet the same for most everyone else).