我尝试fzf | myprogram
时会发生这种非常奇怪的行为,myprogram
依次调用vim
。到vim
启动时,stty
设置已出现问题。我能够在zsh&bash,config&no config,linux&macOS上重现此内容。最小再现:
cargo new myprogram
src/main.rsuse std::io;
fn main() {
let mut file_name = String::new();
io::stdin()
.read_line(&mut file_name)
.expect("Failed to read from stdin");
file_name.retain(|c| !c.is_whitespace());
println!("{}", file_name);
std::process::Command::new("/bin/bash")
.arg("-c")
.arg(format!("vim {}", file_name))
.spawn()
.expect("Error: Failed to run editor")
.wait()
.unwrap()
.success();
}
为了方便起见,我通常:cargo install --path .
当我fzf | myprogram
时,我能够选择一个文件,并且确实按预期在vim
中打开了。但是在insert
模式下,当我退格时,我看到:^?
。当我退出vim和^C
时,它不起作用,它只是插入^C
字符。每个换行符的格式设置不正确,通常很难读取任何输出。经过一些研究,我遇到了
reset
命令。当我调用reset
时,它在每种测试情况下都会输出相同的内容:重置
Erase set to delete.
Kill set to control-U (^U).
Interrupt set to control-C (^C).
我尝试了几种方法来读取stdin
并调用vim
,但是这个问题似乎仍然存在。到底是怎么回事?
最佳答案
@Masklinn启发我创建了一个示例,其中我仅使用cat
和xargs
,但令我惊讶的是,该示例也不起作用!
然后,我遇到了this post,它解释说vim在启动时会修改当前终端,而在关闭时会尝试恢复启动时的配置。除非以非标准方式调用它,否则它不会还原为正确的东西。
我所有的问题都通过告诉rust启动vim来解决,例如:
bash -c '</dev/tty vim "$@"'
而不是像:bash -c 'vim "$@"'
关于shell - fzf,stdin出现问题,并从rust :调用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62668254/