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

Automatically insert newline in bash command line completion #222

Closed
hakonhagland opened this issue May 2, 2015 · 8 comments
Closed

Automatically insert newline in bash command line completion #222

hakonhagland opened this issue May 2, 2015 · 8 comments

Comments

@hakonhagland
Copy link

Is it possible to insert an enter key stroke after fzf returns with a single candidate in bash completion?
I am thinking about the case where the completed word is the last word on a command line, such that the next ting the user will do is to accept and execute the command by pressing enter.

Usually in bash completion when you press tab and there is only a single candidate, it will insert the candidate directly as replacement for the current word on the command line. Then, if this is the last possible word on the command line, you would press enter to accept. It is ok and correct to have to confirm with pressing the enter key in this case.

However, when I use fzf to select a candidate, I first press enter first to select the candidate in fzf, but when returning to the command line, I have to press enter again to execute the command. It feels like I have to accept the same candidate twice.. And it is distracting my workflow..

I am not say that this should be the general behavior; only if the completed word is last on the line, there should be an option to have the command be exectued immediately by pressing enter in fzf..

Any thoughts about this issue?

@junegunn
Copy link
Owner

junegunn commented May 2, 2015

Hmm, I don't think the behavior is generally desirable. Take this for example: you want to copy/move several files from different directories to some destination path.

  • mv ~/aaa/**<TAB>
  • mv ~/aaa/foo/bar/baz ~/bbb/**<TAB>
  • mv ~/aaa/foo/bar/baz ~/bbb/hello/world
  • mv ~/aaa/foo/bar/baz ~/bbb/hello/world ~/trash<Enter>

It's simply not possible to tell if the completed word will be the last argument to the command.
But nothing stops you from customizing/extending the completion code to your liking if you really want, though I'm not sure if it's possible in bash to feed an enter key after completion.

@hakonhagland
Copy link
Author

Yes I agree that in the general case it not possible and not desirable. But if we consider specific cases, for example if I make my own command and would try to use fzf for command line completion. Then I would have full controll over what is the expected arguments, and whether the completed word will be the last one, when I am calling fzf..

So we are looking for a way to feed an enter key into Bash command from a Bash completion function..

@junegunn
Copy link
Owner

junegunn commented May 2, 2015

Yes, and maybe we could apply --expect option to take another key for telling bash to feed an extra enter key. (if that's possible)

@hakonhagland
Copy link
Author

seems like running

xdotool key ctrl+j >/dev/null 2>&1

after fzf returns, is working.. I will do some more testing.

@pokey
Copy link
Contributor

pokey commented Dec 2, 2015

+1 would be great to have alternate key that I can press that would paste selection followed by newline to shell

@Chrysostomus
Copy link

If you just wish fzf to execute the selected command immediately, how about writing a shell script along the lines of dmenu_run? Something along the lines of

#!/bin/sh
sh -c $(ls --color=never -1 --file-type /usr/bin | fzf)

Or another script for opening files:
#!/bin/sh
xdg-open $(fzf)

@ConSeannery
Copy link

This may not be explicitly related to bash completion, but considering this is the only link that comes up when searching for fzf + automatic carriage return and related, quick reminder to others that you can add a \C-m to the end of your key-bindings to automatically hit enter

Eg:

.fzf/shell/key-bindings.bash
   # CTRL-R section, make a selected history item run immediately rather than solely pasting
   # to the command line
    - bind '"\C-r": "\C-x\C-addi$(__fzf_history__)\C-x\C-e\C-x^\C-x\C-a$a\C-x\C-r"'
    + bind '"\C-r": "\C-x\C-addi$(__fzf_history__)\C-x\C-e\C-x^\C-x\C-a$a\C-x\C-r\C-m"'

Remember, there are two sections with these bindings defined (depending on whether you use set -o vi).

@junegunn
Copy link
Owner

junegunn commented Oct 8, 2016

@ConSeannery The problem with the approach is that you can no longer edit the command. On zsh it's possible to use --expect option to allow the user to dynamically decide which action to take, i.e. enter to edit, ctrl-x to directly execute (See #477). Unfortunately I couldn't find a way to do the same on bash. One workaround is to add another key binding such as ctrl-x ctrl-r to use your suggestion.

bind "$(bind -s | grep '^"\\C-r"' | sed 's/"/"\\C-x/' | sed 's/"$/\\C-m"/')"

@junegunn junegunn closed this as completed Oct 8, 2016
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

5 participants