linux中vim的保存退出命令

使用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该文件,就正常了

  • 11
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值