vi使用入门_使用Vi编辑文本文件的入门指南
vi使用入门
Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you’ll have to edit a text file on a system that doesn’t include a friendlier text editor, so knowing Vi is essential.
Vi是大多数Linux系统(甚至嵌入式系统)中都包含的功能强大的文本编辑器。 有时,您必须在不包含友好文本编辑器的系统上编辑文本文件,因此了解Vi是必不可少的。
Unlike Nano, an easy-to-use terminal text editor, Vi doesn’t hold your hand and provide a list of keyboard shortcuts on the screen. It’s a modal text editor, and it has both an insert and command mode.
与Nano(易于使用的终端文本编辑器)不同,Vi不会握住您的手,而是在屏幕上提供键盘快捷键的列表。 这是一个模式文本编辑器,具有插入和命令模式。
入门 (Getting Started)
Vi is a terminal application, so you’ll have to start it from a terminal window. Use the vi /path/to/file command to open an existing file with Vi. The vi /path/to/file command also works if the file doesn’t exist yet; Vi will create a new file and write it to the specified location when you save.
Vi是一个终端应用程序,因此您必须从终端窗口启动它。 使用vi / path / to / file命令可使用Vi打开现有文件。 如果文件尚不存在, vi / path / to / file命令也可以使用。 保存时,Vi将创建一个新文件并将其写入指定的位置。
Remember to use sudo if you want to edit a system file. So, for example, you’d type sudo vi /etc/fstab if you wanted to edit your fstab file. Use the su command instead if you’re using a non-Ubuntu version of Linux that doesn’t use sudo.
如果要编辑系统文件,请记住使用sudo。 因此,例如,如果要编辑fstab文件,则输入sudo vi / etc / fstab 。 如果您使用的是不使用sudo的非Ubuntu版本Linux,请改用su命令。
命令模式 (Command Mode)
This is what you’ll see when you open a file in vi. It looks like you can just start typing, but you can’t. Vi is a modal text editor, and it opens in command mode. Trying to type at this screen will result in unexpected behavior.
这是在vi中打开文件时会看到的内容。 看来您可以开始输入,但不能。 Vi是模式文本编辑器,它在命令模式下打开。 尝试在此屏幕上键入将导致意外行为。
While in command mode, you can move the cursor around with the arrow keys. Press the x key to delete the character under the cursor. There are a variety of other delete commands — for example, typing dd (press the d key twice) deletes an entire line of text.
在命令模式下,您可以使用箭头键来移动光标。 按x键删除光标下方的字符。 还有多种其他删除命令-例如,键入dd (两次按d键)将删除整行文本。
You can select, copy, cut and paste text in command mode. Position the cursor at the left or right side of the text you want to copy and press the v key. Move your cursor to select text, and then press y to copy the selected text or x to cut it. Position your cursor at the desired location and press the p key to paste the text you copied or cut.
您可以在命令模式下选择,复制,剪切和粘贴文本。 将光标放在要复制的文本的左侧或右侧,然后按v键。 移动光标选择文本,然后按y复制所选文本或按x剪切文本。 将光标置于所需位置,然后按p键粘贴您复制或剪切的文本。
插入模式 (Insert Mode)
Aside from command mode, the other mode you need to know about is insert mode, which allows you to insert text in Vi. Entering insert mode is easy once you know it exists — just press the i key once after you’ve positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands.
除了命令模式外,您还需要了解的另一种模式是插入模式,该模式允许您在Vi中插入文本。 一旦知道插入模式的存在,便可以轻松进入它-将光标置于命令模式后只需按一次i键。 开始输入,Vi会将您输入的字符插入文件中,而不是尝试将它们解释为命令。
Once you’re done in insert mode, press the escape key to return to command mode.
在插入模式下完成操作后,请按Escape键以返回命令模式。
保存并退出 (Saving and Quitting)
You can save and quit vi from command mode. First, ensure you’re in command mode by pressing the escape key (pressing the escape key again does nothing if you’re already in command mode.)
您可以从命令模式保存并退出vi。 首先,通过按Escape键确保您处于命令模式(如果您已经处于命令模式,则再次按Escape键不会执行任何操作。)
Type :wq and press enter to write the file to disk and quit vi. You can also split this command up — for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.
键入:wq ,然后按Enter键将文件写入磁盘并退出vi。 您也可以拆分此命令-例如,键入:w并按Enter键将文件写入磁盘而不退出,或者键入:q退出vi而不保存文件。
Vi won’t let you quit if you’ve modified the file since you last saved, but you can type :q! and press enter to ignore this warning.
如果您自从上次保存以来修改了文件,Vi不会让您退出,但是您可以键入:q!。 然后按Enter键可忽略此警告。
Check out Nano if you’re looking for an easier-to-use terminal text editor. Most Linux distributions come with Nano installed, but embedded systems and other stripped-down environments often only include Vi.
如果您正在寻找易于使用的终端文本编辑器, 请查看Nano 。 大多数Linux发行版都安装了Nano,但嵌入式系统和其他精简环境通常仅包含Vi。
翻译自: https://www.howtogeek.com/102468/a-beginners-guide-to-editing-text-files-with-vi/
vi使用入门