如何在空文件(vim)中以可视块模式(Ctrl+v)选择矩形区域?
我可以轻松地在文件中选择一个矩形区域,前提是该区域包含文本或空格(或任何内容)。我使用视觉块模式和运动键来完成此操作。 但是当我尝试在新文件(或任何没有文本的文件)中创建这样的区域时,我不能。 有没有办法通过自动填充空格来“扩展”该区域?或者我走错了方向?
我想要这个的原因:
我用 vim 创建一个新文件,然后想创建一个与此类似的注释块:
##############################################
# #
# My comment goes here #
# #
##############################################
我可以使用 Ctrl+v+ 运动键在现有文本上执行此操作,然后 r# 创建一个充满磅的区域。然后类似地剪出带有空格的内部矩形。
如何在新文件上使用此技术?
谢谢。
I can easily select a rectangular area in a file provided this area contains text or spaces (or anything). I do it with visual block mode and motion keys.
But when I try to create such area in a new file (or any file without text) I can't.
Is there a way to 'expand' this area by automatically filling it with spaces for example? Or am I going in wrong direction?
The reason I want this:
I create a new file with vim and then would like to create a comment block similar to this:
##############################################
# #
# My comment goes here #
# #
##############################################
I can do it over existing text using Ctrl+v+motion keys, then r# to create an area filled with pounds. Then similarly to cut out inner rectangle with spaces.
How do I use this technique on a new file?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了布莱恩·拉斯穆森(Brian Rasmussen)的(非常好的)答案之外,我知道几乎完全按照您的要求进行操作的唯一方法是使用
virtualedit
模式。这不会让您在不存在的行上进行编辑,但可以让您在现有行的末尾之外进行编辑。因此,要将当前行变成 # 符号的负载,您可以这样做:要创建 50x5 块,您可以创建 4 个新的空行,然后执行相同的操作:(
其中
表示按 Ctrl+V,
表示按 Esc)。我相信有一些适用于各种文件类型的插件可以更轻松地创建这样的注释块,但我不确定哪个是最好的。
你可以这样做:
但也许这只是我太傻了!如果您有兴趣,我会将其作为练习,让读者了解其中发生的情况(
:help
是您的朋友)。作为一个稍微更严肃的替代方案怎么样:在 vimrc 中或 vim 运行时文件夹的插件目录中的文件中添加以下内容(例如 Unix 上的 ~/.vim/plugins)
请参阅:
等等...
Apart from the (very good) answer from Brian Rasmussen, the only way I know of to do almost exactly what you're asking is to use
virtualedit
mode. This won't let you edit on non-existent lines, but it will let you edit beyond the end of existing lines. Therefore, to turn the current line into a load of # symbols, you could do this:To make a 50x5 block, you could create 4 new blank lines and then do the same:
(where
<C-V>
means press Ctrl+V and<ESC>
means press Esc).I believe there are some plugins for various file types that make it much easier to create comment blocks like this, but I'm not sure which is best.
You could just do something like:
But maybe that's just me being silly! I'll leave it as an exercise for the reader to figure out what's going on there if you're interested (
:help
is your friend).How about this as a slightly more serious alternative: bung the following in your vimrc or in a file in the plugins directory of the vim runtime folder (e.g. ~/.vim/plugins on Unix)
See:
etc...
如果您想在新文件中创建类似的块,您可以执行类似 50i#EscY5P
因此,插入 # 50 次,然后抽出当前行并放入 5 次。这将为您提供一个 50x5 的 # 块。
当然,你仍然必须做,无论你做什么,从#s块到你的帖子中的评论。
If you want to create a block like that in a new file, you could do something like 50i#EscY5P
So it's 50 times insert # followed by yank current line and put it 5 times. This will give you a 50x5 block of #.
Of course you still have to do, whatever you do to get from the block of #s to the comment in your post.
接受的答案提到使用
但是,然后您在所有模式下启用 virtualedit,这可能不是您想要的。实际上还有一个选项可以在块编辑模式下启用它:
这是我放在 vimrc 中的内容
The accepted answer mentions using
However, then you enable virtualedit in all modes, which might not what you want. There is actually also an option to enable it in just block editing mode:
This is what I put in my vimrc
我不认为有任何可以选择不存在的文本,但简单的解决方案是用
76a yy4p
之类的东西“播种”它......所以绘制 76 个空格,然后复制4号线加时。I don't think there's any to select text that isn't there, but the easy solution would be to "seed" it with something like
76a yy4p
... so draw 76 spaces, then copy the line 4 extra times.