在命令行输入的文本上启动 vim 的快捷方式

A shortcut to fire up vim on the text typed on the command line

我在命令行中输入了一个很长的命令 – 才发现 Bash 不喜欢嵌入的单引号。这是一个简单的 tty,我从文件中为 运行 保存此命令的唯一方法是打开 vim 并再次开始输入这个很长的命令...

在命令行输入的文本上没有启动 vim 的快捷方式 – 直接从命令行?

当然有。以下命令将编辑 $VISUAL 中的当前命令行:

C-x C-e

我找到了我的案例的答案here:

首先关闭来自@marlar的警告(我为自己设置了一个保护自己的功能 由此,见下文):

Be very careful with this feature. If you cancel the edit, the original command line will be immediately executed. So if you are editing rm -rf <forward slash> and invoke the editor and realize you are into something dangerous and thus cancel the edit, your root file system will be deleted without further questions asked.

来自 OP @Kartik:

bash 手册页说:

edit-and-execute-command (C-xC-e) Invoke an editor on the current command line, and execute the result as shell commands. Bash attempts to invoke $VISUAL, $EDITOR, and emacs as the editor, in that order.

来自@Mark E. Haase

If you use Bash's vi mode, the short cut is Esc, V...

我实际上也发现只做 v 就足以用 bash 启动 vim 命令坐在那里。

来自@karlicoss:

if your editor is vim, you can use :cq, it makes it exit with non-zero code, and prevents the command from execution

因为我对rm -rf命令很偏执(我曾经删除过2Tb的大脑 数据 - 切换到 linux 10 天...幸运的是系统管理员有一个 备份...实际引用:“我 20 年来从未见过如此愚蠢的事情 做这个工作”......但我离题了)我把以下内容放在我的 vimrc 中 如果我不小心调出 vim 和 条件反射退出:

function! CheckBashEdit()
    " if the file matches this highly specific reg exp, comment the line
    "(e.g. a file that looks like: /tmp/bash-fc.xxxxxx)
    if match(@%, "\/tmp\/bash-fc\.......") != -1
        " comment out the command
        silent! execute ":%normal! I# "
        write
    endif
endfunction

" if we ended up in vim by pressing <ESC-v>, put a # at the beggining of
" the line to prevent accidental execution (since bash will execute no
" matter what! Imagine rm -rf <forward slash> was there...)
autocmd BufReadPost * :call CheckBashEdit()