Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pasting with overwrite in Vim

Tags:

vim

I want to be able to paste something from the buffer (probably using p), but instead of inserting it into the text, I want to replace whatever was there before (just like the R command). I've searched Google, vim documentation, and Stack Overflow but could not find anything on the issue. I imagine that it's just a command that I don't know about. Any help would be appreciated.

That's all I need to know, but if you want to know my specific problem:

Essentially I'm just trying to create a short script for documentation headers. At the beginning of every function I put the following:

// FunctionName <><><><><><><><><><><><><><><><><><><><>

However adding all those <> gets annoying. I want to be able to place my cursor on a function name, press the F6 key and it produce the above. The problem, of course, is that function names are not constant sizes and it would make the "chain" look weird. So I just want to paste OVER a bunch of pre-made chain so that the whole thing will always be a constant number of characters. i.e.:

start with

//<><><><><><><><><><><><><><><><><><><><><><><><><><><>

Paste " FunctionName " and end with

// FunctionName <><><><><><><><><><><><><><><><><><><><>
like image 927
Tumbler41 Avatar asked Aug 20 '14 19:08

Tumbler41


People also ask

How do I overwrite in Vim?

Replace mode allows you replace existing text by directly typing over it. Before entering this mode, get into normal mode and put your cursor on top of the first character that you want to replace. Then press 'R' (capital R) to enter replace mode. Now whatever you type will replace the existing text.

How do I replace a word with yanked in Vim?

Move the cursor to another word (say "third"). Repeat the operation (change word and replace it with "first"). Move the cursor to another word and press . to repeat the change. Yank inner text (text containing cursor which is in quotes).

How do you replace a word in Vim?

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.


2 Answers

I found a much better solution

R Enter Replace mode: Each character you type replaces an existing character, starting with the character under the cursor.

So R <ctrl-r>" will do what you want. Note there is a space before and after <ctrl-r>".

OLD ANSWER

You can do this with a macro pretty easily

qhmhA <esc>a<><esc>40.80|D`hq

qh start macro
mh set mark
A <esc> insert a space after the existing text
a<><esc> insert '<>'
40. repeat last command 40 times
80| move to column 80
D delete to the end of the line
`h jump back to mark
q end macro

The macro can then be repeated with @h. You probably want to save this to your .vimrc like so

let @h = 'mhA ^[a<>^[40.80|D`h'

Note that the ^[ are supposed to be one character entered by pressing <ctrl-V><esc>

like image 154
Brett Y Avatar answered Sep 18 '22 16:09

Brett Y


You can use visual selection.

  • Vp select the whole line and overwrite with buffer
  • viwp select the word under cursor and overwrite with buffer content.
  • vi(p overwrite what is in between parenthesis.

I particularly like that it highlights what should be overwritten, which makes it easier to verify.

like image 31
0-_-0 Avatar answered Sep 21 '22 16:09

0-_-0