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

相关文章
|
3月前
|
开发工具
Vim工具使用
vim是Visual Interface的简称,一个全屏文本编辑器,支持多种文本操作且可定制。vim有三种基本模式:编辑模式、输入模式和命令模式。编辑模式用于执行命令,输入模式用于输入文本,命令模式用于保存和退出。
58 2
|
3月前
|
Unix Linux Shell
linux入门!
本文档介绍了Linux系统入门的基础知识,包括操作系统概述、CentOS系统的安装与远程连接、文件操作、目录结构、用户和用户组管理、权限管理、Shell基础、输入输出、压缩打包、文件传输、软件安装、文件查找、进程管理、定时任务和服务管理等内容。重点讲解了常见的命令和操作技巧,帮助初学者快速掌握Linux系统的基本使用方法。
190 3
|
3月前
|
Linux 开发工具
linux文本管理命令
本文档介绍了Linux系统中常用的文本处理命令,包括`echo`、`cat`、`head`、`tail`、`wc`、`less`、`grep`以及重定向符号的使用方法和练习题。此外,还详细讲解了VIM编辑器的特点、工作模式、常用快捷键和高级技巧,帮助用户高效地进行文本编辑和处理。
91 4
|
4月前
|
开发工具
用 Vim 编辑 Markdown 时直接粘贴图片
介绍一款 Vim 插件,可以帮助提升编辑 Markdown 时的从剪贴板粘贴图片的效率和体验。
48 2
|
4月前
|
机器学习/深度学习 Linux 编译器
Linux入门3——vim的简单使用
Linux入门3——vim的简单使用
77 1
|
4月前
|
存储 数据可视化 Linux
Linux 基础入门
Linux 基础入门
|
4月前
|
Linux Go 数据安全/隐私保护
Linux入门2——初识Linux权限
Linux入门2——初识Linux权限
46 0
|
6月前
|
Unix Linux 开发工具
在Linux中,有哪些基本的 Vim 命令?
在Linux中,有哪些基本的 Vim 命令?
|
5月前
|
存储 Linux Shell
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
这篇文章介绍了Vim编辑器的基本使用、常用命令和模式,以及Linux系统中用户和组的管理方法,包括用户和组相关文件如/etc/passwd、/etc/shadow和/etc/group的说明。
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件