如何使用 Vim 用字符 x 填充一行到 y 列 [英] How to fill a line with character x up to column y using Vim

查看:21
本文介绍了如何使用 Vim 用字符 x 填充一行到 y 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Vim 用指定的字符填充一行的其余部分直到某一列?例如,假设光标位于第四列,我想用虚线填充当前行的其余部分,直到第 80 列.我该怎么做?

How can I fill the remainder of a line with the specified character up to a certain column using Vim? For example, imagine that the cursor is on column four and I want to fill the remainder of the current line with dashes up to column 80. How would I do that?

推荐答案

这里有一个函数来实现你的要求,还有更多.

Here's a function to implement what you ask, and slightly more.

  • 它从当前行尾而不是光标位置填充该行
  • 它强制当前行和重复字符之间有一个空格
  • 它允许您指定任何字符串来填充该行的其余部分
  • 它使用 vim 的 textwidth 设置来决定该行的长度(而不仅仅是假设 80 个字符)
  • It fills the line from its current end of line, rather than the cursor position
  • It forces a single space between what's currently on the line and the repeated chars
  • It allows you to specify any string to fill the rest of the line with
  • It uses vim's textwidth setting to decide how long the line should be (rather than just assuming 80 chars)

函数定义如下:

" 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 中,并对其进行映射,例如

Insert that into your .vimrc, and make a mapping to it, e.g.

map <F12> :call FillLine( '-' )

然后你可以按 F12 将连字符应用到当前行

Then you can press F12 to apply the hyphens to the current line

注意:这可能很容易扩展到在 VISUAL 模式下对选择进行操作,但目前仅适用于单行.*

Note: this could probably be easily extended to act on a selection in VISUAL mode, but currently works for single lines only.*

这篇关于如何使用 Vim 用字符 x 填充一行到 y 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