如何退出Vi或Vim编辑器
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.
如果您不習(xí)慣使用vi編輯器,則會(huì)感到困惑。 如果您偶然發(fā)現(xiàn)該應(yīng)用程序,則需要進(jìn)行一次秘密握手才能退出該應(yīng)用程序。 這是在Linux,macOS或任何其他類(lèi)似Unix的系統(tǒng)上退出vi或vim的方法。
快速解答 (The Quick Answer)
If you’re in vi or vim and need to get out—with or without saving your changes—here’s how:
如果您使用的是vi或vim ,需要保存或不保存所做的更改,那么請(qǐng)按照以下步驟操作:
First, press the Esc key a few times. This will ensure vi is out of Insert mode and in Command mode.
首先,按幾次Esc鍵。 這將確保vi退出插入模式和命令模式。
Second, type :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退出而不保存任何更改。 (如果確實(shí)要保存更改,請(qǐng)鍵入: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.
如果您想學(xué)習(xí)Linux命令行,則需要了解更多。 繼續(xù)閱讀,我們將向您展示vi工作原理以及為什么退出說(shuō)明如此不尋常。 vi是一個(gè)重要而強(qiáng)大的工具,學(xué)習(xí)曲線值得。
vi,無(wú)所不在的編輯器 (vi, The Ubiquitous Editor)
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.
因?yàn)関i無(wú)處不在,所以您將有機(jī)會(huì)與之抗衡。 您甚至可能偶然發(fā)現(xiàn)自己在vi 。 也許有人要您為他們看一下他們的Linux計(jì)算機(jī)。 您發(fā)出諸如crontab -e類(lèi)的命令,然后vi彈出。 令人驚訝的是,有人將crontab的默認(rèn)編輯器配置為 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.
也許您正在管理一個(gè)系統(tǒng),其中vi是唯一的編輯器,或者是唯一將通過(guò)遠(yuǎn)程SSH會(huì)話運(yùn)行的編輯器,并且您需要編輯用戶(hù)的.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.
啟動(dòng)vi和打開(kāi)文件的命令很簡(jiǎn)單。 鍵入vi ,一個(gè)空格,然后輸入文件名。 按Enter鍵。 啟動(dòng)的程序可能是vi ,也可能是vim ,即“改進(jìn)的vi ”。 這取決于您的Linux發(fā)行版-例如,Ubuntu使用vim 。 本文中的所有說(shuō)明同樣適用于vim 。
vi .bashrcThe 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和其他編輯器之間最明顯的區(qū)別是,在啟動(dòng)vi您不能只是開(kāi)始輸入文本。 這是因?yàn)関i是模式編輯器。 編輯是在一種模式(插入模式)下執(zhí)行的,發(fā)出命令是在命令模式下執(zhí)行的。 vi進(jìn)入命令模式。
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.
如果您不熟悉插入模式和命令模式的概念,可能會(huì)感到困惑。 您可以在“命令”模式下發(fā)出的許多命令都會(huì)影響您正在鍵入的文件。 如果您處于“命令”模式,但是錯(cuò)誤地嘗試在文件中鍵入文本,則結(jié)尾將不會(huì)很好。 您發(fā)出的某些按鍵將被識(shí)別為命令。 這些命令可能會(huì)刪除或分割行,四處移動(dòng)光標(biāo)或刪除文本。
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.
而且,無(wú)論您鍵入什么內(nèi)容,都找不到退出編輯器的方法。 同時(shí),您的文件變得非?;靵y,看似隨機(jī)的嗶嗶聲使您發(fā)瘋。
命令模式和插入模式 (Command Mode and Insert Mode)
You need to switch?vi into the appropriate mode for what you’re trying to accomplish.
您需要將vi切換到要完成的任務(wù)的適當(dāng)模式。
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啟動(dòng)時(shí),命令模式是默認(rèn)模式。 除非您對(duì)此有所了解,否則將開(kāi)始嘗試輸入。 如果碰巧按下“ i”鍵或調(diào)用插入模式的其他10個(gè)鍵(a,A,c,C,I,o,O,R,s和S)中的任何一個(gè),您會(huì)突然看到您正在輸入。 您現(xiàn)在處于插入模式。
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.
直到您按下其中一個(gè)箭頭鍵,這似乎才是進(jìn)步。 如果這樣做,則A,B,C或D將作為唯一字母出現(xiàn)在否則為空白的新行上。 在文件的頂部。
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.
沒(méi)關(guān)系,我們得到了您的支持。 當(dāng)您知道如何時(shí),這非常容易。 記住這兩個(gè)按鍵:Esc帶您進(jìn)入命令模式,“ i”帶您進(jìn)入插入模式。
You need to be in Command mode, and to enter the correct command to leave the editor.
您需要處于“命令”模式,并輸入正確的命令才能離開(kāi)編輯器。
從命令模式到安全 (From Command Mode to Safety)
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.
要進(jìn)入命令模式,請(qǐng)按Esc鍵。 什么都看不見(jiàn)。 再打幾次。 如果在按Escape鍵時(shí)聽(tīng)到蜂鳴聲,則說(shuō)明您處于命令模式。 嗶聲告訴您“停止按Esc鍵,您已經(jīng)處于Command模式。” 如果您在按Esc鍵時(shí)聽(tīng)到嗶嗶聲,那就很好。
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:
鍵入一個(gè)冒號(hào),字母“ q”和一個(gè)感嘆號(hào),沒(méi)有任何空格。 這三個(gè)字符應(yīng)顯示在終端的底行的最左端。 如果不是,請(qǐng)按Esc,直到聽(tīng)到提示音,然后重試。 看到它們時(shí),按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的縮寫(xiě)。 感嘆號(hào)增加了重點(diǎn),就像您在喊“退出!” 在vi 。 那可能會(huì)使您感覺(jué)好一些。
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.
感嘆號(hào)還指示vi 不要保存您可能對(duì)該文件所做的任何更改。 如果您在vi一直犯錯(cuò)誤而又不知道自己在做什么,則可能不想保存您造成的破壞。
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:
返回命令行后,您可能需要仔細(xì)檢查以確保文件沒(méi)有被更改。 您可以使用以下命令執(zhí)行此操作:
cat .bashrc | lessWhen 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 ,如果看到“自從上次更改以來(lái)沒(méi)有寫(xiě)過(guò)”的消息,則表示您錯(cuò)過(guò)了命令的感嘆號(hào)。 為了防止您退出并丟失希望保留的任何更改, vi提供了保存更改的機(jī)會(huì)。 只需重新發(fā)出:q! 帶有感嘆號(hào)的命令退出vi并放棄任何更改。
如果確定,請(qǐng)保存更改 (If You’re Sure, Save Your Changes)
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.
如果對(duì)文件所做的更改感到滿意,則可以使用:wq (寫(xiě)入并退出)命令退出并保存更改。 在繼續(xù)操作之前,請(qǐng)確保您完全希望將屏幕編輯寫(xiě)入文件。
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:
輸入冒號(hào),字母w(寫(xiě))和字母q(退出)。 當(dāng)您在終端的左下方看到它們時(shí),請(qǐng)按Enter鍵:
:wq學(xué)習(xí)曲線值得 (The Learning Curve is Worth It)
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有點(diǎn)像使用鋼琴。 您不能只是坐下來(lái)使用它; 您必須進(jìn)行一些練習(xí)。 坐在冷的地方,試圖在壓力下立即學(xué)習(xí)以進(jìn)行編輯,這并不是解決問(wèn)題的方法。 這就像第一次坐鋼琴一樣有意義,就像為您的就職音樂(lè)會(huì)拉開(kāi)帷幕一樣。
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的強(qiáng)大功能主要來(lái)自于許多擊鍵組合,每個(gè)組合都執(zhí)行共同的編輯任務(wù)。 太好了,但是只有記住,練習(xí)它們并且它們是您肌肉記憶的一部分,您才能從中受益。
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.
在此之前,如果您發(fā)現(xiàn)自己在vi查看重要文件,請(qǐng):q! 然后優(yōu)雅地退出。 您的重要文件將感謝您。
翻譯自: https://www.howtogeek.com/411210/how-to-exit-the-vi-or-vim-editor/
總結(jié)
以上是生活随笔為你收集整理的如何退出Vi或Vim编辑器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Mysql常用命令行大全
- 下一篇: 非科班的秋招攻略贴