gpt4 book ai didi

vim - 在 VIM 中用 # 居中和填充多个空间

转载 作者:行者123 更新时间:2023-12-04 14:55:45 33 4
gpt4 key购买 nike

我想在 Vim 中文件的不同部分之间进行一些很好的分离:
我想用#'s 填充一行,然后在中间写上我的标题:
############################# 居中标题################## ###############
所以,现在(因为默认情况下终端是 80 个字符宽),我做

i (insert mode)
#
esc
79 .
这使得一行#'s。
然后我必须计算标题的宽度,计算起点,转到计算出的起点并替换为 R。
这有点乏味......另一方面,我知道在可视模式下使用 :center 将文本居中放置在 VIM 中。
是否可以将两者结合起来更直接地获得我正在寻找的东西?

最佳答案

由于链接线程中的答案不包含解释的 vimscript 解决方案,因此我将分享我当时编写的解决方案。

function Foo()
let title = input('Title: ')
put =title
center
let line=getline(line('.'))
let spaces = matchstr(line, '^\s*\ze\s')
let prefix = substitute(spaces, ' ', '#', 'g')
call setline(line('.'), prefix . ' ' . trim(line) . ' ')
normal 80A#
normal d80|
endfunction

noremap <leader>x :call Foo()<cr>
let title = input('Title: ') -> Function that ask for title and store it in var
put =title -> paste content to current line
center -> center it to fill beginning with spaces
let line=getline(line('.')) -> get content of current line into variable
let spaces = matchstr(line, '^\s*\ze\s') -> stores all whitespaces expect last one into variable
let prefix = substitute(spaces, ' ', '#', 'g') -> converts spaces to #
call setline(line('.'), prefix . ' ' . trim(line) . ' ') -> changes content of current line
normal 80A# -> append many # at the end of line
normal d80| -> delete everything after 80 column

关于vim - 在 VIM 中用 # 居中和填充多个空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68054933/

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