基于文件长度的动态 vim 字体大小
我希望 vim (MacVim) 默认为新/短文件使用大字体,并随着行数的增加动态缩小到较小的字体(到设定的最小值)。这可以通过插件来实现吗?我需要了解哪些 vim 概念才能编写该插件?
我想要这个的原因是我喜欢用大字体编写代码,但是当文件变长后我宁愿眯着眼睛也不愿滚动。
I want vim (MacVim) to default to a large font for new/short files and dynamically scale down to a smaller font (to a set minimum) as the number of lines grows. Is this possible to do with a plugin? What vim concepts will I need to know to write that plugin?
My reason for wanting this is that I love writing code in a large font, but after files get longer I'd rather squint a little than scroll.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个有趣的想法。不确定我是否会使用它:-)——但这确实是一个有趣的想法。
您不需要编写完整的插件,因为它所需要做的就是执行一些数学运算。更具体地说,一个粗略的公式是:
其中所需的大小 (S )取决于当前文档的行数 (n)、确定什么被视为大文件的常量(k,以行为单位)、所需的幅度 ( a)——意味着大小会变化多少——以及最小字体大小(m)。
现在我们知道了这一点,剩下的就是实施它了。快速说明:
line()
函数,传递"$"
作为参数。exec
执行它的数字考虑到这一点,一个具有描述性的快速函数可以写成:
我确信其他 Vim 高手可以对此进行很大改进。无论如何,快速解释一下:
将其放入您的 .vimrc 中或从其他文件中获取它,您就可以准备测试它了。在只有一行的文件上,字体设置为 16。如果有 39 行,则大小也为 16,但如果有 40 行,则大小为 15。当有 80 行时,大小变为 14,依此类推。
您可能想自动调用它,因此还要创建一个自动命令。
正如名称所示,只有当您输入缓冲区时,这才会起作用。您可以将其更改为包含
InsertLeave
或类似内容,但请记住,这将生成对该函数的更多调用。但不应该是性能问题。检查
:h autocommand-events
并根据需要构建autocmd
。更新
正如 ZyX 在评论中指出的那样,函数的最后一行可以写为:
That's an interesting idea. Not sure if I'd use it :-) — but it's certainly an interesting idea.
You don't need to write a complete plugin, as all it needs to do is to perform some math. More specifically, a rough formula would be:
Where the desired size (S) depends on the current document number of lines (n), a constant determining what is considered a big file (k, in lines), the desired amplitude (a) — meaning how much will the size vary — and a minimum font size (m).
Now that we know that, it's just a matter of implementing it. Quick notes:
line()
function passing"$"
as argumentexec
With that in mind, a quick function quite descriptive could be written as:
I'm sure that other Vim masters can improve this a lot. Anyway, a quick explanation:
Put that in your .vimrc or source it from other file and you'll be ready to test it. On a file with one line, the font is set to 16. If there are 39 lines, also size 16 but size 15 if there are 40. Size goes to 14 when there are 80 lines and so on.
You probably want to call this automatically, so create an auto command as well.
This will work only when you enter a buffer, as the name says. You could change that to include
InsertLeave
or something like, but keep in mind this will generate more calls to the function. Should not be a performance problem though.Check
:h autocommand-events
and build theautocmd
as you like.Update
As ZyX pointed out in the comments, the last line from the function could be written as:
vim 在终端中执行。字体大小取决于终端,因此您所要求的可能是不可能的,除非您的 vim 直接要求终端更改字体大小...这可能很难做到。
vim executes in a terminal. Font size is terminal dependant, so what you ask may be impossible unless your vim ask directly the terminal to change font size... which may be difficult to do.