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


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?


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 have answered a -enter link description here 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)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »