如何在Linux终端上使用vi编辑命令?

如何在Linux终端上使用vi编辑命令?,linux,vim,terminal,Linux,Vim,Terminal,当键入很长的命令时,我希望首先在文本编辑器(例如vi)中编辑该命令,然后在输入错误时执行该命令。是否有一种方法可以直接在终端中编辑命令并运行,而不是通过键入vi来调用vi,然后键入命令?如果您使用的是bash,请尝试使用编辑并执行命令命令。默认情况下,这将分配给Ctrl-x Ctrl-e(键入Ctrl-x,然后键入Ctrl-e) 这将打开环境中指定的任何编辑器。退出时缓冲区中的任何内容都将在shell中执行,包括多行命令。您可以通过设置vi编辑模式来执行。如果您使用的是bash,则可以输入以下内

当键入很长的命令时,我希望首先在文本编辑器(例如vi)中编辑该命令,然后在输入错误时执行该命令。是否有一种方法可以直接在终端中编辑命令并运行,而不是通过键入vi来调用vi,然后键入命令?

如果您使用的是bash,请尝试使用
编辑并执行命令
命令。默认情况下,这将分配给
Ctrl-x Ctrl-e
(键入Ctrl-x,然后键入Ctrl-e)


这将打开环境中指定的任何编辑器。退出时缓冲区中的任何内容都将在shell中执行,包括多行命令。

您可以通过设置vi编辑模式来执行。如果您使用的是bash,则可以输入以下内容或将其放入您的应用程序中:

然后,您可以像在vi中一样使用命令模式和插入模式

当命令已经在线时(通过按向上箭头键、
CTRL-R
fzf
等调用),在shell上处于正常模式时,有用的命令是
v
,因为它将启动默认编辑器来编辑该命令

来自以下网站的备忘:

-----------------------------------------------------------------------------------------。
|                                                                           |
|Readline VI编辑模式|
|Bash的默认键盘快捷键|
|备忘单|
|                                                                           |
'---------------------------------------------------------------------------'
|彼得里斯·克鲁明斯(peter@catonmat.net), 2008.01.08                          |
| http://www.catonmat.net  -优秀的编码人员,代码重用性强|
|                                                                           |
|根据GNU自由文档许可证发布|
'---------------------------------------------------------------------------'
==================================键盘快捷键摘要========================
.--------------.------------------------------------------------------------.
|              |                                                            |
|快捷方式|描述|
|              |                                                            |
'--------------'------------------------------------------------------------'
|切换到命令模式:|
'--------------.------------------------------------------------------------'
|ESC |切换到命令模式|
'--------------'------------------------------------------------------------'
|用于进入输入模式的命令:|
'--------------.------------------------------------------------------------'
|i |在光标前插入|
'--------------+------------------------------------------------------------'
|a |在光标后插入|
'--------------+------------------------------------------------------------'
|I |在行首插入|
'--------------+------------------------------------------------------------'
|A |在行尾插入|
'--------------+------------------------------------------------------------'
|c |更改移动命令的文本(见下文)|
'--------------+------------------------------------------------------------'
|C |将文本更改为行尾(相当于C$)|
'--------------+------------------------------------------------------------'
|cc或S |更改当前行(相当于0c$)|
'--------------+------------------------------------------------------------'
|s |删除光标下的单个字符并输入输入|
||模式(相当于c[空间])|
'--------------+------------------------------------------------------------'
|r |替换光标下的单个字符(不带|
||离开命令模式)|
'--------------+------------------------------------------------------------'
|R |替换光标下的字符|
'--------------+------------------------------------------------------------'
|v |在文本编辑器中编辑(并执行)当前命令|
||(在$VISUAL或$editor变量中定义的编辑器,或vi)|
'--------------'------------------------------------------------------------'
|基本移动命令(在命令模式下):|
'--------------.------------------------------------------------------------'
|h |向右移动一个字符|
'--------------+------------------------------------------------------------'
|l |向左移动一个字符|
'--------------+------------------------------------------------------------'
|w |向右移动一个单词或标记|
'--------------+------------------------------------------------------------'
|b |向左移动一个单词或标记|
'--------------+------------------------------------------------------------'
|W |向右移动一个非空单词|
'--------------+------------------------------------------------------------'
|B |向左移动一个非空单词|
'--------------+----------------------------------
set -o vi
.---------------------------------------------------------------------------.
|                                                                           |
|                          Readline VI Editing Mode                         |
|                     Default Keyboard Shortcuts for Bash                   |
|                               Cheat Sheet                                 |
|                                                                           |
'---------------------------------------------------------------------------'
| Peteris Krumins (peter@catonmat.net), 2008.01.08                          |
| http://www.catonmat.net  -  good coders code, great reuse                 |
|                                                                           |
| Released under the GNU Free Document License                              |
'---------------------------------------------------------------------------'

 ======================== Keyboard Shortcut Summary ========================

