1

I am able to use man inside emacs. M-x man , <ln> , [enter].

=> From the shell can I open man pages from the shell? For examlpe doing $ man ln can open the man page inside the emacs?


$ export MANPAGER='emacsclient -t -q'
$ man ln
❯ # opens the `*scratch*` file

alper
  • 1,238
  • 11
  • 30
  • 1
    I am not aware of an existing solution. `MANPAGER` needs a) the program to read from standard input, but Emacs and Emacsclient can't do this, though maybe vip or vipe can help? b) display the rendered man pager, that is, handle the terminal escape code, an Emacs major mode is needed, I tried Man-mode but it didn't work. According to https://lobste.rs/s/gquf4t/one_small_step_for_man#c_bukh5l, Neovim meets both the requirements, `export MANPAGER="nvim -u NORC -c 'set ft=man' -"`, the result looks good, it even has specific sytnax highlight for C functions such as `man 3 printf`. – xuchunyang Jul 05 '20 at 13:18
  • `nvim` seems pretty nice thank you for sharing. If it also highlights in different color of the current word on the search it would be much better :-) During search(`/`) in `nvim` I wish I able to use basic emacs's key bindings like `ctrl-a` to fo to beginning of line `ctrl-k` to kill the line etc. – alper Jul 05 '20 at 13:41
  • seems like it enter a endless loop and does not open anything :( – alper Jul 05 '20 at 18:26
  • Sorry, I meant a function: `man() { emacsclient -nw -e "(woman \"$1\")" }`. – jagrg Jul 05 '20 at 20:15
  • I pasted it into bash and called `man printf` but didn't work as I wanted – alper Jul 05 '20 at 23:28

1 Answers1

4

Define a shell function that uses the first argument to man as a parameter

macsman() {
    emacsclient -c -e "(man \"$1\")"
}

alias man=macsman

You could invoke the function without the alias (e.g. macman ls), but it may be useful to set the alias for specific shells.

One note: You may want to assure your Man page is visible in the Emacs session as soon as it is invoked. Check the settings of the Man-notify-method variable to achieve that. Either set it using customize-variable, or set it explicitly as part of the function, i.e.

emacsclient -c -e "(let ((Man-notify-method 'bully)) (man \"$1\"))"

(Hat tip to @phils)

phils
  • 48,657
  • 3
  • 76
  • 115
gregoryg
  • 915
  • 6
  • 8
  • it gives `#` error – alper Jul 12 '20 at 00:09
  • I only see that message at the shell if I specify a non-existent man page. Can you execute `macsman ls` ? That should work as long as the function is sourced and the quotes are escaped. (Assuming bash, BTW). Also check the *Messages* buffer in Emacs, it should indicate that it's invoking `man` in the background) – gregoryg Jul 12 '20 at 00:14
  • You might find `--eval "(let ((Man-notify-method 'bully)) (man \"$1\"))"` preferable. – phils Jul 12 '20 at 11:19
  • I keep getting the same error :-( `emacsclient --eval "(let ((Man-notify-method 'bully)) (man \"ls\"))"` output: `#` @phils – alper Jul 12 '20 at 16:52
  • Probably something is wrong on my side: `macsman ls` => `#`. I am using ZSH. Message buffer shows the man page buts its width is 10 characters, so its pretty difficult to read it @gregoryg – alper Jul 12 '20 at 16:55
  • Does it work with `emacs` instead of `emacsclient` ? – phils Jul 12 '20 at 19:57
  • @alper It works for me even in `zsh`. Are you certain that the man page does not appear in your running Emacs - as buffer `*Man ls*`? I am assuming some things here: that you have Emacs running, the server is running (`server-start` in Emacs, check with `M-x list-processes`). If your `Man-notify-method` is not set to `aggressive`, `bully`, `pushy` or `newframe`, you will not see the Man page because it will be buried. This is related to @phils comment - I will expand my answer to include all these options when I get a chance – gregoryg Jul 13 '20 at 01:42
  • @phils Yes it works with `emacs`. When I run under `emacsclient` Man page appears in my emacs as buffer. But its width is 10 charactes and it does not open right away, I have to open it from the buffer. – alper Jul 13 '20 at 14:15
  • @alper I cannot reproduce your experience with bash/zsh/GUI/console, unfortunately. – gregoryg Jul 13 '20 at 16:43
  • 1
    All good seems like there is something is wrong on my end – alper Jul 13 '20 at 16:45
  • @alper I think you just need to use `emacsclient -nw` or `emacsclient -c` (depending on whether you want a terminal or GUI frame), in addition to the rest of the suggested options. I believe the rendering width issue is tied to that as well. You may need to kill any man buffers which are in that mis-rendered state to prevent them being re-used. In my tests everything is fine after that. – phils Jul 13 '20 at 22:05
  • Ah I didn't know I can use `emacsclient -nw`, combining with your advice on the previous commet works fine @phils – alper Jul 13 '20 at 23:46
  • If a single man page is open in emacs and when we press `q` instead of closing the man buffer, can it go back to the terminal-shell? (I can ask as a new question) @phils – alper Jul 13 '20 at 23:49
  • If man page does not exist, is it also prevent emacs to open `Can’t find the cd manpage`, insead it opens with the latest buffer from the recent. @gregoryg – alper Jul 14 '20 at 15:28