vim 基础

vim 基础

vim 是一个古老的编辑器。我们暂且不表vim 的优点,先说一下缺点。vim 的诟病之一是界面丑,甚至连工具栏都没有。其实,这也算是一个优点,当我们像别人介绍一款图形编辑器时,都要先来个截图,再用方框圈出要点击的位置,数量一多,还要加上序号,否则需要一个功能都不知道去哪找,这也是各大网站介绍编辑器基本使用的视频数量极多的原因。另一个让人不愿意使用 vim 的原因是操作复杂,命令繁多(拥有同样烦恼的还有 git,有人不愿意使用 git 是觉得 git 命令多)。灵活好用的工具总是复杂的,为学习这样的工具花点时间也值得

vim 有很多模式

vim 是一个模式化的编辑器,即按键在不同模式下表现出来的行为不一样,我们时刻需要注意自己处于编辑器的哪个模式下,:set showmode 可以在编辑器左下角显示当前模式(如位于插入模式则显示 – INSERT --,普通可视模式则显示 --VISUAL --)。取消这一设置使用 :set noshowmode
经常和 :set showmode 一起使用的命令是 :set ruler,这个命令会在编辑器右下角显示当前行号和列号,以及当前行在整个文档里的位置。取消这一设置使用 :set noruler
这两个命令的显示区域均位于编辑器的最后一行,它们显示编辑器控制信息,不属于编辑区域。

动作命令的作用范围

在普通模式下,vim 下的一个操作(可以 do 或者 redo)由一个动作命令(包含一到多个字符,选择要进行的操作,如复制、删除)和一个作用范围组成,就像一条汇编指令由操作符和操作数组成。作用范围由移动命令指定,一般而言,在当前光标和移动后的光标所在处之间的所有字符(或者当前光标所在行和光标移动后所在的行之间的所有行)就是作用对象。这么说并不是很精确,作用对象是行(linewise)还是字符,如何处理范围的两个边界要视具体移动命令而定,下面的文字摘录自 vim manual,说明了第二个问题:如何处理作用范围的边界?

移动命令的分类

  • linewise
  • nonlinewise
    • inclusive
    • exclusive
A character motion is either inclusive or exclusive.  When inclusive, the
start and end position of the motion are included in the operation.  When
exclusive, the last character towards the end of the buffer is not included.
Linewise motions always include the start and end position.

Which motions are linewise, inclusive or exclusive is mentioned with the
command.  There are however, two general exceptions:
1. If the motion is exclusive and the end of the motion is in column 1, the
   end of the motion is moved to the end of the previous line and the motion
   becomes inclusive.  Example: "}" moves to the first line after a paragraph,
   but "d}" will not include that line.
						*exclusive-linewise*
2. If the motion is exclusive, the end of the motion is in column 1 and the
   start of the motion was at or before the first non-blank in the line, the
   motion becomes linewise.  Example: If a paragraph begins with some blanks
   and you do "d}" while standing on the first non-blank, all the lines of
   the paragraph are deleted, including the blanks.  If you do a put now, the
   deleted lines will be inserted below the cursor position.

Note that when the operator is pending (the operator command is typed, but the
motion isn't yet), a special set of mappings can be used.  See |:omap|.

StackOverflow 上的一个例子

Understanding Vim exclusive motion
题主(arunj)的想法:

@glts After much thought, I think i understand what you are trying to say now.
Basically i was misinterpreting what the manual meant by 'start of motion' and 'end of motion'.
In short, if i understand correctly, the 'start of motion' is always the location nearest to the start
of the buffer, and 'end of motion' is the location nearest the end of the buffer, regardless of
the 'direction' in which the motion causes the cursor to move!

如何像其它文本编辑器一样编辑文件

内容
要想像其它文本编辑器一样编辑文件内容,我们首先需要进入插入模式。进入插入模式需要命令,常用的命令有六个:大小写的 ioa
文本就是一个字符串。我们这里采用 UNIX 系统中行的概念,即行由任意多个(零个及以上)字符和换行符(UNIX 下是 LF,Windows 下是 CRLF)组成。
这些命令的帮助可以直接借助内置的 :help <command> 获取。

命令说明
iInsert text before the cursor [count] times
IInsert text before the first non-blank in the line [count] times
oBegin a new line below the cursor and insert text, repeat [count] times
OBegin a new line above the cursor and insert text, repeat [count] times
aAppend text after the cursor [count] times. If the cursor is in the first column of an empty line Insert starts there
AAppend text at the end of the line [count] times

普通模式下,光标指向某一行里的某一个字符。
当键入命令 i 后,进入插入模式,后面的字符依次填到当前光标开始的位置,原先的从当前光标开始的字符串(包括当前光标处字符)依次后移,退出插入模式后,光标左移一格,位于插入模式中输入的最后一个字符上面。
当键入命令 a 后,光标右移一格,位于当前光标后的下一个位置上并进入插入模式,后面的字符依次填到当前光标开始的位置,原先的从当前光标开始的字符串(不包括当前光标处字符)依次后移,退出插入模式后,光标左移一格,位于插入模式中输入的最后一个字符上面。
当键入命令 o 后,在当前行的 CRLF 后面添加 CRLF(即一个空行),并把光标置于换行符上,进入插入模式,相当于 $a<CRLF> 在这种情况下,编译过后,光标有可能位于换行符上(进入插入模式后直接退出)。
当键入命令 I 后,先把光标移到当前行的第一个非空白字符上,然后执行命令 i 的操作。即命令 I 相当于 ^i
当键入命令 A 后,先把光标移到当前行的最后一个字符(非换行符)上,然后执行命令 a 的操作,即命令 A 相当于 $a
当键入命令 O 后,先把光标移到上一行,再执行命令 o 的操作,即 O 命令相当于 ko

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值