Blog

Always use normal!

Content #

假设有如下映射:

:nnoremap G dd

这时执行如下命令:

:normal G

就会删除一行。

要想避免引入用户的映射,要使用normal!:

:normal! G

总是优先使用normal!.

From #

Function Varargs

Content #

:function Varg2(foo, ...)
: echom a:foo
: echom a:0
: echom a:1
: echo a:000
:endfunction
:call Varg2("a", "b", "c")

a:0 存放可变参数list的长度,这里是2。 a:1 是可变参数中的第1个。这里是"b"。 a:000 是可变参数list.

From #

:help function-argument

Explicit Case Sensitive or Insensitive Comparisons

Content #

Examples:

"abc" ==# "Abc"	  evaluates to 0
"abc" ==? "Abc"	  evaluates to 1
"abc" == "Abc"	  evaluates to 1 if 'ignorecase' is set, 0 otherwise

使用两个等号的比较,表达式的结果取决于用户的设置,很不确定。

From #

:help expr4

Coercion of Strings to Integers

Content #

:echom "hello" + 10

Vim会将非数字开头的字符串coerce成0,因此上面的结果会是10。

:echom "10hello" + 10

数字开头的字符串会coerce成整数,“10hello"的结果是10,最终结果是20。

:echom "hello10" + 10

非数字开头,“hello10"的结果是0,最终结果是10。

:echom "Hello, " + "world"

结果为0。

:echom 10 + "10.10"

Vim不会将string coerce成Floats. 结果为20.

From #

Variables and Options(Vim)

Read option as variables #

:set textwidth=80
:echo &textwidth

布尔值会以0和非0来表示。

:set nowrap
:echo &wrap

Vim displays 0.

Set option as variables #

:let &textwidth=100
:set textwidth?

使用let的形式来设置option的好处在于可以使用vimscript的全部功能:

:let &textwidth = &textwidth + 10

set则只能将其设置为固定的值。

Local Options #

:let &l:number = 1

section heading text object in markdown

Content #

Markdown文件的标题格式如下:

Topic One
=========

some text about topic one.

it has multiple paragraphs.

Topic Two
=========

some text about topic two.
after the last.

inside section’s heading:

:onoremap ih :<c-u>execute "normal! ?^==\\+$\r:nohlsearch\rkvg_"<cr>

around section’s heading:

:onoremap ah :<c-u>execute "normal! ?^==\\+$\r:nohlsearch\rg_vk0"<cr>

From #

每一行前面插入行号和空格

每一行前面插入行号和空格 #

:%s/^/\=line('.') . ' '/

line 是 Vim 的一个内置函数,line(’.’) 表示“当前”行的行号。

From #

gi与g;

gi与g; #

Vim中下面两个Mark的含义有何区别?

`^
`.
`^ To the position where the cursor was the last time when Insert mode was stopped.
`. To the position where the last change was made. The position is at or near where the change started.

前者针对的Insert mode的切换,后者针对的是文本的变化。像x,p等命令会改变文本,但不涉及Insert mode的切换。

前者可用gi命令来操作,后者由g;命令来操作。

From #