1 The script
Based on this script on Emacs Wiki I just added the option tu use as protocol to open urls in the form emacsclient:///path/to/file+linenumber
#!/bin/bash args="" nw=false # check if emacsclient is already running if pgrep -U $(id -u) emacsclient > /dev/null; then running=true; fi # check if the user wants TUI mode for arg in "$@"; do if [ "$arg" = "-nw" ] || [ "$arg" = "-t" ] || [ "$arg" = "--tty" ] then nw=true fi done # if called without arguments - open a new gui instance if [ "$#" -eq "0" ] || [ "$running" != true ]; then args=(-c $args) # open emacsclient in a new window fi if [ "$#" -gt "0" ]; then # if 'Vemacsclient -' open standard input (e.g. pipe) if [[ "$1" == "-" ]]; then TMP="$(mktemp /tmp/emacsstdin-XXX)" cat >$TMP args=($args --eval '(let ((b (generate-new-buffer "*stdin*"))) (switch-to-buffer b) (insert-file-contents "'${TMP}'") (delete-file "'${TMP}'"))') # If called as protocol syntax of call is emacsclient:///path/to/myfile+line elif [[ "$1" == emacsclient:* ]]; then args=($args --eval '(let ((protocall (split-string (substring "'$1'" 12) "+"))) (find-file (nth 0 protocall)) (when (nth 1 protocall) (goto-char (point-min)) (forward-line (string-to-number (nth 1 protocall)))))') else args=($@ $args) fi fi # emacsclient $args if $nw; then emacsclient "${args[@]}" else echo ${args[@]} >> /tmp/ecargs (nohup emacsclient "${args[@]}" > /dev/null 2>&1 &) > /dev/null fi
2 Define the script as handler for emacsclient:/// links
Assuming the script is at home/vic.local/bin/Vemacsclient
2.1 Created a temporary file named vic-emacsclient.desktop
[Desktop Entry] Encoding=UTF-8 Version=1.0 Type=Application Exec=/home/vic/.local/bin/Vemacsclient Name=Emacsclient Comment=Maybe launches Victor's emaclient Categories=Editor;Development Icon=emacs MimeType=x-scheme-handler/emacsclient;
2.2 Installed the protocol
xdg-desktop-menu install vic-emacsclient.desktop xdg-mime default vic-emacsclient.desktop x-scheme-handler/emacsclient
3 Usage
- Open file from commandine
Vemacsclient /path/to/file +line:column
- Pipe shell output to emacs buffer
ls / | Vemacsclient -
- Open file with emacsclient protocol
emacsclient:///path/to/file+line
This is especially useful for Drupal’s (Symfony’s?) Web Profiler plugin. I set its IDE settings toemacsclient:@file+@line
and then clicking any file links in the profiler opens the file with Emacs. Can be very useful to understand where a particular bit of code gets generated.