作者:J Edwards。本文翻译自:dev.to/joaedwar/ge…
什么是Vim
Vim(Vi IMt raid)是一个开源文本编辑器。它是vi的克隆,设计用于命令行界面(CLI)和图形用户界面(GUI)中的独立应用程序。
vi编辑器是Billy Joy在1976年开发的第一个面向屏幕的文本编辑器,是为Unix环境创建的,并为文本操作而构建。
基于他为Stevie(VI爱好者的ST编辑)所做的工作,Bram Moolenaar于1991年开发并公开发布了Vim(v1.14)。
最初,Vim这个名字是“虚拟模仿”的首字母缩略词,但在1993年变成了“虚拟模仿”。
Vim支持比vi更多的功能,非常可配置,并支持高效的文本编辑。
为什么使用Vim?
Vim和类似的文本编辑器非常强大。通过学习它,你可以熟练而有效地导航和修改文件。 根据cmd.com,他们的大多数用户使用vi/Vim作为他们的主要文件编辑器,细分如下:
在Vim中,所有编辑都被保存,您可以轻松加载过去的修订。如果您犯了错误,您可以查看以前版本的列表,并选择最适合您的版本。 Vim功能包括:
- 广泛的插件系统
- 与许多工具集成
- 支持数百种编程语言和文件格式
- 强大的搜索和替换
如何安装Vim
要安装Vim,请输入以下命令:
Ubuntu/Debian
$ sudo apt install vim
CentOS的
$ sudo yum install vim
Fedora
$ sudo dnf install vim
安装Vim后,您可以在终端中键入以下命令打开编辑器:
vi
此命令打开显示Vim版本号的默认页面。
Vim模式
Vim是一个_模态文本_编辑器。基于vi编辑器,Vim继承了vi的键绑定,但也增加了原始vi文本编辑器所缺少的更多功能和扩展性。 在Vim中,编辑器所处的模式决定了字母数字键是输入字符还是方便整个文档的导航。 Vim有几种不同的编辑模式,但它的基本模式是:
正常模式
- 此模式用于编辑器命令。这通常被指定为默认模式。
视觉模式
- 与普通模式类似,此模式用于突出显示文本区域。普通命令在突出显示的区域上运行,可用于移动或编辑选定内容。
插入模式
- 这种模式类似于大多数编辑器中的编辑。在插入模式下,您可以输入字符和修改文件。
Cmd line模式
- 这是命令行模式,支持Vim窗口底部的单行输入。以:开头的命令和其他一些对应于不同操作的特定字母激活此模式。
Vim命令
所有Vim命令都可以使用键盘执行——尽管有些可以通过命令行执行,而另一些可以用作导航键。这可以让你保持手指在键盘上,眼睛在屏幕上。
若要详细了解可在Vim中使用的各种命令和操作,请使用help.txt文件。它可以在Vim中使用F1键或 : help命令。
与此同时,这里有一些基本的命令可以开始使用。
开关模式
默认情况下,Vim以正常模式启动。正常模式可以通过按Esc从其他模式访问。
要将Vim切换到插入模式,请按i。
按v进入可视模式。
导航键
有多种方法可以在打开的文件中移动。除了使用光标键,您还可以使用下表中的键在Vim中浏览文件。
| Key | Description |
|---|---|
| h | Left by one character |
| j | Down by one row |
| k | Up by one row |
| l | Right by one character |
| w | Move one word to the right |
| W | Move one word to the right past punctuation |
| b | Move one word to the left |
| B | Move one word to the left past punctuation |
| H | Move to the top of the current screen |
| M | Move to the middle of the current screen |
| L | Move to the bottom of the current screen |
行移动可以用数字作为前缀,一次移动多行。
基本正常模式命令
| Command | Description |
|---|---|
| d | Delete the text that is selected |
| dk | Delete the current line and the line above it. d ↑ can also be used. |
| x | Delete only the current character under the cursor |
| cc | Delete the current line and change to Insert mode |
| yy | Copy the selected text |
| p | Paste the copied content |
| u | Undo the last edit. Press u again for further undos |
| U | Undo changes on the current line |
| Ctrlr | Redo a change undone by u |
- 您可以组合键来指定移动。例如,dw删除光标右侧的第一个单词。它还复制内容,以便您可以将其粘贴到另一个位置。
基本插入模式命令
| Command | Description |
|---|---|
| a | Insert characters to the right of the cursor |
| A | Append characters to the current line |
| i | Insert characters to the left of cursor |
| I | Insert characters at the beginning of the current line |
| o | Add a new line after the current line |
| O | Insert a new line above the current line |
基本命令模式命令
| Command | Description |
|---|---|
| :q! | Quit but do not save changes |
| :w! | Force save the file |
| :wq | Save the file and exit Vim |
| :w | Write out the file to save changes |
| :w <file_name> | Write the file to the specified file |
| :w <file_name> | Write the file to named file |
| :r <file_name> | Import a file into the current file |
您可以在命令前加上数字来耦合导航。例如: 10 r<file_name>在第10行之后将文件导入到当前文件中。
搜索和替换
要执行基本的搜索和替换功能,请使用以下格式: %s/<search_term>/<replace_term>/g 此命令将的所有实例替换为 要分解命令:
- :进入命令模式
- %表示跨越所有线
- s代替的意思
任选地
- /foo是regex(正则表达式)来寻找要替换的东西
- /bar/是用来替换的正则表达式
- /g表示全局,否则命令每行只执行一次
- c确认每次更换
Vim还有许多其他方法,您可以在帮助文档: h或: help中阅读。 如果给定文本文件:
Hello world
You are my sunshine
In the world
如果执行命令:%s/world/planet/g/c,则world的所有实例都将更改为Planet。新文件将如下所示:
Hello planet
You are my sunshine
In the planet
命令末尾的c会提示一条确认消息,系统会询问您是否要继续进行替换。 这是几个惊人的资源,可以帮助你更好地了解和