Linux入门(3)——vim详细用法及系统输入输出管理
1. vim工作环境配置
- 在命令模式下临时更改vim工作环境
输入指令 | 效果 |
---|
:set nu | 显示行号 |
:set nonu | 取消行号显示 |
:set mouse=a | 鼠标可用 |
:set cursorline | 显示光标所在行的行线 |
- 改变默认vim工作环境
[redhat@localhost ~]$ vim /etc/vimrc
- 为不同用户设置vim工作环境
[redhat@localhost ~]$ vim ~/.vimrc
2. vim基本操作命令
- 搜索指定关键词
在命令模式下输入 /关键字 搜索关键字并高亮显示 ;
输入 :noh 取消高亮; N 向上匹配; n向下匹配。
- 字符操作
复制指令 | 效果 |
---|
y | 复制快捷键 |
yl | 复制一个字母 |
y3l | 复制三个字母 |
yw | 复制一个单词 |
y3w | 复制三个单词 |
yy | 复制一行 |
y3y | 复制三行 |
删除指令 | 效果 |
---|
d | 删除的快捷键 |
dlx | 删除一个字母 |
d3l | 删除三个字母 |
dw | 删除一个单词 |
d3w | 删除3个单词 |
dd | 删除一行 |
d3d | 删除3行 |
剪切指令 | 效果 |
---|
c | 剪切 |
cl | 剪切1个字母 |
c3l | 剪切3个字母 |
cw | 剪切1个单词 |
c3w | 剪切3个单词 |
cc | 剪切1整行 |
c3c | 剪切3行 |
剪切操作完成后会直接进入插入模式,退出插入模式在命令模式按P进行粘贴
在命令模式下:
U:向前撤销
R:向后恢复
- 批量字符替换操作
匹配格式举例 | 解释 |
---|
:%s/原字符/替换字符/g | %表示全文的行,g表示全文的列 |
1,5s/原字符/替换字符/g | ## 1-5行,所有列 |
/string1/,/string2/s/原/替换/g | /string1/,/string2/ lp字符到halt字符之间的行,所有列 |
- 可视化操作
在命令模式下,按Ctrl+V,进入可视化模式,按Esc退出,在该模式下可以批量对字符进行操作处理。
- 进入可视化模式,选中需要处理的行或者列;
- 输入大写I进入插入模式;
- 插入要写入的字符;
- 按Esc退出。
- 分屏操作
- 同步分屏
ctrl+ ws:上下分屏
ctrl+ w v:左右分屏
ctrl + wc :取消分屏 - 异步分屏
命令模式下输入 :sp filename 打开另外的文件
- 光标相关指令
操作 | 效果 |
---|
gg | 光标移动到文件第一行行首 |
G | 光标移动到最后一行 |
:数字 | 光标移动到指定行 |
i | 光标所在位置插入 |
I | 光标所在行行首插入 |
A | 光标所在行的行尾插入 |
a | 光标所在字符的下一个字符插入 |
o | 光标所在行的下一行插入 |
O | 光标所在行的上一行插入 |
s | 删除光标所在字符插入 |
S | 删除光标所在行插入 |
2. 输入输出
在交互式的程序中,输入来自用户的键盘和鼠标,结果输出到用户的屏幕。Linux中有标准输入输出相关信息如下:
类型 | 文件描述符 | 默认情况 | 对应文件句柄位置 |
---|
标准输入(standard input) | 0 | 从键盘获得输入 | /proc/self/fd/0 |
标准输出(standard output) | 1 | 输出到即控制台 | /proc/self/fd/1 |
错误输出(error output) | 2 | 输出到控制台 | /proc/self/fd/2 |
标准输入输出可以通过操作符进行重定向,实现不同的用途。
操作符 | 功能 |
---|
> | 重定向正确输出(覆盖) |
2> | 重定向错误输出(覆盖) |
&> | 重定向所有输出(覆盖) |
>> | 追加正确输出 |
2>> | 追加错误输出 |
&>> | 追加所有输出 |
| | 前一条命令的输出传递到下一条命令作为输入 |
2>&1 | 重定向错误输出至正确输出(不覆盖) |
< | 输入重定向,可将操作符后的内容重定向至操作符前作为输入 |
<< | 多行录入,以任意字符开始,再次遇到相同字符结束,输入至之前命令 |
[redhat@localhost ~]$find /etc -name passwd > westos.out
[redhat@localhost ~]$find /etc -name passwd 2> westos.out
[redhat@localhost ~]$find /etc -name passwd &> westos.out
[redhat@localhost ~]$find /etc -name passwd 2>&1 | tee /dev/pts/0 |cat -n
[redhat@localhost ~]$tr 'a-z' 'A-Z' < file
[redhat@localhost ~]$tr 'a-z' 'A-Z' <<EOF ..... EOF
系统空设备文件/dev/null,当输出重定向至空设备文件时表示丢弃数据。
[redhat@localhost ~]$ find /etc -name passwd 2>&1 1>/dev/null | wc -l
3. 练习![在这里插入图片描述](https://img-blog.csdnimg.cn/20210406203156449.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY5OTg3Nw==,size_16,color_FFFFFF,t_70#pic=center)
[redhat@localhost ~]$find /etc -name passwd 2>/dev/null
[redhat@localhost ~]$find /etc -name passwd 1>/tmp/westos.out 2>/tmp/westos.err
[redhat@localhost ~]$find /etc -name passwd 2>&1 |tee /tmp/westos.all|wc -l
[redhat@localhost ~]$find /etc -name passwd 2>&1 | tee /dev/pts/0 |cat -n
[redhat@localhost Desktop]$ cat test.sh
cat > westos.file <<EOF
hello linux
hello westos
hello linux
westos linux is very nice !!
EOF
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210406210005345.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY5OTg3Nw==,size_16,color_FFFFFF,t_70#pic=center)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210406210406355.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY5OTg3Nw==,size_16,color_FFFFFF,t_70#pic=center)