Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to press a key in tmux to invoke path-extractor to select the paths of files in console? #2

Closed
tkt028 opened this issue Aug 5, 2015 · 9 comments

Comments

@tkt028
Copy link

tkt028 commented Aug 5, 2015

Hi,
I am trying path-extractor. My question is if it is possible to press a key in tmux to invoke path-extractor to select the paths of files in console? (like the tmux-plugins/tmux-fpp )

Thank you very much!
Cheers,
Khon

P/S: I really like the path-extractor feature

@edi9999
Copy link
Owner

edi9999 commented Aug 5, 2015

Hi, thanks for opening this issue, always good when an open-source project gets its first issue, it means that someone wants to get things done with it.

Regarding your issue, I'm not sure that what you want to do needs path-extractor :

You can do that exactly with fzf :

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

and then, answer yes for the question "Do you want to add key bindings".

If you restart your shell, you will have keybindings "Ctrl+T" to do exactly the same.

You can then create your own binding that will open vim for a file :

you can look at the keybinding files to see how to do it :

https://github.com/junegunn/fzf/tree/master/shell

Path extractor is a more "dumb" pipe because it will only convert the input into a list of possible paths.

Did that help ?

@tkt028
Copy link
Author

tkt028 commented Aug 5, 2015

Hi Edi,
Thank for your quickly reply. In my case:

  • step 1: I execute a command X, it produces lots of output with many file paths to screen
  • step 2: I want to press key binding to capture the list of file path in screen, I do fzf to filter for things I interested in, then press Enter >> to copy it to clipboard (I don't want to open it by editor)

Is there any way to achive this? Thank you very much!
Cheers,
Khon

@edi9999
Copy link
Owner

edi9999 commented Aug 5, 2015

Here is how to do that in bash :

Add this to your bashrc :

bind '"CC": "| pe | uniq | fzf | while read filename; do [ ! -z $filename ] && echo -n $filename | xclip -selection c ; done\n

And then you can do <up> and press CC, you will have fzf opened to select the filepath. You need to install xclip which is " apt-get install xclip"

@edi9999
Copy link
Owner

edi9999 commented Aug 5, 2015

You could also put !! At the beginning of the bind, so that you don't even have to press <up>

@tkt028
Copy link
Author

tkt028 commented Aug 5, 2015

Hi Edi,
Thank for the tip. I highly appreciate that.
If I understand correctly, to press <up> and press CC is to re-execute the previous command and use CC to activate PE. Right?
But in my case, the command takes long time to go, so it is not convenient to re-execute it again just for copying the path into clipboard. Do you have any suggestion for this case?

Best regards,
Khon

P/S:

  • for your suggestion about putting !! at the beginning of the bind, I think it is not flexible, because sometimes I don't really know in advance that I will need to copy the path into clipboard. I want to see the output first, then decide if to copy it later.
  • like the tmux-plugins/tmux-fpp (facebook path picker), it scans the output on the screen for path and give you action menu >> I wish path-extractor have such capability, but I need to do fzf filter and copy to clipboard.

@edi9999
Copy link
Owner

edi9999 commented Aug 5, 2015

I guess that you can mix https://github.com/tmux-plugins/tmux-fpp/blob/master/fpp.tmux and replace fpp by the snippet I put before. I will be integrating this my self tomorrow, and will be posting the full code snippet if you haven't already find out

@edi9999
Copy link
Owner

edi9999 commented Aug 6, 2015

Create a file in ~/scripts/tmux-path-extractor.sh

#!/usr/bin/env bash

copy () {
input=$1
if [ ! -t 0 ]; then
    [ "$input" ] || input=$(cat)
fi

echo -e -n "$input" > ~/.clipboard
which xclip 2>/dev/null && echo -e -n "$input" | xclip -selection c && exit 0
tmux set-buffer "$(echo -e -n $input)"
}

fzf_with_opts="fzf +s --tac +m --tiebreak=index --toggle-sort=ctrl-r"
get_input="cat /tmp/tmux-buffer | grep '.' | grep -v ' \\$ ' | grep -v ' \\\$\$'"

tmux bind-key "f" capture-pane \\\; \
    save-buffer /tmp/tmux-buffer \\\; \
    new-window -c "#{pane_current_path}" "sh -c \"$get_input | pe | uniq | $fzf_with_opts | copy && rm /tmp/tmux-buffer\""

and add

run-shell ~/scripts/tmux-path-extractor.sh

at the end of your tmux.conf

If you press f, you will get to choose your filepath.

Did that work for you ?

@tkt028
Copy link
Author

tkt028 commented Aug 7, 2015

Hi Edi,
Thank you very much! You are so kind.
I did try your script but it didn't work. When I press "f" in tmux, it successfully copied tmux buffer into /tmp/tmux-buffer and pipe into pe and fzf, but the copy() was not visible (I tested it by add "touch abc.txt" inside copy() function).

However, your script is still useful to me. I create an alias and

alias pp="tmux capture-pane && tmux save-buffer /tmp/tmux-buffer && cat /tmp/tmux-buffer | grep '.' | grep -v ' \\$ ' | grep -v ' \\\$\$' | pe | uniq | fzf | xargs echo -n | xclip -i -sel p -f | xclip -i -sel c"

And bind key to that alias in bash, and now it works well for me 💃

bind '"PP": "pp\C-m'

Thank you very much! Have a nice weekend.
Cheers,
Khon

P/S: you can create tmux plugin for path-extractor, I think it is useful. :D

@tkt028 tkt028 closed this as completed Aug 7, 2015
@edi9999
Copy link
Owner

edi9999 commented Aug 7, 2015

I don't exactly see the point of creating a tmux plugin for this. Shouldn't I just put your script into the readme, so that others can use it ? I find it much simpler to have just those two lines, so that you can edit them the way you want if you need it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants