Ubuntu – Terminal : open path with middle-click/context menu

gnome-terminal

I spend a lot of time copying the paths output by grep, and pasting them after a command that opens a visual editor.

Is there a way of programming a terminal emulator to open selected text in a specific editor? Perhaps a middle-click on selected text, or an addition to the context menu?

Is this perhaps a feature of some terminal I don't know?

Best Answer

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.