.--------------.------------------------------------------------------------.
|              |                                                            |
| Shortcut     | Description                                                |
|              |                                                            |
'--------------'------------------------------------------------------------'
| Switching to COMMAND Mode:                                                |
'--------------.------------------------------------------------------------'
| ESC          | Switch to command mode.                                    |
'--------------'------------------------------------------------------------'
| Commands for Entering INPUT Mode:                                         |
'--------------.------------------------------------------------------------'
| i            | Insert before cursor.                                      |
'--------------+------------------------------------------------------------'
| a            | Insert after cursor.                                       |
'--------------+------------------------------------------------------------'
| I            | Insert at the beginning of line.                           |
'--------------+------------------------------------------------------------'
| A            | Insert at the end of line.                                 |
'--------------+------------------------------------------------------------'
| c<mov. comm> | Change text of a movement command <mov. comm> (see below). |
'--------------+------------------------------------------------------------'
| C            | Change text to the end of line (equivalent to c$).         |
'--------------+------------------------------------------------------------'
| cc or S      | Change current line (equivalent to 0c$).                   |
'--------------+------------------------------------------------------------'
| s            | Delete a single character under the cursor and enter input |
|              | mode (equivalent to c[SPACE]).                             |
'--------------+------------------------------------------------------------'
| r            | Replaces a single character under the cursor (without      |
|              | leaving command mode).                                     |
'--------------+------------------------------------------------------------'
| R            | Replaces characters under cursor.                          |
'--------------+------------------------------------------------------------'
| v            | Edit (and execute) the current command in the text editor. |
|              | (an editor defined in $VISUAL or $EDITOR variables, or vi  |
'--------------'------------------------------------------------------------'
| Basic Movement Commands (in command mode):                                |
'--------------.------------------------------------------------------------'
| h            | Move one character right.                                  |
'--------------+------------------------------------------------------------'
| l            | Move one character left.                                   |
'--------------+------------------------------------------------------------'
| w            | Move one word or token right.                              |
'--------------+------------------------------------------------------------'
| b            | Move one word or token left.                               |
'--------------+------------------------------------------------------------'
| W            | Move one non-blank word right.                             |
'--------------+------------------------------------------------------------'
| B            | Move one non-blank word left.                              |
'--------------+------------------------------------------------------------'
| e            | Move to the end of the current word.                       |
'--------------+------------------------------------------------------------'
| E            | Move to the end of the current non-blank word.             |
'--------------+------------------------------------------------------------'
| 0            | Move to the beginning of line                              |
'--------------+------------------------------------------------------------'
| ^            | Move to the first non-blank character of line.             |
'--------------+------------------------------------------------------------'
| $            | Move to the end of line.                                   |
'--------------+------------------------------------------------------------'
| %            | Move to the corresponding opening/closing bracket.         |
'--------------'------------------------------------------------------------'
| Character Finding Commands (these are also Movement Commands):            |
'--------------.------------------------------------------------------------'
| fc           | Move right to the next occurance of char c.                |
'--------------+------------------------------------------------------------'
| Fc           | Move left to the previous occurance of c.                  |
'--------------+------------------------------------------------------------'
| tc           | Move right to the next occurance of c, then one char       |
|              | backward.                                                  |
'--------------+------------------------------------------------------------'
| Tc           | Move left to the previous occurance of c, then one char    |
|              | forward.                                                   |
'--------------+------------------------------------------------------------'
| ;            | Redo the last character finding command.                   |
'--------------+------------------------------------------------------------'
| ,            | Redo the last character finding command in opposite        |
|              | direction.                                                 |
'--------------+------------------------------------------------------------'
| |            | Move to the n-th column (you may specify the argument n by |
|              | typing it on number keys, for example, 20|)                |
'--------------'------------------------------------------------------------'
| Deletion Commands:                                                        |
'--------------.------------------------------------------------------------'
| x            | Delete a single character under the cursor.                |
'--------------+------------------------------------------------------------'
| X            | Delete a character before the cursor.                      |
'--------------+------------------------------------------------------------'
| d<mov. comm> | Delete text of a movement command <mov. comm> (see above). |
'--------------+------------------------------------------------------------'
| D            | Delete to the end of the line (equivalent to d$).          |
'--------------+------------------------------------------------------------'
| dd           | Delete current line (equivalent to 0d$).                   |
'--------------+------------------------------------------------------------'
| CTRL-w       | Delete the previous word.                                  |
'--------------+------------------------------------------------------------'
| CTRL-u       | Delete from the cursor to the beginning of line.           |
'--------------'------------------------------------------------------------'
| Undo, Redo and Copy/Paste Commands:                                       |
'--------------.------------------------------------------------------------'
| u            | Undo previous text modification.                           |
'--------------+------------------------------------------------------------'
| U            | Undo all previous text modifications.                      |
'--------------+------------------------------------------------------------'
| .            | Redo the last text modification.                           |
'--------------+------------------------------------------------------------'
| y<mov. comm> | Yank a movement into buffer (copy).                        |
'--------------+------------------------------------------------------------'
| yy           | Yank the whole line.                                       |
'--------------+------------------------------------------------------------'
| p            | Insert the yanked text at the cursor.                      |
'--------------+------------------------------------------------------------'
| P            | Insert the yanked text before the cursor.                  |
'--------------'------------------------------------------------------------'
| Commands for Command History:                                             |
'--------------.------------------------------------------------------------'
| k            | Move backward one command in history.                      |
'--------------+------------------------------------------------------------'
| j            | Move forward one command in history.                       |
'--------------+------------------------------------------------------------'
| G            | Move to history line N (for example, 15G).                 |
'--------------+------------------------------------------------------------'
| /string or   | Search history backward for a command matching string.     |
| CTRL-r       |                                                            |
'--------------+------------------------------------------------------------'
| ?string or   | Search history forward for a command matching string.      |
| CTRL-s       | (Note that on most machines Ctrl-s STOPS the terminal      |
|              | output, change it with `stty' (Ctrl-q to resume)).         |
'--------------+------------------------------------------------------------'
| n            | Repeat search in the same direction as previous.           |
'--------------+------------------------------------------------------------'
| N            | Repeat search in the opposite direction as previous.       |
'--------------'------------------------------------------------------------'
| Completion commands:                                                      |
'--------------.------------------------------------------------------------'
| TAB or = or  | List all possible completions.                             |
| CTRL-i       |                                                            |
'--------------+------------------------------------------------------------'
| *            | Insert all possible completions.                           |
'--------------'------------------------------------------------------------'
| Miscellaneous commands:                                                   |
'--------------.------------------------------------------------------------'
| ~            | Invert case of the character under cursor and move a       |
|              | character right.                                           |
'--------------+------------------------------------------------------------'
| #            | Prepend '#' (comment character) to the line and send it to |
|              | the history.                                               |
'--------------+------------------------------------------------------------'
| _            | Inserts the n-th word of the previous command in the       |
|              | current line.                                              |
'--------------+------------------------------------------------------------'
| 0, 1, 2, ... | Sets the numeric argument.                                 |
'--------------+------------------------------------------------------------'
| CTRL-v       | Insert a character literally (quoted insert).              |
'--------------+------------------------------------------------------------'
| CTRL-r       | Transpose (exchange) two characters.                       |
'--------------'------------------------------------------------------------'


 ===========================================================================

.---------------------------------------------------------------------------.
| Peteris Krumins (peter@catonmat.net), 2008.01.08.                         |
| http://www.catonmat.net  -  good coders code, great reuse                 | 
|                                                                           |
| Released under the GNU Free Document License                              |
'---------------------------------------------------------------------------'
bindkey "^X^E" edit-command-line