【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的一个增强版)

相关文章
|
4天前
|
程序员 开发工具
Vim命令大全:掌握编辑利器
Vim命令大全:掌握编辑利器
|
2天前
|
运维 Linux
探索 Linux 系统信息工具:uname
探索 Linux 系统信息工具:uname
7 0
|
4天前
|
安全 前端开发 Linux
深入了解Linux命令pinentry-curses:保护敏感输入的终端工具
`pinentry-curses`是GnuPG的一部分,用于CLI中安全输入密码或PIN。它利用curses库创建一个隐藏输入的终端界面,增强安全性。适用于GnuPG及其它需要安全输入的场景。特点包括安全隐藏输入、用户友好、灵活配置。常用参数如`--password`、`--title`等。在GPG加密文件时,可以结合`--pinentry-mode curses`使用。记得在安全环境下操作,勿明文显示密码,保持软件更新,并使用强密码。
|
4天前
|
NoSQL 编译器 Linux
【Linux】--- Linux编译器-gcc/g++、调试器-gdb、项目自动化构建工具-make/Makefile 使用
【Linux】--- Linux编译器-gcc/g++、调试器-gdb、项目自动化构建工具-make/Makefile 使用
10 0
|
4天前
|
缓存 人工智能 Linux
【Linux】--- 详解Linux软件包管理器yum和编辑器vim
【Linux】--- 详解Linux软件包管理器yum和编辑器vim
13 0
|
8月前
|
存储 Linux C语言
Linux:入门学习知识及常见指令
Linux:入门学习知识及常见指令
|
1月前
|
人工智能 Unix Linux
Linux的学习之路:2、基础指令(1)
Linux的学习之路:2、基础指令(1)
34 0
|
1月前
|
Unix Linux Windows
Linux的学习之路:3、基础指令(2)
Linux的学习之路:3、基础指令(2)
43 0
|
1月前
|
Linux Shell 文件存储
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(深入df和dh的区别和探索)
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(深入df和dh的区别和探索)
86 1
|
1月前
|
监控 Linux Shell
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)
在线上排查问题时,查询日志、查看系统配置和分析操作系统信息是至关重要的。这些操作可以帮助我们深入了解软件和服务的兼容性,并解决潜在的问题。在本次学习中,我们将介绍并深入学习一些我在处理类似问题时常用的指令。通过掌握这些指令,你将能够更加高效地定位和解决线上问题,提高系统的稳定性和性能。让我们一同进入这个学习过程吧!
57 0
【Linux技术专题】「夯实基本功系列」带你一同学习和实践操作Linux服务器必学的Shell指令(排查问题指令 - 下)