2017-10-18 186 views

回答

0

您可以通過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 
+0

您好,我應該使用它開在VI和緊迫'文件後:'鍵? – Mistu4u

+0

@ Mistu4u是的,這是一個'ex'命令,所以你需要一個冒號。 – DJMcMayhem

2

寫入電流線到一個外殼命令:

:.w !wc -c 

也要注意,使用wc -c的長度包括\n。當你不希望出現這種情況,一個。減去或使用類似

:.w !tr -d '\n'|wc -c 
+0

'.w'做什麼? – Mistu4u

+0

點是當前行。沒有代表寫入的w,當前行將被系統調用的輸出替換。 –

相關問題