【Linux】指令入门之文本编辑工具Vim(五)

简介: Vim 非常稳定,开发迭代版本贴别快。其特点有以下几个:持久的多级撤消树、广泛的插件系统、支持数百种编程语言和文件格式、强大的搜索和替换、与许多工具集成

1 Vim简介


1.1 什么是Vim?

Vim 是一个高度可配置的文本编辑器,旨在让创建和更改任何类型的文本变得非常高效。


1.2 Vim的优点

Vim 非常稳定,开发迭代版本贴别快。其特点有以下几个:

  • 持久的多级撤消树
  • 广泛的插件系统
  • 支持数百种编程语言和文件格式
  • 强大的搜索和替换
  • 与许多工具集成


1.3 Vim下载与使用

Vim的官方网址:https://www.vim.org/


Vim的下载地址:https://www.vim.org/download.php


Vim的版本:目前Vim的最新版本是Vim 9.0 ,也是最新的稳定版本。


Vim的平台支持的平台image.png

1.4 Vim在Mac中的使用


当前我的机器已经安装了Vim,我们可以使用--help来查看下帮助文档。从文档中可以看到,我当前机器的版本是VIM - Vi IMproved 8.2


Aion.Liu $ vim--helpVIM - Vi IMproved 8.2 (2019 Dec 12, compiled Apr 19202221:41:43)
Usage: vim [arguments] [file ..]       edit specified file(s)
   or: vim [arguments] -               read text from stdin
   or: vim [arguments] -t tag          edit file where tag is defined
   or: vim [arguments] -q [errorfile]  edit file with first error
Arguments:
--     Only file names after this
-v     Vi mode (like "vi")
-e     Ex mode (like "ex")
-E     Improved Ex mode
-s     Silent (batch) mode (only for"ex")
-d     Diff mode (like "vimdiff")
-y     Easy mode (like "evim", modeless)
-R     Readonly mode (like "view")
-Z     Restricted mode (like "rvim")
-m     Modifications (writing files) not allowed
-M     Modifications in text not allowed
-b     Binary mode
-l     Lisp mode
-C     Compatible with Vi: 'compatible'-N     Not fully Vi compatible: 'nocompatible'-V[N][fname]   Be verbose [level N] [log messages to fname]
-D     Debugging mode
-n     No swap file, use memory only
-r     List swap files and exit-r (with file name)  Recover crashed session
-L     Same as -r-T <terminal>  Set terminal type to <terminal>
--not-a-term   Skip warning for input/output not being a terminal
--ttyfail    Exit if input or output is not a terminal
-u <vimrc>   Use <vimrc> instead of any .vimrc
--noplugin   Don't load plugin scripts   -p[N]    Open N tab pages (default: one for each file)   -o[N]    Open N windows (default: one for each file)   -O[N]    Like -o but split vertically   +      Start at end of file   +<lnum>    Start at line <lnum>   --cmd <command>  Execute <command> before loading any vimrc file   -c <command>   Execute <command> after loading the first file   -S <session>   Source file <session> after loading the first file   -s <scriptin>  Read Normal mode commands from file <scriptin>   -w <scriptout> Append all typed commands to file <scriptout>   -W <scriptout> Write all typed commands to file <scriptout>   -x     Edit encrypted files   --startuptime <file> Write startup timing messages to <file>   -i <viminfo>   Use <viminfo> instead of .viminfo   --clean    'nocompatible', Vim defaults, no plugins, no viminfo   -h  or  --help Print Help (this message) and exit   --version    Print version information and exit

1.5 Vim在Linux中的使用

当我进入Liunx虚拟机,当前我的机器也安装了Vim,我们可以使用--help来查看下帮助文档。从文档中可以看到,我当前机器的版本是VIM - Vi IMproved 7.4。使用参数以及帮助文档同Mac中相差不多。

vim--helpVIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug  9201903:17:15)
usage: vim [arguments] [file ..]       edit specified file(s)
   or: vim [arguments] -               read text from stdin
   or: vim [arguments] -t tag          edit file where tag is defined
   or: vim [arguments] -q [errorfile]  edit file with first error
