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的方法。

快速解答 (The Quick Answer)

If you’re in vi or vim and need to get out—with or without saving your changes—here’s how:

如果您使用的是vivim ,需要保存或不保存所做的更改,那么请按照以下步骤操作:

  • 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退出而不保存任何更改。 (如果确实要保存更改,请键入: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是一个重要而强大的工具,学习曲线值得。

vi,无所不在的编辑器 (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.

因为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.

而且,无论您键入什么内容,都找不到退出编辑器的方法。 同时,您的文件变得非常混乱,看似随机的哔哔声使您发疯。

命令模式和插入模式 (Command Mode and Insert Mode)

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.

您需要处于“命令”模式,并输入正确的命令才能离开编辑器。

从命令模式到安全 (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.

要进入命令模式,请按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.

在此命令中qquit的缩写。 感叹号增加了重点,就像您在喊“退出!” 在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 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.

如果对文件所做的更改感到满意,则可以使用: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

学习曲线值得 (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有点像使用钢琴。 您不能只是坐下来使用它; 您必须进行一些练习。 坐在冷的地方,试图在压力下立即学习以进行编辑,这并不是解决问题的方法。 这就像第一次坐钢琴一样有意义,就像为您的就职音乐会拉开帷幕一样。

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/

如何退出Vi或Vim编辑器相关推荐

  1. Linux(2) vi和vim编辑器

    Linux(2) vi和vim编辑器 学习视频: https://www.bilibili.com/video/BV1Sv411r7vd?p=16&spm_id_from=pageDriver ...

  2. linux vi或者vim编辑器中如何显示行号

    设置行号很简单 我们要到vi或者vim编辑器的末行模式下,输入set number :set number 按下回车就显示行号了 那么怎么关闭行号呢? 只要再到vi或者vim编辑器的末行模式下输入se ...

  3. 大数据技术之_01_Linux学习_01_linux的入门+VM和linux的安装+linux的目录结构+远程登录到linux服务器+vi和vim编辑器+开机、重启和用户登录注销+用户管理+用户组管理

    大数据学习之_01_Linux学习_01 1 linux的入门 1.1 Linux的介绍 2 VM和linux的安装 2.1 安装vm和Centos 2.1.1 基本说明 2.1.2 CentOS安装 ...

  4. Linux — 实操篇_01(XShellXftp的使用、vi和vim编辑器入门、实操命令_用户管理)

    文章目录 一.远程访问工具 1.1 远程登录软件 XShell 1.2 远程上传下载软件 Xftp 1.3 注意 1.4 XShell新建会话 1.5 Xftp新建会话 二.vi和vim编辑器 2.1 ...

  5. Linux系列3:vi与vim编辑器

    vi与vim编辑器 0 引言 1 vi的使用 1.1 vi的模式介绍 1.2 简易示例 1.3 功能键介绍 1.4 案例练习 1.5 vim的暂存,恢复与开启时的警告 2 vim的额外功能 2.1 区 ...

  6. linux实操篇之-----vi和vim编辑器

    vi和vim编辑器 1. vi和vim的基本介绍 所有Linux系统都会内置vi文本编辑器 vim是vi的升级版,可以主动以字体颜色分辨语法的正确性,代码补完和编译,错误跳转等功能. 2. vi和vi ...

  7. Linux学习(三)--vi和vim编辑器

    Linux学习–vi和vim编辑器 一.基本介绍   首先,我们需要明白一点,所有的Linux系统内都会默认安装vi文本编辑器:   其次,vim可以看做是vi的增强版,具有程序编辑的能力,可以主动的 ...

  8. 学习vi和vim编辑器(8):全局替换(1)

    本章学习vi编辑器中的全局替换命令.通过全局替换命令,可以自动替换文件中所有出现过的某个单词.全局替换一般会用到两个ex命令:":g"(global),":s" ...

  9. VI 与 VIM 编辑器实操演示(动图)

    本文通过实操直观演示VI与VIM编辑器的基础操作: 简介 vi 是"Visual interface"的简称,它在 Linux 上的地位就仿佛 Edit 程序在 DOS 上一样. ...

最新文章

  1. 独家揭秘!阿里大规模数据中心的性能分析
  2. python 列表去重(数组)的几种方法
  3. 本地git存储库关闭_Git 入门:术语基础 | Linux 中国
  4. SQL SERVER 2000安装遇到的问题小汇总(转载)
  5. 原型设计真的对用户体验那么重要吗?
  6. 夸克浏览器怎么安装脚本_还你清爽流畅!这五款手机浏览器!黑马强推
  7. MySQL命令梳理_MySQL操作命令梳理(1)
  8. python基础十一之迭代器和生成器
  9. python程序设计简明教程知识点总结_Python程序设计简明教程 在线阅读
  10. 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。
  11. 【图像增强】基于matlab区域相似变换函数和蜻蜓算法灰度图像增强【含Matlab源码 089期】
  12. iReport 生成pdf分页问题
  13. LTE FDD 时频资源
  14. 中国网页游戏行业调研与分析
  15. android 矢量粒子动画,Android碎裂的粒子效果
  16. 详解Maven多模块Spring Boot项目从创建到打包
  17. linux系统Redis下载安装步骤
  18. 产品经理需要掌握的十大知识模块
  19. 全民拼购系统介绍 全民拼购功能分析
  20. 【动态规划】买卖股票

热门文章

  1. 这 10 种 MySQL 经典错误案例,99% 的程序员一定遇到过!你呢?
  2. flutter 微信登录 包名不对,请检查包名是否与开放平台填写一致
  3. 继电器模块的的使用方法、引脚说明、内部结构、接线说明(超通俗超详细看了就能给别人讲)
  4. 数字基带信号及其频谱特性
  5. 【百度知道】拉弧圈球时,总感觉吃不透板是怎么回事啊?
  6. NYOJ-弟弟的作业
  7. 湖南省第六届大学生计算机程序设计竞赛---弟弟的作业
  8. (翻译)所见即所得(WYSIWYG)
  9. 打印机无法打印,配置端口显示错误?
  10. 《Google Android开发入门与实战随书视频》