使用vim命令打开一个文件:
例如,打开openwrt系统下的system配置文件
vim /etc/config/system
内容如下:
config system
option hostname 'OpenWrt'
option timezone 'UTC'
option ttylogin '0'
option log_size '64'
option urandom_seed '0'
config timeserver 'ntp'
option enabled '1'
option enable_server '0'
list server '0.openwrt.pool.ntp.org'
list server '1.openwrt.pool.ntp.org'
list server '2.openwrt.pool.ntp.org'
list server '3.openwrt.pool.ntp.org'
如果想要编辑该文件
按i键或者a键进入插入模式,这个时候左下角显示–INSERT–
编辑完成之后
按Esc键退出到命令模式
然后进入保存或者不保存的退出操作:
:w ---- 保存,不退出
:w! ---- 强制保存,不退出
:wq ---- 保存,退出(或者是:x)
:wq! ---- 强制保存,退出
:q ---- 不保存,退出
:q! ---- 不保存,强制退出
:e! ---- 放弃所有修改,从上次保存文件开始再编辑命令历史
如果是非正常关闭编辑器
再次vim该文件的时候会出现以下情形:
E325: ATTENTION
Found a swap file by the name ".system.swp"
owned by: root dated: Sun Apr 25 06:33:37 2021
file name: /etc/config/system
modified: no
user name: root host name: OpenWrt
process ID: 16987 (STILL RUNNING)
While opening file "system"
dated: Fri Apr 16 09:17:19 2021
(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 system"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".system.swp"
to avoid this message.
Swap file ".system.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
这是因为非正常关闭编辑器会生成一个.swp的文件
可以直接删除该文件
首先使用ll -a命令查看该文件
root@OpenWrt:/etc/config# ll -a
drwxr-xr-x 2 1006 1006 4096 Apr 25 06:48 ./
drwxr-xr-x 36 1006 1006 4096 Apr 23 03:17 ../
-rw------- 1 root root 12288 Apr 25 06:33 .system.swp
然后使用rm -rf删除该文件
root@OpenWrt:/etc/config# rm -rf .system.swp
再次vim该文件,就正常了