大家好,又见面了,我是你们的朋友全栈君。
The vi
editor is confusing if you’re not used to it. It takes a secret handshake to escape this application if you’ve stumbled into it. Here’s how to quit vi or vim on Linux, macOS, or any other Unix-like system.
如果您不习惯使用vi
编辑器,则会感到困惑。 如果您偶然发现该应用程序,则需要进行一次秘密握手才能退出该应用程序。 这是在Linux,macOS或任何其他类似Unix的系统上退出vi或vim的方法。
If you’re in vi
or vim
and need to get out—with or without saving your changes—here’s how:
如果您使用的是vi
或vim
,需要保存或不保存所做的更改,那么请按照以下步骤操作:
vi
is out of Insert mode and in Command mode.
首先,按几次Esc键。 这将确保vi
退出插入模式和命令模式。
:q!
and press Enter. This tells vi
to quit without saving any changes. (If you do want to save your changes, type :wq
instead.)
其次,输入:q!
然后按Enter。 这告诉vi
退出而不保存任何更改。 (如果确实要保存更改,请键入:wq
。)
If you want to learn the Linux command line, you’ll need to know a lot more than that. Read on and we’ll show you just how vi
works and why the instructions for quitting are so unusual. vi
is an important, powerful tool and the learning curve is worth it.
如果您想学习Linux命令行,则需要了解更多。 继续阅读,我们将向您展示vi
工作原理以及为什么退出说明如此不寻常。 vi
是一个重要而强大的工具,学习曲线值得。
Because vi
is everywhere the chances are you’re going to run up against it. You can even find yourself inside vi
by accident. Perhaps someone asks you to look at their Linux computer for them. You issue a command like crontab -e
, and vi
pops up. Surprise, someone has configured the default editor for crontab
to be vi
.
因为vi
无处不在,所以您将有机会与之抗衡。 您甚至可能偶然发现自己在vi
。 也许有人要您为他们看一下他们的Linux计算机。 您发出诸如crontab -e
类的命令,然后vi
弹出。 令人惊讶的是,有人将crontab
的默认编辑器配置为 vi
。
Perhaps you’re administering a system where vi
is the only editor, or the only one that will work through a remote SSH session, and you need to edit a user’s .bashrc file.
也许您正在管理一个系统,其中vi
是唯一的编辑器,或者是唯一将通过远程SSH会话运行的编辑器,并且您需要编辑用户的.bashrc文件。
The command to start vi
and open a file is straight forward. Type vi
, a space, and then the filename. Press Enter. The program that is launched might be vi
or it might be vim
, an ‘improved vi
‘. It depends on your Linux distribution—for example, Ubuntu uses vim
. All of the instructions in this article apply equally to vim
.
启动vi
和打开文件的命令很简单。 键入vi
,一个空格,然后输入文件名。 按Enter键。 启动的程序可能是vi
,也可能是vim
,即“改进的vi
”。 这取决于您的Linux发行版-例如,Ubuntu使用vim
。 本文中的所有说明同样适用于vim
。
vi .bashrc
The immediately noticeable difference between vi
and other editors is that when vi
launches you can’t just start typing text. That’s because vi
is a modal editor. Editing is performed in one mode, the Insert mode, and issuing commands is performed in the Command mode. vi
launches into Command mode.
vi
和其他编辑器之间最明显的区别是,在启动vi
您不能只是开始输入文本。 这是因为vi
是模式编辑器。 编辑是在一种模式(插入模式)下执行的,发出命令是在命令模式下执行的。 vi
进入命令模式。
If you’re unfamiliar with the concept of Insert mode and Command mode, it can be baffling. A great many of the commands that you can issue in Command mode affect the file you’re typing. If you are in Command mode but you’re mistakenly trying to type text into your file, it isn’t going to end well. Some of the keystrokes you issue will be recognized as commands. Those commands are liable to delete or split lines, move the cursor around, or delete text.
如果您不熟悉插入模式和命令模式的概念,可能会感到困惑。 您可以在“命令”模式下发出的许多命令都会影响您正在键入的文件。 如果您处于“命令”模式,但是错误地尝试在文件中键入文本,则结尾将不会很好。 您发出的某些按键将被识别为命令。 这些命令可能会删除或分割行,四处移动光标或删除文本。
And, no matter what you type, you can’t find a way to exit or quit from the editor. Meanwhile, your file is getting pretty mangled and the seemingly random beeps are driving you crazy.
而且,无论您键入什么内容,都找不到退出编辑器的方法。 同时,您的文件变得非常混乱,看似随机的哔哔声使您发疯。
You need to switch vi
into the appropriate mode for what you’re trying to accomplish.
您需要将vi
切换到要完成的任务的适当模式。
Command mode is the default mode when vi
launches. Unless you know better, you’ll start trying to type. If you happen to hit the ‘i’ key, or any of the other 10 keys that invoke Insert mode (a, A, c, C, I, o, O, R, s, and S) you’ll suddenly see what you’re typing. You’re now in Insert mode.
vi
启动时,命令模式是默认模式。 除非您对此有所了解,否则将开始尝试输入。 如果碰巧按下“ i”键或调用插入模式的其他10个键(a,A,c,C,I,o,O,R,s和S)中的任何一个,您会突然看到您正在输入。 您现在处于插入模式。
This might feel like progress until you hit one of the arrow keys. If you do that, A, B, C, or D will appear as the only letter on an otherwise blank new line. At the top of the file.
直到您按下其中一个箭头键,这似乎才是进步。 如果这样做,则A,B,C或D将作为唯一字母出现在否则为空白的新行上。 在文件的顶部。
It’s OK, we’ve got your back. This is surprisingly easy when you know how. Remember these two keystrokes: Esc takes you to Command mode and “i” takes you to Insert mode.
没关系,我们得到了您的支持。 当您知道如何时,这非常容易。 记住这两个按键:Esc带您进入命令模式,“ i”带您进入插入模式。
You need to be in Command mode, and to enter the correct command to leave the editor.
您需要处于“命令”模式,并输入正确的命令才能离开编辑器。
To enter Command mode, hit the Esc key. Nothing visible will happen. Hit it a few more times. If you hear a beep when you hit the Escape key, then you’re in Command mode. The beep is telling you “Stop pressing Esc, you’re in Command mode, already.” If you hear a beep when you hit Esc, we’re good.
要进入命令模式,请按Esc键。 什么都看不见。 再打几次。 如果在按Escape键时听到蜂鸣声,则说明您处于命令模式。 哔声告诉您“停止按Esc键,您已经处于Command模式。” 如果您在按Esc键时听到哔哔声,那就很好。
Type a colon, the letter “q,” and an exclamation point, without any spaces. These three characters should appear at the far left of the bottom line of the terminal. If they don’t, hit Esc until you hear a beep, and try again. Press the Enter key when you can see them:
键入一个冒号,字母“ q”和一个感叹号,没有任何空格。 这三个字符应显示在终端的底行的最左端。 如果不是,请按Esc,直到听到提示音,然后重试。 看到它们时,按Enter键:
:q!
In this command q
is an abbreviation for quit
. The exclamation point adds emphasis, so it’s like you’re shouting “Quit!” at vi
. That might make you feel a little better.
在此命令中q
是quit
的缩写。 感叹号增加了重点,就像您在喊“退出!” 在vi
。 那可能会使您感觉好一些。
The exclamation point also instructs vi
to not save any of the changes you may have made to the file. If you’ve been blundering about in vi
and not knowing what you’re doing you probably don’t want to save the havoc you’ve wreaked.
感叹号还指示vi
不要保存您可能对该文件所做的任何更改。 如果您在vi
一直犯错误而又不知道自己在做什么,则可能不想保存您造成的破坏。
Once you’re back at the command line you might want to double-check to make sure the file hasn’t been altered. You can do this with the following command:
返回命令行后,您可能需要仔细检查以确保文件没有被更改。 您可以使用以下命令执行此操作:
cat .bashrc | less
When you are exiting vi
, if you see a message saying “no write since last change,” it means you missed the exclamation point off the command. To prevent you quitting and losing any changes you might wish to keep, vi
is giving you the chance to save them. Just reissue the :q!
command with the exclamation point in place to exit from vi
and abandon any changes.
退出vi
,如果看到“自从上次更改以来没有写过”的消息,则表示您错过了命令的感叹号。 为了防止您退出并丢失希望保留的任何更改, vi
提供了保存更改的机会。 只需重新发出:q!
带有感叹号的命令退出vi
并放弃任何更改。
If you’re happy with the changes you’ve made to your file, you can exit and save the changes using the :wq
(write and quit) command. Make sure you are entirely satisfied that you want your screen edits written to the file before you proceed.
如果对文件所做的更改感到满意,则可以使用:wq
(写入并退出)命令退出并保存更改。 在继续操作之前,请确保您完全希望将屏幕编辑写入文件。
Type a colon, the letter w (write) and the letter q (quit). Press the Enter key when you can see them in the lower left of the terminal:
输入冒号,字母w(写)和字母q(退出)。 当您在终端的左下方看到它们时,请按Enter键:
:wq
Using vi
is a bit like using a piano. You can’t just sit down and use it; you’ve got to put in some practice. Sitting down to it cold and trying to learn on the fly when the pressure is on you to get something edited is not the way to do it. It makes as much sense as sitting down to a piano for the first time just as the curtain raises for your inaugural concert.
使用vi
有点像使用钢琴。 您不能只是坐下来使用它; 您必须进行一些练习。 坐在冷的地方,试图在压力下立即学习以进行编辑,这并不是解决问题的方法。 这就像第一次坐钢琴一样有意义,就像为您的就职音乐会拉开帷幕一样。
Much of the power of vi
comes from its many keystroke combinations that each perform a common editing task. That’s great, but you can’t benefit from them until you have memorized them, practiced them, and they’re part of your muscle memory.
vi
的强大功能主要来自于许多击键组合,每个组合都执行共同的编辑任务。 太好了,但是只有记住,练习它们并且它们是您肌肉记忆的一部分,您才能从中受益。
Until then, if you find yourself in vi
and looking at an important file, just :q!
and exit gracefully. Your important file will thank you.
在此之前,如果您发现自己在vi
查看重要文件,请:q!
然后优雅地退出。 您的重要文件将感谢您。
翻译自: https://www.howtogeek.com/411210/how-to-exit-the-vi-or-vim-editor/
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/187641.html原文链接:https://javaforall.cn