这是一个vim函数,它通过用2个空格替换单个拆分管道字符来更改某些行。它报告替换后的行长度与之前相同
" :T5 replaces all ¦ with 2 spaces
com! T5 call T5()
function! T5()
let @l=len(getline("'a"))
'a,'bs/¦/ /ge
let @m=len(getline("'a"))
let @n=@m-@l
exe "norm 'ak$xp".@n.".'bj$."
endfunction
@n持续为零。
这是一个失败的数据样本
----------------------------------------------
Special number¦Sign¦Exponent (biased)¦Mantissa
--------------¦----¦-----------------¦--------
+0 ¦ 0 ¦ 0 ¦ 0
-0 ¦ 1 ¦ 0 ¦ 0
+ ¦ 0 ¦ FFH ¦ 0
- ¦ 1 ¦ FFH ¦ 0
NaN ¦0/1 ¦ FFH ¦ !=0
Denormals ¦0/1 ¦ 0 ¦ !=0
----------------------------------------------
它用空格进行空格替换,但不完全用破折号扩展两行。
答案 0 :(得分:2)
len()
返回字符串中的字节数,而不是返回的单元格数。来自:h len()
:
len({expr}) The result is a Number, which is the length of the argument.
When {expr} is a String or a Number the length in bytes is
used, as with |strlen()|.
¦
个字符占用2个字节,与两个空格相同,因此len(getline("'a"))
的值保持不变。
改为使用strdisplaywidth()
,它返回显示的单元格数:
strdisplaywidth({expr}[, {col}]) strdisplaywidth()
The result is a Number, which is the number of display cells
String {expr} occupies on the screen when it starts at {col}.
When {col} is omitted zero is used. Otherwise it is the
screen column where to start. This matters for Tab
characters.
The option settings of the current window are used. This
matters for anything that's displayed differently, such as
'tabstop' and 'display'.
When {expr} contains characters with East Asian Width Class
Ambiguous, this function's return value depends on 'ambiwidth'.
Also see |strlen()|, |strwidth()| and |strchars()|.