Pad all lines with spaces to a fixed width in Vim or using sed, awk, etc

后端 未结 8 1774
日久生厌
日久生厌 2021-02-02 12:33

How can I pad each line of a file to a certain width (say, 63 characters wide), padding with spaces if need be?

For now, let’s assume all lines are guaranteed to be less

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-02 12:57

    In Vim, I would use the following command:

    :%s/$/\=repeat(' ',64-virtcol('$'))
    

    (The use of the virtcol() function, as opposed to the col() one, is guided by the necessity to properly handle tab characters as well as multibyte non-ASCII characters that might occur in the text.)

提交回复
热议问题