Execute custom command with selection

Asked by Juan Simón on 2016-02-18

I want to create a custom command to open a file (it is selected the path/name of file in Terminator) with an editor. Is there any keyword to represent the selected text?

Question information

English Edit question
Terminator Edit question
No assignee Edit question
Last query:
Last reply:
Juan Simón (simonbcn) said : #1

I have found a workaround with autocopy selection option activated in Terminator. I use "xclip -o" to obtain the selected text. But it would be better with a keyword to represent the selected text.

Keyword? Do you mean keyboard shortcut?

Let me see if I understand: You want to select a filename with the mouse, hit a keyboard shortcut, and have the system try to open the file?

Should the selection/filename be absolute, or does it need to detect the current working directory?

A lot of this is already there for hyperlinking (i.e. file:///tmp/random_file is turned into a hyperlink that can be Ctrl-left clicked and Terminator will use xdg-open to try and open it with a default application. Adding a shortcut and looking for a selection and trying to open that selection shouldn't be a massive amount of work. But this would be a wishlist item. If my understanding is correct, you can turn this into a wishlist item (via a bug report) using the link "Create bug report" on this page.

Juan Simón (simonbcn) said : #3

Sorry, I mean parameter, not keyword.
For example, when I create one action with "nautilus-actions-config-tool", in "Command" tab, there is a field named "Parameter", in this field I can put words like "%b", "%f", etc. that it represent filename, patch, etc.

Ah, OK, I think I see. You want to be able to select a piece of text (i.e. a filename) and then run a command in the terminal where you pass the selected text, using a short variable name?

So, you run a command that outputs a list of files, and select the one with filename: "myfile.txt"

You can then type (var name is just an example, standing for Terminator Primary Selection): vi ${TPS}

If I've got this right this time, I'll add that I don't think we could use the %f type notation, as it would require intercepting/parsing user input, which is way beyond what Terminator should be doing. Even the example I've given has a question mark. We set environment variables when we create a new terminal, but I'm not sure if we can inject an environment variable in an already running terminal.

Have I got it now?

Juan Simón (simonbcn) said : #5

Yes, you got it.

OK. I thought something re environment vars had cropped up before. See:
https://bugs.launchpad.net/terminator/+bug/696374 which seems to about MacOS X, but mentions placing the selection into an env variable.
https://bugs.launchpad.net/terminator/+bug/1340542 which is talking about setting an env variable on the profile changing.

The "solutions" I can find are some kind of hacky manipulation of the process running under the terminal by finding the child bash PID and attaching to it with gdb (the debugger) to set the env var.

Better would be to add a DBus call that would return the current selection. Then create a bash alias to call the function i.e.(untested):
alias tps='remotinator get_primary_selection'
Then you could use something like:
vi $(tps)

Note that this is only going to work for the bash processes on the same system as the Terminator instance. ssh into another machine, and this will not work.

Launchpad Janitor (janitor) said : #7

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Should really be marked answered, and a bug/wishlist item should be added for this.

Actually, I'm still a little puzzled. Why not just middle-mouse-click to insert the primary selection? Or Ctrl-Shift-C, Ctrl-Shift-V to copy and paste the selection using the keyboard? Both of those seem easier and quicker than what you are proposing.

Can you help with this problem?

Provide an answer of your own, or ask Juan Simón for more information if necessary.

To post a message you must log in.