Skip to content

Instantly share code, notes, and snippets.

@usergenic
Created September 21, 2011 19:51
Show Gist options
  • Save usergenic/1233105 to your computer and use it in GitHub Desktop.
Save usergenic/1233105 to your computer and use it in GitHub Desktop.
iterm2 users, want cmd-click to open file in emacs?
#!/bin/bash
# This file is for iTerm2 users.
#
# Put this file in /usr/local/bin/iterm-cmd-click-emacsclient
#
# You'll need to do the following at command line...
# $ defaults write com.googlecode.iterm2 SemanticHistoryHandler /usr/local/bin/iterm-cmd-click-emacsclient
emacsclient -e "(find-file \"$1\")" "(goto-line $2)"
@jasonm23
Copy link

Alternatively do this : (Tested with zsh.)

https://raw.githubusercontent.com/ocodo/.emacs.d/master/iterm-emacsclient

@wpcarro
Copy link

wpcarro commented Jul 11, 2018

@jasonm23 that link is 404ing for me. Do you have an alternative link available?

@jasonm23
Copy link

jasonm23 commented Jun 2, 2023

@wpcarro I should update my notification settings for gist comments!

Create a script in ~/bin/iterm-emacsclient:

#!/bin/zsh --login
#
# Simple emacsclient wrapper for item2 "Semantic History" process running.
#

if [[ $1 == "+" ]]; then
  # throw $1 out.
  shift 
fi

emacsclient -n $1 $2

Make it executable:

chmod +x ~/bin/iterm-emacsclient

In iTerm2 Session preferences Advanced pane setup Semantic History as:

[Run command...]

Enter:

~/bin/iterm-emacsclient +\2 \1

What does it do?

emacsclient accepts row number if prefixed with + e.g.

emacsclient -n +20 filename

This'll open filename and move to line 20. -n stops emacsclient waiting for the buffer to close.

So when you click filename in iTerm, it'll call emacsclient -n filename

If the user clicks on filename:123, it'll call emacsclient -n +123 filename.

Note in the run command above, we call it with +\2 and \1. \2 is the row number, and \1 is the filename. When no row number is sent, the script has $1 as + so we shift it off, and $2 becomes $1, so we don't send emacsclient a stray + which will throw an error.

*ERROR*: Invalid -position command in client args

That's detailed enough of a rundown, hopefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment