1

I want to paste text which spans across multiple lines and is rectangular. For example, a rectangular box representing a device topology diagram.

      +-------+
       |         |
       |         |
      +-------+

When I paste this box, it should replace the existing characters and get pasted. I believe this is kind of replace mode. Not only first line, every line in my block should be pasted at the same column in every line (, that is, the block should be pasted as block itself, without pasting the second line of my block in start of the line.)

What I want is a mix of replace and visual block mode.

Before:

+------+
|        |
|        |
|        |
+------+

After:

  +------+.     +-------+
 |      |
 |      |
  |      |
  +------+
 |      |
 |      |
 +------+

Expected:

 +------+.   +-----+
 |      |     |     |
 |      |     |     |
 |      |     |     |
 +------+.   +-----+

Second box is the one I pasting. I am familiar with visual block mode and can paste manually. But I want to paste this way, through a function or set of commands.

7
  • 2
    This is not very clear to me. Can you add an example before/after?
    – muru
    May 2, 2016 at 7:50
  • I tried this :iabbr <ESC><CR>P. This works when I have the multiline rectangular text in the recent copy register. But I will need to paste these kind of text in any file, by storing them in iabbr or a function similar to this.
    – SibiCoder
    May 2, 2016 at 8:15
  • 3
    @SibiCoder: I have read a lot of your questions lately and they all are pretty hard to understand (they are all edited by other users). I do understand that english isn't your primary language (it is not mine either and I don't pretend that I never make unclear sentences) but please try to make your questions more clear. Make short sentences, try to be more precise about what you're talking about, include example of commands you are using. It will really help us to help you. I hope that you understand that this comment is respectul and friendly: my only goal is to have more clear questions :-)
    – statox
    May 2, 2016 at 9:39
  • 2
    @SibiCoder: Thank you, indeed it is clear now: I retracted my close vote. Now on my system if I select the box to past with visual-block (ctrl-v instead of v), put my cursor on the last character of the first line of the first box and paste with p, the second box is pasted next to the other one... Do you use visual-block to select the box?
    – statox
    May 2, 2016 at 12:49
  • 1
    @SibiCoder I don't know if this is what you want, but you could try this: vpaste.net/CmvOX Hit \y in visual mode to copy a rectangular box, then \p in normal mode to paste it by replacing a box of text with similar dimensions. May 8, 2016 at 13:03

1 Answer 1

3

I'm not entirely sure what you want to accomplish, but from what I understand, you want to easily create boxes using commands/functions. Maybe this will help you get started on a solution.

function! s:makebox(...) abort
  let width = str2nr(a:1)
  let height = a:0 < 2 ? width : str2nr(a:2)
  let margin = a:0 > 2 ? str2nr(a:3) : 0
  let r_margin = repeat(' ', margin)

  let x = '+'.repeat('-', width - 2).'+'.r_margin
  let y = '|'.repeat(' ', width - 2).'|'.r_margin

  let lines = [x]
  call extend(lines, repeat([y], height - 2))
  call add(lines, x)
  call setreg('b', lines, 'b'.(width + margin))
endfunction


command! -nargs=+ MakeBox call s:makebox(<f-args>)
nnoremap <leader>b mb$"bp`b

You create a box with MakeBox width [[height] margin] which stores it in the b register as blockwise text. The height and margin are optional. <leader>b makes it easy to paste the stored box at the end of the current line.

MakeBox example

2
  • 1
    This is just an example. What I wanted to do is, "paste blockwise". My text has multiple lines, whenever I paste it, all lines of my text should be pasted in the cursor's position in all lines. Please modify your answer .
    – SibiCoder
    May 8, 2016 at 4:43
  • 2
    I think my answer is perfectly fine with your given question. If you want to paste blockwise text next to each other, the answer is simply: $p (which is what the script is basically doing). But, the effort you put into your question and your statement of how you are already familiar with visual blocks lead me to believe it wasn't that simple.
    – Tommy A
    May 8, 2016 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.