3

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?

Lee Goddard
  • 130
  • 6
  • Similar idea but not exactly what you are looking for https://askubuntu.com/questions/447978/is-there-a-plugin-for-the-terminal-which-allow-to-detect-and-open-files/ – user.dz Apr 15 '15 at 22:20

2 Answers2

2

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.

A.B.
  • 90,397
  • That's very neat, thanks - but I really want a button to open a file in my editor of choice (sublime, but don't think that is important, just that I get to choose rather than rely upon file-associations/xclip). – Lee Goddard Apr 29 '15 at 15:07
1

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)

Eliah Kagan
  • 117,780
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497