VI编辑器如何获取当前行长?

How to get the current line length in VI editor?

有没有办法在VI编辑器中查看光标当前所在行的长度?我尝试在 Google 中搜索,但我只能找到如何使用 "set nu".

查看当前打开的文件中的行号

您可以通过strwidthgetline获取当前行的长度。来自 :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

将当前行写入 shell 命令:

:.w !wc -c

另请注意,带有 wc -c 的长度包括 \n。如果您不想要,请减去一个或使用

之类的东西
:.w !tr -d '\n'|wc -c

使用$将光标移动到行尾,然后使用<ctrl>g显示行和列信息(以及文件名和编辑状态),看起来像这样:

"big.csv" [Modified] line 400 of 866 --46%-- col 504

更进一步,如果使用vim,您可以通过使用:se ruler打开标尺使行和列信息始终显示在右下角。然后,只要光标移动,您就会看到如下内容得到更新:

400,504 46%

您可以将 :se ruler 添加到您的 .vimrc 中,以便在 vim 启动时将此设置设置为默认设置。