Emacs: How to read command help just as one would read Man pages? - emacs

It's nice to run M-x man foo for command foo within Emacs. It's then possible to navigate easily within the man page and to find details quickly.
When the help of a command (such as git) produces limited output, one can just use a terminal instead of emacs.
But occasionally, a command (such as aws help—run in a terminal) produces extensive output. Yet the output is not compatible with the emacs Man mode. An option is to use M-x shell within emacs, but that will not display the page at once. It will report "WARNING: terminal is not fully functional" and require pressing a key endlessly until the complete help appears, or, for Emacs 25, "Could not find executable named 'groff'".
What is a good way to read long manual pages produced by commands within emacs?

I just ran into this exact problem a few days ago.
Type escape + ! then type (for example) “aws ec2 help”. This brings the help text into a new buffer called Shell Command Output, with all of the control-h characters, etc.
Switch to the new buffer with control-x then lowercase ‘o’ (for other buffer)
Type escape + lowercase ‘x’ to run an emacs function, then type ‘man’ and hit Enter. It will prompt for man page entry and default to EC2, just hit Enter. In my case, it displays an error in the status line, “error in process sentinel: Can’t find the EC2 manpage”.
However, the “man page” functions are now available, so now (in that buffer)you can type escape + x and run the function Man-fontify-manpage. The page should now look like a nice man page with bold, etc.
You can then rename the buffer (escape + x then something like ec2) so the buffer isn’t replaced if you run another shell command.

I you just want the output in a buffer, you can simply use:
M-! aws help RET

If you want the output in a shell buffer, you can do git help commit | cat (so no more "terminal is not fully functional").
Probably you can do M-! aws help | cat RET also. I do not have aws, but hopefully the piping will remove the escape characters if aws output formatting is done right. You should try also TERM=dumb aws help. Any command should know better than using fancy output when TERM is set to dumb. If aws is dumb that way itself, you could pipe its output to something that filters out control characters -- try this
For forcing man mode in an arbitrary buffer, M-x Man-mode (yes, uppercase). I am not sure if that will play nice with aws's output.
By the way, for git I suppose you know you can do man git-commit (or man git-any_git_command, in general), so you have a nice alternative to git help when using emacs (output of help and man page is the same).

Related

How to fix some tmux things (screen instead of xterm; function keys)?

