How to select a rectangular area in the visual block mode (Ctrl + v) in an empty file (vim)?

I can easily select a rectangular area in a file if that area contains text or spaces (or something else). I do this with visual block mode and motion keys. But when I try to create such an area in a new file (or any file without text), I cannot. Is there a way to "expand" this area by automatically filling it with spaces, for example? Or am I going in the wrong direction?

The reason I want:

I am creating a new file with vim, and then I would like to create a comment block similar to this:

##############################################
#                                            #
#  My comment goes here                      #
#                                            #
##############################################

I can do this on top of existing text using Ctrl+v+ motion keys, then r#to create an area filled with pounds. Then similarly cut out the inner rectangle with spaces.

How to use this technique in a new file?

Thank.

+3
source share
4 answers

Besides Brian Rasmussen's (very good) answer, the only way I know almost exactly what you are asking is to use the mode virtualedit. This will not allow you to edit on non-existent lines, but it will allow you to edit outside existing lines. Therefore, to turn the current line into a load of # characters, you can do this:

:set virtualedit=all
v50lr#

50x5, 4 , :

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

( <C-V> Ctrl + V <ESC> 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 (, ~/.vim/plugins 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

:

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

....

+5

, - 5 0 i # Esc Y 5 P

, 50- #, yank 5 . 50x5 #.

, , , #s .

+4

:set virtualedit=all

virtualedit , , . :

:set virtualedit=block

vimrc

+1

, - , , "" - 76a yy4p... 76 , 4 .

0

Source: https://habr.com/ru/post/1763776/


All Articles