Skip to main content
added 7 characters in body; edited title
Source Link
statox
  • 51.1k
  • 19
  • 158
  • 237

Copy a line, comment it and replace worlda word in copied line

Libraries evolve all the time and I often face the situation where after an update some function namesname have changed and they return errors in the updated version of the library.

It's easy to replace all the instances of a word by another however I would like to keep track of the old version by commenting the line containing the old function name, as follows:

original line (which raises an error)

a line with old code

a line with old code 

after:

// a line with old code a line with new code

// a line with old code a line with new code 

So far I did it manually: yy + p to copy the line, :%s/\<old code\>/new code/gc to replace with confirmation, then in the line I want to comment, esc + I + // + esc and I use the repeat command . to add my comment to the other appropriate lines. This is fine for few dozen lines of code but not for hundreds of lines, so I'd like to make this less manual.

The steps to be taken would be:

  • search lines with the old code (old function name)
  • copy the lines
  • comment the original lines: add // (or other) in front of the line
  • replace the old code in the uncommented line

Can this be done in a "vim-apprentice" friendly manner? More advanced methods will be welcome too.

Copy a line, comment it and replace world in copied line

Libraries evolve all the time and I often face the situation where after an update some function names have changed and they return errors in the updated version of the library.

It's easy to replace all the instances of a word by another however I would like to keep track of the old version by commenting the line containing the old function name, as follows:

original line (which raises an error)

a line with old code

after:

// a line with old code a line with new code

So far I did it manually: yy + p to copy the line, :%s/\<old code\>/new code/gc to replace with confirmation, then in the line I want to comment, esc + I + // + esc and I use the repeat command . to add my comment to the other appropriate lines. This is fine for few dozen lines of code but not for hundreds of lines, so I'd like to make this less manual.

The steps to be taken would be:

  • search lines with the old code (old function name)
  • copy the lines
  • comment the original lines: add // (or other) in front of the line
  • replace the old code in the uncommented line

Can this be done in a "vim-apprentice" friendly manner? More advanced methods will be welcome too.

Copy a line, comment it and replace a word in copied line

Libraries evolve all the time and I often face the situation where after an update some function name have changed and they return errors in the updated version of the library.

It's easy to replace all the instances of a word by another however I would like to keep track of the old version by commenting the line containing the old function name, as follows:

original line (which raises an error)

a line with old code 

after:

// a line with old code a line with new code 

So far I did it manually: yy + p to copy the line, :%s/\<old code\>/new code/gc to replace with confirmation, then in the line I want to comment, esc + I + // + esc and I use the repeat command . to add my comment to the other appropriate lines. This is fine for few dozen lines of code but not for hundreds of lines, so I'd like to make this less manual.

The steps to be taken would be:

  • search lines with the old code (old function name)
  • copy the lines
  • comment the original lines: add // (or other) in front of the line
  • replace the old code in the uncommented line

Can this be done in a "vim-apprentice" friendly manner? More advanced methods will be welcome too.

Source Link
calocedrus
  • 417
  • 1
  • 4
  • 6

Copy a line, comment it and replace world in copied line

Libraries evolve all the time and I often face the situation where after an update some function names have changed and they return errors in the updated version of the library.

It's easy to replace all the instances of a word by another however I would like to keep track of the old version by commenting the line containing the old function name, as follows:

original line (which raises an error)

a line with old code

after:

// a line with old code a line with new code

So far I did it manually: yy + p to copy the line, :%s/\<old code\>/new code/gc to replace with confirmation, then in the line I want to comment, esc + I + // + esc and I use the repeat command . to add my comment to the other appropriate lines. This is fine for few dozen lines of code but not for hundreds of lines, so I'd like to make this less manual.

The steps to be taken would be:

  • search lines with the old code (old function name)
  • copy the lines
  • comment the original lines: add // (or other) in front of the line
  • replace the old code in the uncommented line

Can this be done in a "vim-apprentice" friendly manner? More advanced methods will be welcome too.