如何编写 vim 脚本以在缓冲区上运行 perltidy? [英] How can I script vim to run perltidy on a buffer?
问题描述
在我目前的工作中,我们的编码风格标准与我通常遵循的标准不同.幸运的是,我们有 perltidy
的 RC 罐头文件,我可以在将文件提交给我们的审核流程之前申请重新格式化文件.
At my current job, we have coding-style standards that are different from the ones I normally follow. Fortunately, we have a canned RC file for perltidy
that I can apply to reformat files before I submit them to our review process.
我有 emacs 的代码,我用来在缓冲区上运行命令并将缓冲区替换为输出,我已经为此做了调整.但我有时会在 emacs 和 vim 之间交替使用,并希望在那里拥有相同的功能.我确信这个或类似的东西很简单,并且已经完成并重新完成了很多次.但是我没有太多运气找到任何似乎可以满足我需要的 vim-script 示例.从本质上讲,这是为了能够点击组合键(例如 Ctrl-F6,我在 emacs 中使用的)并通过 perltidy
就地重新格式化缓冲区.虽然我是一个舒适的 vim 用户,但我完全不知道为 vim 编写这种东西.
I have code for emacs that I use to run a command over a buffer and replace the buffer with the output, which I have adapted for this. But I sometimes alternate between emacs and vim, and would like to have the same capabilities there. I'm sure that this or something similar is simple and had been done and re-done many times over. But I've not had much luck finding any examples of vim-script that seem to do what I need. Which is, in essence, to be able to hit a key combo (like Ctrl-F6, what I use in emacs) and have the buffer be reformatted in-place by perltidy
. While I'm a comfortable vim-user, I'm completely clueless at writing this sort of thing for vim.
推荐答案
在尝试@hobbs 回答后,我注意到当通过 perltidy
过滤整个缓冲区时,光标返回到字节 1,我不得不记下原始行号,以便在 :Tidy
完成后返回.
After trying @hobbs answer I noticed that when filtering the entire buffer through perltidy
the cursor returned to byte 1, and I had to make a mental note of the original line number so I could go back after :Tidy
completed.
因此,基于@hobbs' 和 @Ignacio 的回答,我在 .vimrc
中添加了以下内容:
So building on @hobbs' and @Ignacio's answers, I added the following to my .vimrc
:
"define :Tidy command to run perltidy on visual selection || entire buffer"
command -range=% -nargs=* Tidy <line1>,<line2>!perltidy
"run :Tidy on entire buffer and return cursor to (approximate) original position"
fun DoTidy()
let l = line(".")
let c = col(".")
:Tidy
call cursor(l, c)
endfun
"shortcut for normal mode to run on entire buffer then return to current line"
au Filetype perl nmap <F2> :call DoTidy()<CR>
"shortcut for visual mode to run on the current visual selection"
au Filetype perl vmap <F2> :Tidy<CR>
(关闭 "
添加到注释中以突出显示 SO 语法(不是必需的,但有效的 vim 语法))
(closing "
added to comments for SO syntax highlighting purposes (not required, but valid vim syntax))
DoTidy()
将光标返回到原来的位置最多加上或减去 X
个字节,其中 X
是字节数perltidy
相对于原始光标位置添加/删除.但是只要你保持整洁,这就是相当微不足道的:)
DoTidy()
will return the cursor to its original position plus or minus at most X
bytes, where X
is the number of bytes added/removed by perltidy
relative to the original cursor position. But this is fairly trivial as long as you keep things tidy :).
[Vim 版本:7.2]
[Vim version: 7.2]
更新了 DoTidy()
以包含@mikew 的注释以提高可读性和与 Vim 7.0 的兼容性
Updated DoTidy()
to incorporate @mikew's comment for readability and for compatibility with Vim 7.0
这篇关于如何编写 vim 脚本以在缓冲区上运行 perltidy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!