Skip to content

Commit

Permalink
Automatically set /dev/tty as STDIN on execute action
Browse files Browse the repository at this point in the history
junegunn#1360 (comment)

  # Redirect /dev/tty to suppress "Vim: Warning: Input is not from a terminal"
  ls | fzf --bind "enter:execute(vim {} < /dev/tty)"

  # With this change, we can omit "< /dev/tty" part
  ls | fzf --bind "enter:execute(vim {})"
  • Loading branch information
junegunn authored and kralicky committed Jun 23, 2021
1 parent bd4b56b commit 7c02ab1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,7 @@ func (t *Terminal) executeCommand(template string, forcePlus bool, background bo
command := t.replacePlaceholder(template, forcePlus, string(t.input), list)
cmd := util.ExecCommand(command, false)
if !background {
cmd.Stdin = os.Stdin
cmd.Stdin = tui.TtyIn()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
t.tui.Pause(true)
Expand Down
16 changes: 16 additions & 0 deletions src/tui/ttyname_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package tui

import (
"io/ioutil"
"os"
"syscall"
)

Expand All @@ -29,3 +30,18 @@ func ttyname() string {
}
return ""
}

// TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin
func TtyIn() *os.File {
in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)
if err != nil {
tty := ttyname()
if len(tty) > 0 {
if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {
return in
}
}
return os.Stdin
}
return in
}
7 changes: 7 additions & 0 deletions src/tui/ttyname_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

package tui

import "os"

func ttyname() string {
return ""
}

// TtyIn on Windows returns os.Stdin
func TtyIn() *os.File {
return os.Stdin
}

0 comments on commit 7c02ab1

Please sign in to comment.