Arguments:
--                   Only file names after this
-v                   Vi mode (like "vi")
-e                   Ex mode (like "ex")
-E                   Improved Ex mode
-s                   Silent (batch) mode (only for"ex")
-d                   Diff mode (like "vimdiff")
-y                   Easy mode (like "evim", modeless)
-R                   Readonly mode (like "view")
-Z                   Restricted mode (like "rvim")
-m                   Modifications (writing files) not allowed
-M                   Modifications in text not allowed
-b                   Binary mode
-l                   Lisp mode
-C                   Compatible with Vi: 'compatible'-N                   Not fully Vi compatible: 'nocompatible'-V[N][fname]         Be verbose [level N] [log messages to fname]
-D                   Debugging mode
-n                   No swap file, use memory only
-r                   List swap files and exit-r (with file name)  Recover crashed session
-L                   Same as -r-Astartin Arabic mode
-H                   Start in Hebrew mode
-F                   Start in Farsi mode
-T <terminal>        Set terminal type to <terminal>
-u <vimrc>           Use <vimrc> instead of any .vimrc
--noplugin           Don't load plugin scripts   -p[N]                Open N tab pages (default: one for each file)   -o[N]                Open N windows (default: one for each file)   -O[N]                Like -o but split vertically   +                    Start at end of file   +<lnum>              Start at line <lnum>   --cmd <command>      Execute <command> before loading any vimrc file   -c <command>         Execute <command> after loading the first file   -S <session>         Source file <session> after loading the first file   -s <scriptin>        Read Normal mode commands from file <scriptin>   -w <scriptout>       Append all typed commands to file <scriptout>   -W <scriptout>       Write all typed commands to file <scriptout>   -x                   Edit encrypted files   --startuptime <file> Write startup timing messages to <file>   -i <viminfo>         Use <viminfo> instead of .viminfo   -h  or  --help       Print Help (this message) and exit   --version            Print version information and exit


2 Vim详细介绍


2.1 Vim三种模式


vim有三种操作模式,分别是


  • 命令模式(Command mode)
  • 输入模式(Insert mode)
  • 底线命令模式(Last line mode)


三种模式切换快捷键:


模式

快捷键

命令模式

ESC

输入模式

i或a

底线命令模式

:


2.2 命令模式(CM)

在命令模式中控制光标移动和输入命令,可对文本进行复制、粘贴、删除和查找等工作。


命令模式中常用的快捷操作:


操作

快捷键

光标左移

h

光标右移

l(小写L)

光标上移

k

光标下移

j

光标移动到下一个单词

w

光标移动到上一个单词

b

移动游标到第n行

nG

移动游标到第一行

gg

移动游标到最后一行

G

快速回到上一次光标所在位置

Ctrl+o

删除当前字符

x

删除前一个字符

X

删除整行

dd

删除一个单词

dw或daw

删除至行尾

d$或D

删除至行首

d^

删除到文档末尾

dG

删除至文档首部

d1G

删除n行

ndd

删除n个连续字符

nx

将光标所在位置字母变成大写或小写

~

复制游标所在的整行

yy(3yy表示复制3行)

粘贴至光标后(下)

p

粘贴至光标前(上)

P

剪切

dd

交换上下行

ddp

替换整行,即删除游标所在行并进入插入模式

cc

撤销一次或n次操作

u{n}

撤销当前行的所有修改

U

恢复撤销操作

Ctrl+r

整行将向右缩进

>>

整行将向左退回

<<

若档案没有更动,则不储存离开,若档案已经被更动过,则储存后离开

ZZ


「实验步骤」

1、首先我们创建一个文本文件:春望.txt

2、使用vim 春望.txt回车后就进入了命令行模式。

image.png

3、可以看到里面是一个空空的文本

image.png

4、当我们在此模式下,输入o则是换行+输入


6、确认已经输入的内容

image.png


2.3 输入模式(IM)

在命令模式下按ia键就进入了输入模式,在输入模式下,您可以正常的使用键盘按键对文本进行插入和删除等操作。


注意⚠️:

1、通常情况下,我们使用i命令进入输入比较多,这里的i代表insert或者input的首字母。

2、我们通常所说的命令或者字符都是是英文输入状态下。


「实验步骤」

