Terminal : open path with middle-click/context menu?

Create a script:

nano ~/<your_script_folder>/open_selection

Include the following code:

#!/bin/bash
selected_text=$(xclip -o)
if [[ "$selected_text" == ~* ]]; then
    file_name=$(readlink -f ${selected_text/\~/$HOME})
else
    file_name="$selected_text"
fi

notify-send "Open selection" "$file_name"
xdg-open "$file_name"

*You can replace xdg-open with another command of your choice to open the selection with this program.

Make the script executable

chmod +x ~/<your_script_folder>/open_selection

Create a shortcut for this script.

Then only select a file name in your terminal and use your shortcut. A detour via the clipboard is not required.


What you could do as an alternative is to parse output of grep as an argument to your text editor of choice. For instance

COMMAND | grep -i filename | xargs nano

In this case nano is just a placeholder. You could use whatever text editor you want there. What you might want to do is to add a nohup in front of text editor name, so that you can continue using terminal.

Some time ago I answered a question, Is it possible to open a terminal in the current directory?, which asked to "open in terminal". The workaround I've been using is to bind a shortcut to script. You could do something similar - bind a shortcut to simple script bellow:

#!/bin/bash

FILENAME=$(zenity --entry --text="Enter path to file")

if [ $? -eq 0 ] 
   nano $FILENAME
fi

This basically should bring up a popup asking for path to file. The path can be copied from command line with CtrlShiftC, or you can download xclip and pipe the output into xclip -sel clip (which will put your text path into clipboard)