如何快速将光标恢复到shell命令执行前的先前位置?

huangapple go评论118阅读模式
英文:

How to quickly restore the cursor at the previous position were it was before the command execution in a shell?

问题

这是我经常遇到的情况:我正在shell中编写一个带有大量标志和参数的长命令(这里是fish,但也可以是bash或其他任何东西),所以我在命令中来回移动。最后,我的光标位于命令中间的一个标志后面。我认为我完成了,所以我按回车键执行命令。但命令未能执行,因为我犯了一些错误。所以我按上箭头键,以在提示符中恢复先前的命令以修复它。

但shell不会恢复我的光标位置,它将我的光标移到末尾。这不太实用,因为我需要修复的参数位于命令中间,实际上就在我按回车键之前光标所在的位置。

我正在寻找一种方法,让我的shell在需要时恢复光标位置,例如当我按下某个组合键。这将为我节省将光标导航回正确位置的时间。

我目前正在使用fish shell,但我愿意使用任何其他具有此功能的shell(bash、zsh)。

如何快速在shell中将光标恢复到在执行命令之前的先前位置?

感谢任何建议。

英文:

This is the situation that I'm finding in regularly: I'm crafting a long command in a shell (here fish but could be bash or anything) with a bunch of flags and arguments, so I go back and forth in the command. At the end, my cursor is located just after a flag in the middle of the command. I think I'm done, so I hit enter to execute the command. But the command failed to execute because I made some mistake. So I hit the Up arrow to have the previous command back in the prompt to fix it.

But the shell doesn't restore my cursor position, it brings my cursor at the end. Which is not practical because the argument that I need to fix is located in the middle of the command, in fact at the exact location were my cursor was just before I hit enter.

I'm looking for a way for my shell to restore my cursor position on demand, for example when I hit some key combination. That would save me the time of navigating my cursor back into the right position.

I'm currently using the fish shell but I'm open to use any other shell (bash, zsh) that has this feature.

How to quickly restore the cursor at the previous position were it was before the command execution in a shell?

Thanks for any advice.

答案1

得分: 3

以下是翻译好的部分:

在fish-shell中,你可以通过添加一对函数来实现这一目标。第一个函数保存光标位置并执行命令,第二个函数恢复光标位置:

function save_cursor_and_exec
    set -g SAVED_CURSOR (commandline --cursor)
    commandline -f execute
end

function restore_cursor
    commandline --cursor $SAVED_CURSOR
end

现在,我们将\r(回车)绑定到第一个函数,将控制-R绑定到第二个函数:

bind \r save_cursor_and_exec
bind \cR restore_cursor

现在,在按上箭头后,控制-R将恢复光标到保存的位置。

英文:

One way you can achieve this in fish-shell would be to add a pair of functions. One saves the cursor position and executes the command, the second restores the cursor position:

function save_cursor_and_exec
    set -g SAVED_CURSOR (commandline --cursor)
    commandline -f execute
end

function restore_cursor
    commandline --cursor $SAVED_CURSOR
end

Now we bind \r (enter) to the first function, and control-R to the second:

bind \r save_cursor_and_exec
bind \cR restore_cursor

Now after hitting up arrow, control-R will restore the cursor to the saved position.

huangapple
  • 本文由 发表于 2023年3月7日 23:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定