问题
When typing a very long command, I'd like to first edit the command in a text editor(e.g. vi) then execute in case of typos. Is there a way to edit the command directly in a terminal and run instead of invoke vi by typing vi then type the command?
回答1:
If you're using bash, try the edit-and-execute-command command. By default, this is assigned to Ctrl-x Ctrl-e (type ctrl-x, then ctrl-e).
This should open whatever editor is specified in your environment. Whatever is in the buffer when you exit will execute in the shell - including multiple-line commands.
回答2:
You can by setting vi editing mode. If you are using bash, you can enter:
set -o vi
You can then, just like in vi use command mode and insert mode.
回答3:
If you are using zsh the shell-command is called edit-command-line. It is not bound by default, so add something like this to your configuration:
bindkey "^X^E" edit-command-line
Now Ctrl+xCtrl+e will work the same way as in bash except that the command is not executed before Return is struck.
回答4:
Have you tried the fc ("fix command") shell built-in?1
By default, it opens the very last command in your editor, but you can discard that and replace it with whatever you wish, and it gets executed on exit. See help fc.
Idea suggested by Tom Ryder in his blog post, Vi mode in Bash.
(1) Available in fish, zsh, bash, dash, but probably in others as well.
回答5:
If you are using the fish shell instead of bash you can open the current command in an editor with:
Alt-v (which uses the editor set in the VISUAL environment variable)
Alt-e (which uses the editor set in the EDITOR environment variable)
来源:https://stackoverflow.com/questions/22655587/how-to-use-vi-to-edit-a-command-in-terminal-on-linux