如何在VI编辑器中获取当前行的长度?
答
您可以通过strwidth
和getline
获得当前行的长度。从:h getline
*getline()*
getline({lnum} [, {end}])
Without {end} the result is a String, which is line {lnum}
from the current buffer. Example: >
getline(1)
< When {lnum} is a String that doesn't start with a
digit, line() is called to translate the String into a Number.
To get the line under the cursor: >
getline(".")
< When {lnum} is smaller than 1 or bigger than the number of
lines in the buffer, an empty string is returned.
和:h strwidth
:
strwidth({expr}) *strwidth()*
The result is a Number, which is the number of display cells
String {expr} occupies. A Tab character is counted as one
cell, alternatively use |strdisplaywidth()|.
When {expr} contains characters with East Asian Width Class
Ambiguous, this function's return value depends on 'ambiwidth'.
Also see |strlen()|, |strdisplaywidth()| and |strchars()|.
把所有这些组合起来,你可以做
echo strwidth(getline('.'))
呼应当前行的长度。当然,你也可以通过改变参数来获得特定行的长度。
echo strwidth(getline(3)) "Length of line 3
echo strwidth(getline('$')) "Length of the last line
答
写入电流线到一个外壳命令:
:.w !wc -c
也要注意,使用wc -c
的长度包括\n
。当你不希望出现这种情况,一个。减去或使用类似
:.w !tr -d '\n'|wc -c
+0
'.w'做什么? – Mistu4u
+0
点是当前行。没有代表写入的w,当前行将被系统调用的输出替换。 –
您好,我应该使用它开在VI和紧迫'文件后:'键? – Mistu4u
@ Mistu4u是的,这是一个'ex'命令,所以你需要一个冒号。 – DJMcMayhem