gpt4 book ai didi

vim - 如何使用Vim用字符x填充一行直到y列

转载 作者:行者123 更新时间:2023-12-03 09:13:38 24 4
gpt4 key购买 nike

如何使用Vim使用指定字符填充行的其余部分,直到特定列?例如,假设光标在第四列上,而我想用到第80列的破折号填充当前行的其余部分。我该怎么做?

最佳答案

这是一个功能,可以实现您的要求,甚至更多。

  • 它从其当前行尾开始填充行,而不是光标位置
  • 它强制当前行和重复的字符
  • 之间有一个空格
  • 它允许您指定任何字符串以用
  • 填充行的其余部分
  • 它使用vim的textwidth设置来决定该行应该多长时间
    (而不是仅假设80个字符)

  • 该函数定义如下:
    " fill rest of line with characters
    function! FillLine( str )
    " set tw to the desired total length
    let tw = &textwidth
    if tw==0 | let tw = 80 | endif
    " strip trailing spaces first
    .s/[[:space:]]*$//
    " calculate total number of 'str's to insert
    let reps = (tw - col("$")) / len(a:str)
    " insert them, if there's room, removing trailing spaces (though forcing
    " there to be one)
    if reps > 0
    .s/$/\=(' '.repeat(a:str, reps))/
    endif
    endfunction

    将其插入到 .vimrc中,并对其进行映射,例如
    map <F12> :call FillLine( '-' )

    然后您可以按 F12将连字符应用于当前行

    注意:可以很容易地扩展为在VISUAL模式下对选择进行操作,但目前仅适用于单行。*

    关于vim - 如何使用Vim用字符x填充一行直到y列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3364102/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com