如何在空文件(vim)中以可视块模式(Ctrl+v)选择矩形区域?

发布于 2024-09-18 09:27:58 字数 593 浏览 9 评论 0原文

我可以轻松地在文件中选择一个矩形区域,前提是该区域包含文本或空格(或任何内容)。我使用视觉块模式和运动键来完成此操作。 但是当我尝试在新文件(或任何没有文本的文件)中创建这样的区域时,我不能。 有没有办法通过自动填充空格来“扩展”该区域?或者我走错了方向?

我想要这个的原因:

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提笔书几行 2024-09-25 09:27:58

除了布莱恩·拉斯穆森(Brian Rasmussen)的(非常好的)答案之外,我知道几乎完全按照您的要求进行操作的唯一方法是使用virtualedit模式。这不会让您在不存在的行上进行编辑,但可以让您在现有行的末尾之外进行编辑。因此,要将当前行变成 # 符号的负载,您可以这样做:

:set virtualedit=all
v50lr#

要创建 50x5 块,您可以创建 4 个新的空行,然后执行相同的操作:(

:set virtualedit=all
4o<ESC>
<C-V>4k50lr#

其中 表示按 Ctrl+V 表示按 Esc)。

我相信有一些适用于各种文件类型的插件可以更轻松地创建这样的注释块,但我不确定哪个是最好的。

你可以这样做:

50i#<ESC>yyo#<ESC>48a<SPACE><ESC>a#<ENTER>#<SPACE><SPACE>My comment goes here<ESC>:exe<SPACE>'normal'<SPACE>(49-getpos('.')[2]).'a<SPACE>'<ENTER>a#<ENTER>#<ESC>48a<SPACE><ESC>a#<ESC>p

但也许这只是我太傻了!如果您有兴趣,我会将其作为练习,让读者了解其中发生的情况(:help 是您的朋友)。

作为一个稍微更严肃的替代方案怎么样:在 vimrc 中或 vim 运行时文件夹的插件目录中的文件中添加以下内容(例如 Unix 上的 ~/.vim/plugins)

nmap <F4> :InsertCommentBlock<CR>
command! InsertCommentBlock call InsertCommentBlock()
function! InsertCommentBlock()
    let linelength = 50
    let linelist = []

    call add(linelist, repeat('#', linelength))
    call add(linelist, '#' . repeat(' ', linelength-2) . '#')
    let comment = input('Please enter a comment: ')
    call add(linelist, '#  ' . comment . repeat(' ', linelength - (4+len(comment))) . '#')
    call add(linelist, '#' . repeat(' ', linelength-2) . '#')
    call add(linelist, repeat('#', linelength))

    call append(line('.'), linelist)
endfunction

请参阅:

:help function
:help 'virtualedit'
:help command
:help nmap
:help repeat()
:help append()
:help add()
:help getpos()
:help :exe

等等...

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:

:set virtualedit=all
v50lr#

To make a 50x5 block, you could create 4 new blank lines and then do the same:

:set virtualedit=all
4o<ESC>
<C-V>4k50lr#

(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:

50i#<ESC>yyo#<ESC>48a<SPACE><ESC>a#<ENTER>#<SPACE><SPACE>My comment goes here<ESC>:exe<SPACE>'normal'<SPACE>(49-getpos('.')[2]).'a<SPACE>'<ENTER>a#<ENTER>#<ESC>48a<SPACE><ESC>a#<ESC>p

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)

nmap <F4> :InsertCommentBlock<CR>
command! InsertCommentBlock call InsertCommentBlock()
function! InsertCommentBlock()
    let linelength = 50
    let linelist = []

    call add(linelist, repeat('#', linelength))
    call add(linelist, '#' . repeat(' ', linelength-2) . '#')
    let comment = input('Please enter a comment: ')
    call add(linelist, '#  ' . comment . repeat(' ', linelength - (4+len(comment))) . '#')
    call add(linelist, '#' . repeat(' ', linelength-2) . '#')
    call add(linelist, repeat('#', linelength))

    call append(line('.'), linelist)
endfunction

See:

:help function
:help 'virtualedit'
:help command
:help nmap
:help repeat()
:help append()
:help add()
:help getpos()
:help :exe

etc...

沙与沫 2024-09-25 09:27:58

如果您想在新文件中创建类似的块,您可以执行类似 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.

乱世争霸 2024-09-25 09:27:58

接受的答案提到使用

:set virtualedit=all

但是,然后您在所有模式下启用 virtualedit,这可能不是您想要的。实际上还有一个选项可以在块编辑模式下启用它:

:set virtualedit=block

这是我放在 vimrc 中的内容

The accepted answer mentions using

:set virtualedit=all

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:

:set virtualedit=block

This is what I put in my vimrc

情归归情 2024-09-25 09:27:58

我不认为有任何可以选择不存在的文本,但简单的解决方案是用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文