I typically write all my software in the same way (omitting the TDD parts):

  1. Iterate pseudo code until it is granular enough that each part can be written in, hopefully, under 20 minutes.
  2. Implement each "pseudo"-component

Contrived example:

# Get file
# Open file
# Verify integrity
# Parse
# Close file

To encourage modularity i typically don't write these functions in "one" block. I write them in separate files or at least separate blocks and then "paste" them in. This is to ensure I'm not encouraged to add unnecessary dependencies. Right now, I'm literally copy->move to pseudocode-line->Paste over it with block of code, a function or method name, or whatever copy represents. This works very well for me and the code turns out nice. However, being lazy, I don't want to have to touch the mouse or do too much if there's a way to do it with less effort. I'm looking for a way to select->copy->replace a pseudocode-line with the selected:

In file Parse.c:

int ParseMyFile(char* fileName)
  # Get file
  return 0;

I would then write # Get file in another file or buffer:


As a next step I would love to just mark this block of code, and replace # Get file with the block to produce:

int ParseMyFile(char* fileName)
  return 0;

I tried experimenting with marks to copy&paste "onto" a mark, but it did not work well enough. I copied->move to mark-> replace line. I would love to get this down to copy->replace. I can not download extensions. I only have access to standard Vim. It must be less effort than physically marking the block, navigate to the correct pseudo-line, and paste it. It may not sound like a lot of work, but I write even very small blocks of code like this, as little as 2-3 lines if it helps me reason about the code. It adds up.

  • Welcome to Vi and Vim! Did you look into whether the :r (:read) command does what you want? It can read an external file into the current cursor position... – filbranden Dec 15 '20 at 0:07
  • 1
    Can't you just mark the line you want to replace with visual line mode (shift+v) and then past whatever you have copied with p? – Doktor OSwaldo Dec 15 '20 at 6:53
  • Hello filbranden, Yes I did. Ideally I don't want to have my cursor be moved at all. I want the paste and replace to be done automatically by me providing a symbolic label and a paste command. No cursor involved. – gwow12345 Dec 15 '20 at 7:42
  • 1
    If you were able to install plugins, I'd suggest checking out Christian Brabandt's excellent NrrwRgn. – Rich Dec 15 '20 at 20:04
  • When I wrote my answer I was interpreting “I only have access to standard Vim”, as you not being able to set up a vimrc, but that seems implausible now I think about it. For maximum efficiency, I think you’re going to want to set up a function you can map to. Are you interested in this sort of solution, or were you hoping only to use standard Vim commands? – Rich Dec 15 '20 at 21:11

The ex commands :put and :delete both allow you to specify a line, so if you've already placed mark 'a on the line you want replaced, then you can execute the following after the copy:


or as a one liner:


If you haven't placed a mark, you could specify the line using a search instead:

:/# Get here/pu

Or, again, as a one liner:

:/# Get here/pu|//d

I'm not convinced this is actually any more efficient than Doktor OSwaldo's suggestion—it's certainly more keystrokes—but perhaps it aligns better with your mental model.

  • Cleverer, doing the delete after the put :) – D. Ben Knoble Dec 15 '20 at 19:51
  • Hey. This agrees very much with my model. Huge points for the simplicity, it means I can work on multiple environments without having to setup macros. It uses commands I am already comfortable with. After using it, I tried failed get my cursor back to the copy source path, because this does indeed move my cursor. Typically I would use `` or '', which if I understand operate on edit points. I assumed it would work here as well, but i guess the put before delete marks an edit point as well. I can overcome that with another mark unless you have a better idea? (I use marks a lot... :)) – gwow12345 Dec 16 '20 at 7:32
  • @gwow12345 Jumping back twice should work: <C-O><C-O> – Rich Dec 16 '20 at 10:48
  • @gwow12345 Or if you did your copy using visual mode, you could jump back with `< – Rich Dec 16 '20 at 10:55

Alternative solution:

If you want to replace the text at the mark, you could just use


Or in one line with the copy:

:'<,'>y | norm!'aVp``
  • 1
    Nice! Tack a jump on the end and it doesn’t move the cursor either: :norm!'aVp`` – Rich Dec 16 '20 at 10:34
  • thx @Rich, updated my answer! – Doktor OSwaldo Dec 16 '20 at 10:39

One approach:

Go to the # Get file line, delete it (e.g., dd), and then :read your file starting from the previous line:

:-read otherfile

This only works if you save the file though.

However, you can write it as one sequence of commands:

:/# Get file/delete | -read otherfile

If you have the contents in a register from a previous yank, you can use (e.g.)

:put! a

Instead of -read otherfile. Substitute a for the register you yanked to (or nothing for the default register).

  • 1
    Heh, I don’t always shown the colons on code-blocks for ex commands, but the first one looks a little like prolog :) – D. Ben Knoble Dec 14 '20 at 22:34
  • Hello. This is probably more work than I am currently doing. Right now I copy, move to previously set mark by command, paste over line. I would like to completely eliminate the second step involving cursor position and have the line replaced simply by providing a label of some sort. Possibly a mark, but something else if it exists. – gwow12345 Dec 15 '20 at 7:45
  • 1
    Well, if you mark a line (say, ma) you can do :'a delete | -read otherfile – D. Ben Knoble Dec 15 '20 at 12:41
  • Hello. I wish I could mark two answers, they (this and the marked) solve slightly different problems but both address my question. – gwow12345 Dec 16 '20 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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