Why does tmux change the terminal from xterm to screen, and how can I fix the resulting text color change in emacs? I think the easiest way would be to prevent it from changing to screen in the first place.
I can use TERM=xterm emacs file.ext to do it temporarily, but that's just a workaround that doesn't solve the root of the problem.
Furthermore, the function keys no longer work in emacs when using tmux. Instead of F3 and F4 being macro shortcuts, they just print a tilde as they would in the shell. This seems unrelated to xterm/screen mentioned above. What is happening here, and how I can fix this?
tmux sets TERM to screen because that terminal description is limited to things that tmux knows how to work with. Like screen, tmux translates features from the outer terminal description to the inner.
If a special key (function-key, cursor-key, etc) does not have an exact match in the terminal description, tmux will ignore it.
The default configuration for PuTTY sends different escape sequences for F1-F4. The sequences which PuTTY sends are not in the terminal description for xterm.
Here's a comparison of the two (as a CSV file, but readable enough):
NAME,putty,xterm
kf1,\E[11~,\EOP
kf2,\E[12~,\EOQ
kf3,\E[13~,\EOR
kf4,\E[14~,\EOS
kf5,\E[15~,\E[15~
kf6,\E[17~,\E[17~
kf7,\E[18~,\E[18~
kf8,\E[19~,\E[19~
kf9,\E[20~,\E[20~
kf10,\E[21~,\E[21~
kf11,\E[23~,\E[23~
kf12,\E[24~,\E[24~
kf13,\E[25~,\E[1;2P
kf14,\E[26~,\E[1;2Q
kf15,\E[28~,\E[1;2R
kf16,\E[29~,\E[1;2S
kf17,\E[31~,\E[15;2~
kf18,\E[32~,\E[17;2~
kf19,\E[33~,\E[18;2~
kf20,\E[34~,\E[19;2~
You'd have trouble getting PuTTY to send F13-F20, but will certainly run into trouble using PuTTY and tmux with TERM=xterm.
Regarding colors, the same issue applies. The screen terminal description tells applications that the terminal can support 8 colors, and tells how to display those eight colors. If your external terminal can do more, then tmux and screen hide that.
The conversion is not perfect. GNU screen has a feature where it looks for a corresponding screen.$TERM terminal description (i.e., concatenating the outer TERM value to screen). tmux does not do that: it makes assumptions regarding xterm. But PuTTY is not xterm...
ncurses has several of those concatenated terminal-names for terminal descriptions, but no one has suggested a way for tmux to use them automatically.

Complete command from history in ipython

For reasons outside of my control I'm stuck using python 2.6.6 and IPython 0.10.2. I also normally use the tcsh shell, and have gotten quite used to completing a command from the history using <A-p> (i.e. pressing the ALT key and p). However, this doesn't work in IPython. I know I can press <C-r> and then start typing a command, but what inevitably is happening is that I start a command, press <A-p>, get a colon indicating some weird state, then exit out of that state, delete my command, press <C-r> then search for my command. It's getting rather irritating. Is there any way to make <A-p> complete my already started command by relying on the history?
Ouch, this is an old version of IPython, Python (and pip). The bad news is I don't have much experience with such an old version of IPython, the good new is; it was way simpler at that time.
Most of the shortcut and feature are provided using readline, and the python bindings of stdlib. Meaning that most likely what you are trying to configure is readline itself and not only IPython; so you can find more information on that outside of IPython !
The secret is to grep in the source-code for parse_and_bind, then you'll find the following example configuration, leading me to change the ~/.ipython/ipy_user_conf.py to be like so at around line 99 (all indented an extra 4 space to be in the main() function):
import readline
readline.parse_and_bind('set completion-query-items 1000')
readline.parse_and_bind('set page-completions no')
rlopts = """\
tab: complete
"\C-l": possible-completions
set show-all-if-ambiguous on
"\C-o": tab-insert
"\M-i": " "
"\M-o": "\d\d\d\d"
"\M-I": "\d\d\d\d"
"\C-r": reverse-search-history
"\C-s": forward-search-history
"\C-p": history-search-backward
"\C-n": history-search-forward
"\e[A": history-search-backward
"\e[B": history-search-forward
"\C-k": kill-line
"\C-u": unix-line-discard"""
for cmd in rlopts.split('\n'):
readline.parse_and_bind(cmd)
The repetition of commands make me think that what \C,\M or [e mean might be system dependant. I would bet on \C being Control, and \M being Meta (Alt, Opt), but at least one of these line did the trick for me (and also now tab allows to complete). See also man readline for the list of commands you can bind to what, and enjoy! Hoping you can upgrade to Python 3 and IPython 6 at some point.
[Edit]
See Eric Carlsen second comment under this answer for how it was resolved.

Emacs Comint History: Search Rather Than Navigate One By One

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.)

How to have find-file's prompt match Emacs shell's $PWD?

With Emacs, if the current buffer is one that's "visiting" a normal file (for example), whose full pathname is /path/to/somefile, and one runs find-file (C-x C-f), the prompt that appears in the mini-buffer is something like
Find file: /path/to/▮
...with the cursor placed as indicated above by ▮. IOW, the suggested path shown by default is always to the directory containing the file that the current buffer is visiting.
If, however, the current buffer is an Emacs shell process, and one runs find-file, then, AFAICT, the path shown in the prompt remains fixed at the value of $PWD when the shell process was started, irrespective of the current value of $PWD:
Find file: /pwd/at/startup/▮
This behavior is not so useful, because the $PWD at startup often becomes irrelevant later on. It would be really nice if the directory shown in find-file's prompt were instead the shell process's current $PWD.
Is there a simple way to modify find-file to behave this way whenever the current buffer is a shell process?
You want "shell directory tracking". E.g. check dirtrack-mode or shell-dirtrack-mode.
shell-dirtrack-mode tries to parse "cd" commands, (event_jr: which in my experience does not work consistently). dirtrack-mode uses the prompt regexp, which works very well.
There are a number of ways to manage this. As Stefan notes, there are a couple of built in packages that manage it.
My preferred way is to alter your prompt (when in Emacs) to have the $PWD embedded in it, and then Emacs strips it out and uses it. This has the benefit of always being up to date. I've found that dirtrack-mode sometimes gets out of sync.
Check out my solution here, which is a modification of a similar implementation on the Emacs Wiki.

Would it be possible to jump between prev/next Terminal command prompts?

I'm using zsh in OS X Terminal.app and for quite a while, I've been longing for a way to jump back and forth between prev/next prompts in the terminal's output.
One convenience with this would be to be able to review (and track errors at) the end of each command's output; eg. when you building stuff from source with ./configure; make; make install. Note: I'm obviously not referring to jumping back and forth in the command-history, but for a way to take a peek at the endings of each command's output.
Has anyone heard of such functionality in the *nix (preferrably also Mac) world? Would it require some sort of OS-centric Terminal plugin, or can it be programmatically done via a shell script which can be tied to a keyboard shortcut? Maybe I'm the only one thinking about this? :)
Edit: Here's an example scenario: Let's say I want to compile and install some program (using standard ./configure && make && make install procedure) and after the make command, I run into some errors. Now, the way I understand it (I may be completely wrong), the crucial error causing the make command to fail usually shows up in the last line(s) in the output, no? Anyway, at this point, I might do something like cat INSTALL to read up on the INSTALL document to check whether there's something I've missed. NOW, if I want to go back to see what the error was, that caused my initial make command to fail, I then have to manually scroll up to that position again, since my cat INSTALL command printed a ton of text after it.
I don't know if this scenario is the most elucidative – but there are many other situations where I wish I could just "jump" back to previous prompt lines and check up on previous command output; whether it was a simple ls command, make, git status, or whatever it was – swapping positions in the window by means of using prompt lines as "bookmark" positions seems an interesting idea to me.
command + left or right goes between tabs in iterm. is this what you are asking?
Emacs has a shell-mode that runs a shell inside the Emacs editor, providing a rich environment of additional commands for navigating and working with shell commands. This includes commands for going to the previous/next prompt, and deleting the output from commands so you can "clean up" and issue another command.
If you aren't familiar with Emacs: to start a shell inside Emacs, run emacs from the shell, then type Esc-x (or Meta-x, if you have "Use option as meta key" enabled in Terminal > Preferences > Settings > [profile] > Keyboard). This will ask for a command to execute. Enter shell.
To see a list of commands you can use in Shell Mode, enter Control-h m. Here are the ones for moving the cursor to the previous/next prompt:
C-c C-n comint-next-prompt
C-c C-p comint-previous-prompt
These commands would also be useful:
C-c C-r comint-show-output
C-c C-o comint-delete-output