文本编辑器的作用
创建或修改文本文件
维护Linux系统中的各种配置文件
文本(text):文字,没有图片,声音,视频内容,只是字符串
英文字母、中文、特殊符号、数字等组成的内容,是文本
Linux中最常用的文本编辑器
vi:类Unix系统中默认的文本编辑器
vim:vi编辑器的增强版本,习惯上也称为vi
编辑器(editor):是一个对文本内容进行修改的工具
vim - Vi IMproved, a programmer's text
vim 是vi的升级版,是程序员文本编辑工具
IMproved 提升、升级
programmer 程序员
vi 是Linux里默认安装的
centos7/8 最小安装,默认没有安装vim
vim 是vi的升级版,需要安装
安装
yum install vim -y
卸载vim编辑器
yum remove vim -y
[root@sc ~]# vim
~
~ VIM - Vi IMproved
~
~ 版本 7.4.629
~ 维护人 Bram Moolenaar 等
~ 修改者 <bugzilla@redhat.com>
~ Vim 是可自由分发的开放源代码软件
~
~ 帮助乌干达的可怜儿童!
~ 输入 :help iccf<Enter> 查看说明
~
~ 输入 :q<Enter> 退出
~ 输入 :help<Enter> 或 <F1> 查看在线帮助
~ 输入 :help version7<Enter> 查看版本信息
退出 --》:q 是没有在vim里输入内容
强制退出 --》:q! 是强制退出,退出并且不保存内容
默认情况下,会在当前目录下新建一个文本文件,如果文件存在就是打开这个文件。
[root@sc feng]# vim hunan.txt
编辑目录
[root@sc feng]# mkdir sc
[root@sc feng]# ls
sc
[root@sc feng]# cd sc
[root@sc sc]# touch feng{1..10}.txt
[root@sc sc]# ls
feng10.txt feng2.txt feng4.txt feng6.txt feng8.txt
feng1.txt feng3.txt feng5.txt feng7.txt feng9.txt
[root@sc sc]# cd ..
[root@sc feng]# vim sc
vim常用工作模式
命令模式、输入模式、末行模式、可视模式、替换模式
命令模式:vim启动后的默认模式
光标移动、字符串查找,以及删除、复制、粘贴文件内容等相关操作
插入模式
用来输入文本
进入vim编辑器后,按a或者i等进入插入模式
vim是多模式编辑器,在此之前的版本是vi;vim是vi的升级版本。
从命令模式进入插入模式
操作键 功能
i 光标在当前位置进入插入模式
a 光标后退一格并进入插入模式
o 在光标所在行下新起一行并进入插入模式
末行模式
操作键 功能
:q 如果未对文件做改动则退出 quit
:q! 放弃保存并退出
:wq 保存当前文件并退出
:wq! 强制保存当前文件并退出
:w /lianxi/sc.txt 另存为
:w 只是保存,不退出 write
:x 保存当前文件并退出
:x! 强制保存当前文件并退出
ZZ 保存当前文件并退出 ---》在命令模式使用
:set nu 显示行号 number
:set nonu 不显示行号
命令模式里行的跳转
1G 第1行
G 最后一行
30G 30行
末行模式里跳转
:1 第一行
:40 第40行
:$ 最后一行
命令模式
操作类型 操作键 功能
删除x 删除光标处的单个字符
dd 删除当前光标所在行
#dd 删除从光标处开始的#行内容
d^ 删除当前光标之前到行首的所有字符
d$ 删除当前光标处到行尾的所有字符
dw 删除一个单词(word),以空格,冒号,/等标点符号
复制yy 复制当前行整行的内容到剪贴板
#yy 复制从光标处开始的#行内容
粘贴p 将缓冲区中的内容粘贴到光标位置处之后
p 粘贴到光标位置处之前
命令模式
操作键 功能
/word 从上而下在文件中查找字符串"word"
?word 从下而上在文件中查找字符串"word"
n 定位下一个匹配的被查找的字符串
N 定位上一个匹配的被查找的字符串
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sc:x:1001:1001::/home/sc:/bin/bash
~
~
~
~
~
~
~
~
~
~
/sbin # 命令模式
# 末行模式
:/sbin
substitute 替换
vim mobile_phone.txt
xiaomi apple XIAOMI huawei
vivo OPPO lenovo
oneplus meizu sansung
xiaomi apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
xiaomi apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
xiaomi apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
xiaomi apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
dami apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
vivo OPPO lenovo
oneplus meizu sansung
dami apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
:s/xiaomi/dami/ # 替换光标所在行的第1个xiaomi为dami
dami apple XIAOMI huawei dami XIAOMI huawei apple huawei apple
:s/xiaomi/dami/g # 替换光标所在行里的所有xiaomi为dami -global 全局,全部
dami apple XIAOMI huawei dami XIAOMI huawei apple huawei apple
dami apple XIAOMI huawei dami XIAOMI huawei apple huawei apple
dami apple XIAOMI huawei dami XIAOMI huawei apple huawei apple
dami apple XIAOMI huawei dami XIAOMI huawei apple huawei apple
dami apple XIAOMI huawei xiaomi XIAOMI huawei apple huawei apple
:5,7s/xiaomi/dami/g # 将5-7行所有的xiaomi替换为dami
:%s/xiaomi/dami/g # 将行里所有的xiaomi替换为dami
:1,$s/xiaomi/dami/g # 将行里所有的xiaomi替换为dami
:1,$s/apple//g # 删掉所有行里的apple--》思路:使用空的内容去替换,达到删除的目的
:1,$s/huawe/harmony/gc # 替换的时候要求确认 comfirm 确认
y yes
n no
a all
q quit
:r mobile_phone.txt 文件里的内容
:r /etc/hosts
打开/root/lianxi/mobile_phone.txt -->从一个文件退出,去打开另外一个文件 edit
:e /root/lianxi/mobile_phone.txt
把当前文件另存为/tmp目录下的hello.txt
:w /tmp/hello.txt
显示行号
:set nu
取消显示行号
:set nonu
:set paste 设置粘贴的时候,按照原来的格式,保障格式不乱
:set ts=4 设置一个tab键占4个空格,默认一个tab键占8个,方便一行里可以多写点代码
:set cursorline 显示光标所在行 line 行 cursor 光标
:set cursorcolumn 显示光标所在列 column 列
vim 打开文件的异常场景
交换文件 ".a.txt.swp"是vim产生的临时文件
(1) Another program may be editing the same file. 有2个程序同时在编辑同一个文件
(2) An edit session for this file crashed. 有一个编辑的会话进程突然崩溃了(程序断开了或者终止,退出了)
E325: ATTENTION
Found a swap file by the name ".a.txt.swp"
owned by: root dated: Sat Sep 17 10:51:32 2022
file name: ~root/lianxi/a.txt
modified: no
user name: root host name: nginx
process ID: 4066391 (STILL RUNNING)
While opening file "a.txt"
dated: Sat Sep 17 10:36:36 2022
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r a.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".a.txt.swp"
to avoid this message.
root@nginx:~/lianxi# ls -a
. .. a.txt .a.txt.swp bigfile.sh linux.txt mobile_phone.txt passwd zhang.txt
root@nginx:~/lianxi# rm -rf .a.txt.swp
替换模式
R --》replace 替换
直接移动光标到某个位置,直接输入内容,就会替换
撤销:退格键
可视模式
主要用来选择复制区块文件
在命令模式下,使用v或V可以进入可视模块
可视或者可视块模式可以复制或者删除一部分单词,选中一部分单词,然后进行操作。
小v 可视模式 visual --》选择一部分 --》可以选择一行或者多行的一部分
大V 可视块模式 visual line --》选择行
crtl+v 可使块模式 visual block
按y复制,x或者d是删除
按p粘贴
另起一行,按o,再按esc,在p
tmux:多窗口编辑文本
1.安装
[root@aliyun ~]# yum install tmux -y
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
ius | 1.3 kB 00:00:00
mysql-connectors-community | 2.6 kB 00:00:00
mysql-tools-community | 2.6 kB 00:00:01
mysql57-community | 2.6 kB 00:00:00
updates | 2.9 kB 00:00:00
webtatic | 3.6 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 250 kB 00:00:00
(2/4): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(3/4): epel/x86_64/primary_db | 7.0 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 22 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package tmux.x86_64 0:1.8-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================================================================================
Installing:
tmux x86_64 1.8-4.el7 base 243 k
Transaction Summary
=========================================================================================================================================================================================================
Install 1 Package
Total download size: 243 k
Installed size: 558 k
Downloading packages:
tmux-1.8-4.el7.x86_64.rpm | 243 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tmux-1.8-4.el7.x86_64 1/1
Verifying : tmux-1.8-4.el7.x86_64 1/1
Installed:
tmux.x86_64 0:1.8-4.el7
Complete!
[root@aliyun ~]# tmux
2.输入tmux就可以进入tmux
tmux的基本用法
ctrl b " 分割出来一个窗口 上下2个窗口
ctrl b % 分割出来一个窗口 左右窗口
ctrl b x 关闭当前光标处的小窗口
ctrl b ! 关闭所有小窗口
显示已有tmux列表(C-b s)
ctrl b (方向键)上 上一个窗口
ctrl b (方向键)上 下
ctrl b (方向键)上 上一个窗口
ctrl b (方向键)上 上一个窗口
简单应用