VIM 中的字符串格式化(填充)

发布于 2024-10-16 19:11:37 字数 1003 浏览 5 评论 0原文

VIM 中的字符串格式化让我头疼。

我想我已经习惯了 Python 中的字符串格式化,你可以在其中指定如何格式化 输出字符串将被分隔,之间有多少个空格等等。

所以我有一个 VIM 插件,它可以将信息输出到临时缓冲区,如下所示:

Line: 1 ==>> ErrorName ==>> File Path: /foo

我在将字符串附加到缓冲区之前构建该字符串,一切都运行得非常好。但是,每当你有不同的行号(数字方面)时,你就会得到这样的结果:

Line: 1 ==>> Error ==>> File Path: /foo
Line: 123  ==>> ErrorNameLong ==>> File Path: /foo
Line: 12  ==>> ErrorShort ==>> File Path: /foo

我尝试做制表符而不是空格,但这只会使情况变得更糟(空格变得更大)。所以我理想的最终结果必须是这样的:

Line: 1    ==>> Error         ==>> File Path: /foo
Line: 123  ==>> ErrorNameLong ==>> File Path: /foo
Line: 12   ==>> ErrorShort    ==>> File Path: /foo

我很清楚有帮助的插件(例如 tabular.vim),但由于这是一个插件本身,我不想仅仅为了简单而需要依赖于不同的插件字符串格式化。

这些是我到目前为止尝试过的事情:

  • 制表符(和制表符+空格)
  • 搜索并替换为制表符(以及在缓冲区中渲染后带有空格的制表符)

在我看来,必须有一个好的方法来解决这个问题,而我只是缺少它。

如何在VIM中实现我需要的字符串格式?

String formatting in VIM is giving me headaches.

I guess I feel very used to string formatting in Python where you can specify how
output string are going to be delimited, with how many spaces in between and so on.

So I have a VIM plugin that outputs information to a scratch buffer that looks like:

Line: 1 ==>> ErrorName ==>> File Path: /foo

I build that string before appending it to a buffer and everything works fantastic. But whenever you have line numbers that are different (digit wise) you get things like this:

Line: 1 ==>> Error ==>> File Path: /foo
Line: 123  ==>> ErrorNameLong ==>> File Path: /foo
Line: 12  ==>> ErrorShort ==>> File Path: /foo

I have tried doing doing tabs instead of spaces but it only makes it worse (the spaces grow even bigger). So my ideal end result would have to be something like this:

Line: 1    ==>> Error         ==>> File Path: /foo
Line: 123  ==>> ErrorNameLong ==>> File Path: /foo
Line: 12   ==>> ErrorShort    ==>> File Path: /foo

I am well aware of plugins that help (e.g. tabular.vim) but since this is a plugin itself, I do not want to require a dependency on a different plugin just for simple string formatting.

These are the things I have tried so far:

  • Tabs (and tabs + spaces)
  • Search and replace with tabs (and tabs with spaces after rendering in buffer)

It seems to me that there must be a good approach to this and I am just missing it.

How can achieve the string formatting I need in VIM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-10-23 19:11:37

我使用两个小函数,没什么花哨的,一个在字符串后面添加填充,一个如果我想在字符串前面添加。 PrePad 函数允许填充字符的可选参数,我有时用它来用前面的 0 填充数字。

function! Pad(s,amt)
    return a:s . repeat(' ',a:amt - len(a:s))
endfunction

 "  Pad('abc', 5) == 'abc  '
 "  Pad('ab', 5) ==  'ab   '


function! PrePad(s,amt,...)
    if a:0 > 0
        let char = a:1
    else
        let char = ' '
    endif
    return repeat(char,a:amt - len(a:s)) . a:s
endfunction

" PrePad('832', 4)      == ' 823'
" PrePad('832', 4, '0') == '0823'

使用 Pad() 构建原始日志消息会很简单。像这样的东西:

echo 'Line: ' . Pad(linenum,8) . '==>> ' . Pad(errmsg,12) . '==>> FilePath: ' . path

I use two little functions, nothing fancy, one to add padding after string and one if I want to add in front of string. The PrePad function allows for an optional argument of the padding character, which I sometimes use to pad numbers with preceding 0's.

function! Pad(s,amt)
    return a:s . repeat(' ',a:amt - len(a:s))
endfunction

 "  Pad('abc', 5) == 'abc  '
 "  Pad('ab', 5) ==  'ab   '


function! PrePad(s,amt,...)
    if a:0 > 0
        let char = a:1
    else
        let char = ' '
    endif
    return repeat(char,a:amt - len(a:s)) . a:s
endfunction

" PrePad('832', 4)      == ' 823'
" PrePad('832', 4, '0') == '0823'

It would be simple to Pad() in building your original log messages. Something like:

echo 'Line: ' . Pad(linenum,8) . '==>> ' . Pad(errmsg,12) . '==>> FilePath: ' . path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文