mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
Translated (mengxinayan)
File name: 20200219 Basic Vim Commands You Need to Know to Work in Vim Editor.md Translator: mengxinayan
This commit is contained in:
parent
afe70fd5e1
commit
5b67b02956
@ -1,168 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (mengxinayan)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (Basic Vim Commands You Need to Know to Work in Vim Editor)
|
|
||||||
[#]: via: (https://www.2daygeek.com/basic-vim-commands-cheat-sheet-quick-start-guide/)
|
|
||||||
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
|
|
||||||
|
|
||||||
Basic Vim Commands You Need to Know to Work in Vim Editor
|
|
||||||
======
|
|
||||||
|
|
||||||
If you are a system administrator or developer, you may need to edit a file while working on the Linux terminal.
|
|
||||||
|
|
||||||
There are several file editors on Linux, and how to choose the right one for your needs.
|
|
||||||
|
|
||||||
I would like to recommend Vim editor.
|
|
||||||
|
|
||||||
### You may ask, why?
|
|
||||||
|
|
||||||
You may spend more time in the editor to modify an existing file than writing new text.
|
|
||||||
|
|
||||||
In this case, Vim Keyboard shortcuts allow you to efficiently meet your needs.
|
|
||||||
|
|
||||||
The following articles may help you learn about file and directory manipulation.
|
|
||||||
|
|
||||||
* [**L**][1]**[inux Basic – Linux and Unix Commands for File and Directory Manipulation][1]**
|
|
||||||
* **[10 Methods to View Different File Formats in Linux][2]**
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### What’s vim?
|
|
||||||
|
|
||||||
Vim is one of the most popular and powerful text editor that widely used by Linux administrators and developers.
|
|
||||||
|
|
||||||
It’s highly configurable text editor which enables efficient text editing. This is an updated version of the vi editor, which is already installed on most Unix systems.
|
|
||||||
|
|
||||||
Vim is often called a “programmer’s editor,” but it is not limited to it, and is suitable for all types of text editing.
|
|
||||||
|
|
||||||
It comes with many features like multi level undo, multi windows and buffers, syntax highlighting, command line editing, file name completion, visual selection.
|
|
||||||
|
|
||||||
You can easily obtain online help with the “:help” command.
|
|
||||||
|
|
||||||
### Understanding Vim Modes
|
|
||||||
|
|
||||||
Vim has two modes, the details are below:
|
|
||||||
|
|
||||||
**Command Mode:** When you launch Vim Editor, you will default to Command Mode. You can move around the file, and modify some parts of the text, cut, copy, and paste parts of the text and issue commands to do more (press ESC for Command Mode).
|
|
||||||
|
|
||||||
**Insert Mode:** The nsert mode is used to type text in a given given document (Press i for insert mode).
|
|
||||||
|
|
||||||
### How do I know which Vim mode I am on?
|
|
||||||
|
|
||||||
If you are in insert mode, you will see **“INSERT”** at the bottom of the editor. If nothing is shown, or if it shows the file name at the bottom of the editor, you are in “Command Mode”.
|
|
||||||
|
|
||||||
### Cursor Movement in Normal Mode
|
|
||||||
|
|
||||||
These Vim keyboard shortcuts allow you to move your cursor around a file in different ways.
|
|
||||||
|
|
||||||
* `G` – Go to the last line of the file
|
|
||||||
* `gg` – Go to the first line of the file
|
|
||||||
* `$` – Go to the end of line.
|
|
||||||
* `0` (zero) – Go to the beginning of line.
|
|
||||||
|
|
||||||
|
|
||||||
* `w` – Jump by start of words
|
|
||||||
* `W` – Jump by words (spaces separate words)
|
|
||||||
* `b` – Jump backward by words
|
|
||||||
* `B` – Jump backward by words (spaces separate words)
|
|
||||||
|
|
||||||
|
|
||||||
* `PgDn` Key – Move down page-wise
|
|
||||||
* `PgUp` Key – Move up page-wise
|
|
||||||
* `Ctrl+d` – Move half-page down
|
|
||||||
* `Ctrl+u` – Move half-page up
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Insert mode – insert a text
|
|
||||||
|
|
||||||
These vim keyboard shortcuts allows you to insert a cursor in varies position based on your needs.
|
|
||||||
|
|
||||||
* `i` – Insert before the cursor
|
|
||||||
* `a` – Insert after the cursor
|
|
||||||
* `I` – Insert at the beginning of the line, this is useful when you are in the middle of the line.
|
|
||||||
* `A` – Insert at the end of the line
|
|
||||||
* `o` – Open a new line below the current line
|
|
||||||
* `O` – Append a new line above the current line
|
|
||||||
* `ea` – Insert at the end of the word
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Copy, Paste and Delete a Line
|
|
||||||
|
|
||||||
* `yy` – yank (copy) a line
|
|
||||||
* `p/P` – Paste after cursor/ put before cursor
|
|
||||||
* `dd` – delete a line
|
|
||||||
* `dw` – delete the word
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Search and Replace Pattern in Vim
|
|
||||||
|
|
||||||
* `/pattern` – To search a given pattern
|
|
||||||
* `?pattern` – To search backward a given pattern
|
|
||||||
* `n` – To repeat search
|
|
||||||
* `N` – To repeat backward search
|
|
||||||
|
|
||||||
|
|
||||||
* `:%s/old-pattern/new-pattern/g` – Replace all old formats with the new format across the file.
|
|
||||||
* `:s/old-pattern/new-pattern/g` – Replace all old formats with the new format in the current line.
|
|
||||||
* `:%s/old-pattern/new-pattern/gc` – Replace all old formats with the new format across the file with confirmations.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### How do I go to a particular line in Vim Editor
|
|
||||||
|
|
||||||
You can do this in two ways, depending on your need. If you don’t know the line number I suggest you go with the first method.
|
|
||||||
|
|
||||||
Add line number by opening a file and running the command below.
|
|
||||||
|
|
||||||
```
|
|
||||||
:set number
|
|
||||||
```
|
|
||||||
|
|
||||||
Once you have set the line number, press **“: n”** to go to the corresponding line number. For example, if you want to go to **line 15**, enter.
|
|
||||||
|
|
||||||
```
|
|
||||||
:15
|
|
||||||
```
|
|
||||||
|
|
||||||
If you already know the line number, use the following method to go directly to the corresponding line. For example, if you want to move to line 20, enter the command below.
|
|
||||||
|
|
||||||
```
|
|
||||||
$ vim +20 [File_Name]
|
|
||||||
```
|
|
||||||
|
|
||||||
### Undo/Redo/Repeat Operation
|
|
||||||
|
|
||||||
* `u` – Undo the changes
|
|
||||||
* `Ctrl+r` – Redo the changes
|
|
||||||
* `.` – Repeat last command
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Saving and Exiting Vim
|
|
||||||
|
|
||||||
* `:w` – Save the changes but don’t exit
|
|
||||||
* `:wq` – Write and quit
|
|
||||||
* `:q!` – Force quit
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.2daygeek.com/basic-vim-commands-cheat-sheet-quick-start-guide/
|
|
||||||
|
|
||||||
作者:[Magesh Maruthamuthu][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[萌新阿岩](https://github.com/mengxinayan)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://www.2daygeek.com/author/magesh/
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://www.2daygeek.com/linux-basic-commands-file-directory-manipulation/
|
|
||||||
[2]: https://www.2daygeek.com/unix-linux-command-to-view-file/
|
|
@ -0,0 +1,161 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (mengxinayan)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: subject: (Basic Vim Commands You Need to Know to Work in Vim Editor)
|
||||||
|
[#]: via: (https://www.2daygeek.com/basic-vim-commands-cheat-sheet-quick-start-guide/)
|
||||||
|
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
|
||||||
|
|
||||||
|
你需要知道的 Vim 编辑器中的基础命令
|
||||||
|
======
|
||||||
|
|
||||||
|
如果你是一名系统管理员或者开发者,当你在终端工作时有时会需要编辑一个文件。
|
||||||
|
|
||||||
|
在 Linux 系统中有几种文件编辑器,你可以根据需求选择合适的文件编辑器
|
||||||
|
|
||||||
|
在这里,我想推荐 Vim 编辑器
|
||||||
|
|
||||||
|
|
||||||
|
### 为什么推荐 Vim 编辑器
|
||||||
|
|
||||||
|
和创建编写新文件相比,你会在编辑器中花费更多的时间用来修改已经存在的文件。
|
||||||
|
|
||||||
|
在这种情况下,Vim 快捷键可以有效地满足你的需求。
|
||||||
|
|
||||||
|
文章下面的内容可以帮助你了解对文件和目录的操作。
|
||||||
|
|
||||||
|
[Linux 基础:对文件和目录进行操作的 Linux 和 Unix 命令][1]
|
||||||
|
[在 Linux 中查看不同文件格式的10种方法][2]
|
||||||
|
|
||||||
|
|
||||||
|
### 什么是 Vim
|
||||||
|
|
||||||
|
Vim 是被 Linux 管理员和开发者广泛使用的最流行和功能强大的编辑器之一。
|
||||||
|
|
||||||
|
它可以通过高度的自定义配置来提高文本编辑效率。它是在众多 Unix 默认安装的 Vi 编辑器的升级版。
|
||||||
|
|
||||||
|
Vim 通常被称为“程序员的编辑器”,但并不限于此,它也可用于编辑任何类型的文件。
|
||||||
|
|
||||||
|
它具有许多功能,例如:多次撤销,多窗口和缓冲区,语法高亮,命令行编辑,文件名补全,可视选择。
|
||||||
|
|
||||||
|
你可以使用 `:help` 命令来获取在线帮助
|
||||||
|
|
||||||
|
|
||||||
|
### 理解 Vim 的模式
|
||||||
|
|
||||||
|
Vim 有两种模式,详细介绍如下:
|
||||||
|
|
||||||
|
**命令模式:** 当启动 Vim 编辑器后,默认处在命令模式下。你可以在文件中移动并且修改内容,剪切,复制和粘贴文件的一部分,同时发出命令执行更多操作(按 ESC 键进入命令模式)
|
||||||
|
|
||||||
|
**插入模式:** 插入模式用于在给定的文档位置插入文本(按 i 键进入插入模式)
|
||||||
|
|
||||||
|
### 我如何知道我正使用哪种 Vim 模式呢?
|
||||||
|
|
||||||
|
如果你正在使用插入模式,你会在编辑器的底部看到 `INSERT` 。如果编辑器底部没有显示任何内容,或者在编辑器底部显示了文件名,则处于 “命令模式”。
|
||||||
|
|
||||||
|
### 正常模式下的光标移动
|
||||||
|
### Cursor Movement in Normal Mode
|
||||||
|
|
||||||
|
Vim 快捷键允许你使用不同的方式来移动光标:
|
||||||
|
|
||||||
|
* `G` – 跳转到文件最后一行
|
||||||
|
* `gg` – 跳转到文件首行
|
||||||
|
* `$` – 跳转到行末尾
|
||||||
|
* `0` (零) – 跳转到行开头
|
||||||
|
|
||||||
|
* `w` – 跳转到下一个单词的开始(单词的分隔符可以是空格或其他符号)
|
||||||
|
* `W` – 跳转到下一个单词的开始(单词的分隔符只能是空格)
|
||||||
|
* `b` – 跳转到下一个单词的末尾(单词的分隔符可以是空格或其他符号)
|
||||||
|
* `B` – 跳转到下一个单词的末尾(单词的分隔符只能是空格)
|
||||||
|
|
||||||
|
* `PgDn` 键 – 向下移动一页
|
||||||
|
* `PgUp` 键 – 向上移动一页
|
||||||
|
* `Ctrl+d` – 向下移动半页
|
||||||
|
* `Ctrl+u` – 向上移动半页
|
||||||
|
|
||||||
|
|
||||||
|
### 插入模式:插入文字
|
||||||
|
|
||||||
|
下面的 vim 快捷键允许你根据需要在光标的不同位置插入内容。
|
||||||
|
|
||||||
|
* `i` – 在光标之前插入
|
||||||
|
* `a` – 在光标之后插入
|
||||||
|
* `I` – 在光标所在行的开头插入。当光标位于行中间时,这个键很有用
|
||||||
|
* `A` – 在光标所在行的末尾插入。
|
||||||
|
* `o` – 在光标所在行的下面插入新行
|
||||||
|
* `O` – 在光标所在行的上面插入新行
|
||||||
|
* `ea` – 在单词的末尾插入
|
||||||
|
|
||||||
|
|
||||||
|
### 拷贝,粘贴和删除一行
|
||||||
|
|
||||||
|
* `yy` – 复制一行
|
||||||
|
* `p/P` – 将内容粘贴到光标之后 / 将内容粘贴到光标之前
|
||||||
|
* `dd` – 删除一行
|
||||||
|
* `dw` – 删除一个单词
|
||||||
|
|
||||||
|
|
||||||
|
### 在 Vim 中搜索和替换模式
|
||||||
|
|
||||||
|
* `/pattern` – 向后搜索给定的模式
|
||||||
|
* `?pattern` – 向前搜索给定的模式
|
||||||
|
* `n` – 向后重复搜索之前给定的模式
|
||||||
|
* `N` – 向前重复搜索之前给定的模式
|
||||||
|
|
||||||
|
* `:%s/old-pattern/new-pattern/g` – 将文件中所有的旧模式替换为新模式
|
||||||
|
* `:s/old-pattern/new-pattern/g` – 将当前行中所有的旧模式替换为新模式
|
||||||
|
* `:%s/old-pattern/new-pattern/gc` – 逐个询问是否文件中的旧模式替换为新模式
|
||||||
|
|
||||||
|
|
||||||
|
### 如何在 Vim 编辑器中跳转到特定行
|
||||||
|
|
||||||
|
你可以根据需求以两种方式达到该目的,如果你不知道行号,建议采用第一种方法。
|
||||||
|
|
||||||
|
通过打开文件并运行下面的命令来显示行号
|
||||||
|
|
||||||
|
```
|
||||||
|
:set number
|
||||||
|
```
|
||||||
|
|
||||||
|
当你设置好显示行号后,按 `:n` 跳转到相应的行号。例如,如果你想跳转到第 15 行,请输入:
|
||||||
|
|
||||||
|
```
|
||||||
|
:15
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你已经知道行号,请使用以下方法在打开文件时直接跳转到相应行。例如,如果在打开文件时直接跳转到 20 行,请输入下面的命令:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ vim +20 [File_Name]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 撤销操作/恢复上一次操作/重复上一次操作
|
||||||
|
|
||||||
|
* `u` – 撤销更改
|
||||||
|
* `Ctrl+r` – 恢复更改
|
||||||
|
* `.` – 重复上一条命令
|
||||||
|
|
||||||
|
|
||||||
|
### 保存和退出 Vim
|
||||||
|
|
||||||
|
* `:w` – 保存更改但不退出 vim
|
||||||
|
* `:wq` – 写并退出
|
||||||
|
* `:q!` – 强制退出
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.2daygeek.com/basic-vim-commands-cheat-sheet-quick-start-guide/
|
||||||
|
|
||||||
|
作者:[Magesh Maruthamuthu][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[萌新阿岩](https://github.com/mengxinayan)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://www.2daygeek.com/author/magesh/
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://www.2daygeek.com/linux-basic-commands-file-directory-manipulation/
|
||||||
|
[2]: https://www.2daygeek.com/unix-linux-command-to-view-file/
|
Loading…
Reference in New Issue
Block a user