1、按下i进入输入模式,输入春望的诗名(这里我就直接拷贝这里了,为了方便拷贝,我放在代码块)。

image.png

image.png


2.4 底线命令模式(LLM)

在命令模式下按:键就进入了底线命令模式,在底线命令模式中可以输入单个或多个字符的命令。


操作

命令

保存

:w

退出

:q

保存并退出

:wq(:wq!表示强制保存退出)

将文件另存为其他文件名

:w new_filename

显示行号

:set nu

取消行号

:set nonu

使本行内容居中

:ce

使本行文本靠右

:ri

使本行内容靠左

:le

向光标之下寻找一个名称为word的字符串

:/word

向光标之上寻找一个字符串名称为word的字符串

:?word

重复前一个搜寻的动作

:n

从第一行到最后一行寻找word1字符串,并将该字符串取代为word2

:1,$s/word1/word2/g :%s/word1/word2/g


「实验步骤」

1、按下ECS键回到命令模式,并输入底线命令:ce,使诗名居中。这里执行都是一行行执行。

image.png

2、当我们输入完成内容后,按下Esc键,然后输入:wq来保存已经输入的内容

image.png

3 总结

针对在Mac和Linux,Vim命令是我们经常使用的一个软件工具,所以希望能够记住这个命令,学以致用。当然有些时候,如果机器没有Vim,而是有另外一个工具Vi(这个比较常用,相对于Vim来说,个人认为Vim是Vi的一个增强版)

相关文章
|
8月前
|
Linux 开发工具 iOS开发
【Vim 核心攻略】 —— 文本编辑高手的进阶秘籍
Vim 是效率与优雅的化身,蕴藏着无尽的可能。在这里,我希望通过记录点滴心得,让更多人发现它的魅力。或许,某个不经意的技巧、一个贴心的配置,便能让你与 Vim 的旅途更加契合。愿这片小小的天地,成为你探索高效编辑世界的一盏微光。
【Vim 核心攻略】 —— 文本编辑高手的进阶秘籍
|
11月前
|
Linux 开发工具
linux文本管理命令
本文档介绍了Linux系统中常用的文本处理命令,包括`echo`、`cat`、`head`、`tail`、`wc`、`less`、`grep`以及重定向符号的使用方法和练习题。此外,还详细讲解了VIM编辑器的特点、工作模式、常用快捷键和高级技巧,帮助用户高效地进行文本编辑和处理。
241 4
|
12月前
|
开发工具
用 Vim 编辑 Markdown 时直接粘贴图片
介绍一款 Vim 插件,可以帮助提升编辑 Markdown 时的从剪贴板粘贴图片的效率和体验。
130 2
|
机器学习/深度学习 Linux 编译器
Linux入门3——vim的简单使用
Linux入门3——vim的简单使用
153 1
Linux 系列之 在一个文本中查找指定的数据
文章介绍了在Linux命令行中使用文本搜索功能快速查找指定内容的方法,并提供了按`n`键查找下一个匹配项的技巧。
Linux 系列之 在一个文本中查找指定的数据
|
Linux Shell
10-10|linux命令查询 关键字在文本中出现的行数
10-10|linux命令查询 关键字在文本中出现的行数
|
机器学习/深度学习 存储 Linux
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
186 4
|
Ubuntu Linux
在Linux中,想修改ip,需要编辑哪个配置⽂件?修改完配置⽂件后,如何重启网卡?使配置生效?
在Linux中,想修改ip,需要编辑哪个配置⽂件?修改完配置⽂件后,如何重启网卡?使配置生效?
|
Oracle 关系型数据库 Linux
2022年最新编辑Linux基础知识总结
这篇文章是一份2022年的Linux基础知识总结,涵盖了Linux目录结构、远程操作、文本编辑、快捷键、系统管理命令、用户管理等方面的内容。
2022年最新编辑Linux基础知识总结
|
域名解析 缓存 负载均衡
在Linux中,自定义解析域名的时候,可以编辑哪个⽂件?是否可以⼀个ip对应多个域名?是否⼀个域名对应多个ip?
在Linux中,自定义解析域名的时候,可以编辑哪个⽂件?是否可以⼀个ip对应多个域名?是否⼀个域名对应多个ip?
下一篇
oss教程