vim - 在VIM中用#居中和填充多个空间
问题描述
我想在 Vim 文件的不同部分之间做一些很好的分离:
我想用 # 填充一行,然后在中间写下我的标题:
############################# 居中标题################## ##############
所以,现在(因为默认情况下终端是 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
推荐阅读
- javascript - 如何将多行正则表达式文本转义为有效的 json?
- php - 使用 PDO 查询的静态函数错误
- java-7 - 未初始化的密钥库:即使在加载之后
- ruby-on-rails - Ruby on Rails - 如何处理 gem 依赖
- javascript - 当字符串并排具有相同字符时,按字符分割()
- r - RShiny 应用程序无法连接到 SQL Server
- php - 为用户插入的每个自定义分类术语自动创建新帖子
- apache-spark - 根据 Pyspark 数据框中的条件分配分数
- google-cloud-platform - Google Cloud Platform Pub Sub - Python 客户端不发布消息
- python - 在 Python 中使用 StatsModels 运行 GLM