1

I would like to know if it is possible for me to delete some lines/characters of a file in Vim, do some more work in that file, and then restore those lines/characters I originally deleted. I am asking this because I would like to temporarily remove some braces that are commented out in a C++ file. This is so that when locating the beginning and ending braces of a function/condition, Vim doesn't send me to one of the commented out braces. I know there are Vim plugins/add-ons that can fix this issue, but installing them is out of the question. And I should say that I definitely need to put back the braces.

So this is what I want to do. I have some code here that is only meant as an example and doesn't mean much in terms of functionality:

int mod(int a, int b) {
  // if (true) {
  //   return a%b;
  // }

  return a%b;
}

So I want to first delete the commented out braces, and somehow save it:

int mod(int a, int b) {
  // if (true) 
  //   return a%b;
  // 

  return a%b;
}

And then I'll make some modifications, maybe like this:

int mod(int a, int b) {
  // if (true) 
  //   return a%b;
  // 
  if (b < 1) return -1;
  else return a%b;
}

And then finally, I want to be able to restore those braces that I deleted before, using some combination of Vim commands:

int mod(int a, int b) {
  // if (true) {
  //   return a%b;
  // }
  if (b < 1) return -1;
  else return a%b;
}

Is this possible? I know worse comes to worst there are other more complicated ways of putting back those braces, but I am particularly curious if Vim has some way to do this. I should also mention that I am familiar with Vim, but I am by no means an expert.

JohnnyD27
  • 83
  • 1
  • 7
  • [Cut](http://vimdoc.sourceforge.net/htmldoc/change.html#d) into a [named regsiter](http://vimdoc.sourceforge.net/htmldoc/change.html#registers). [Paste](http://vimdoc.sourceforge.net/htmldoc/change.html#p) from the named register later. – phd Oct 27 '20 at 20:52
  • I see, but if I understand correctly, it looks like I would need 1 register per cut line, which I guess is manageable if there aren't many lines that need to be cut. But even with that, I still won't be able to save the line number with the cut. Or can I? My example is a small function, but the functions I'm working with could have 1000+ lines of code in them. – JohnnyD27 Oct 27 '20 at 20:59
  • You can cut many lines at once into one register. – phd Oct 27 '20 at 21:09
  • I understand, but then how do I determine where they go when I want to paste them back in different locations? – JohnnyD27 Oct 27 '20 at 21:17
  • Paste with `P` put text above the cursor line; paste with `p` put text below the cursor line. So before pasting move cursor to the desired place. – phd Oct 27 '20 at 21:29
  • Yes, but the issue is that I need to know the association between the cut lines and the line numbers. So for instance let's say I cut lines 5, 20, and 500 into register 'a', 'b', and 'c', respectively. Then I do some work, and now I want to put the lines back. But how will I know to paste line from register 'a' back to line 5, line from register 'b' to 20, etc... ? – JohnnyD27 Oct 27 '20 at 21:35
  • That part hardly could be automated, sorry. Especially if the line numbers are changed due to your edits. – phd Oct 28 '20 at 00:47
  • I believe `matchit` comes with vim and on your simplified examples, using `%` on any pair works just fine. Do you have an example where it doesn't work? – Lieven Keersmaekers Oct 28 '20 at 07:14
  • 1
    If you cut lines 5, 20, and 500 into "a, "b, and "c respectively, then you can `put` them in reverse order. Instead of starting with "a and messing up your line numbering, start with "c at 500, then "b at 20, etc. As to telling each chunk where to go, you can use macros to get include the line number when you cut, then erase the line number when you paste. https://stackoverflow.com/questions/14993012/getting-the-current-row-number https://stackoverflow.com/questions/2024443/saving-vim-macros Also, consider Vim's bookmarks: https://vimhelp.org/usr_03.txt.html#bookmark – m_mlvx Oct 28 '20 at 07:24
  • 1
    well for this usecase I usually do the changes, then go back in the version `:earlier` copy the old lines, go back to the current version and paste. But this mostly happens unplaned – Doktor OSwaldo Oct 28 '20 at 14:21

0 Answers0