Re: undo & replace in multiple files
Hi, On 5/23/07, Michael Henry <[EMAIL PROTECTED]> wrote: Tim Chase wrote: > I think given those conditions (autowrite and nohidden), there seems to > be no such way from my experimenting. However, if you switch to using > hidden instead of nohidden, you can do > > :argdo u > > which will undo the last action in each argument. I've been casually looking for such a feature, but so far I've come up a little short. After a multi-file search-and-replace, I'd like to be able to undo the replace across the files. The `:argdo u` command almost does what I want, but I can't think how to restrict the undo operation to changes made only by the replace operation. For example, suppose I create two files and edit them together: echo "first file" > first.txt echo "second file" > second.txt gvim first.txt second.txt Suppose in first.txt I edit `first` to become `1st` using Vim editing commands: cw1st Now I perform a search-and-replace to change `second` to `2nd`: :argdo %s/second/2nd/ge Now I try to undo my most recent replace operation: :argdo u I'd like this to undo only the change(s) made by the s/// command, but it also changes `1st` back to `first`. Since the `u` is performed indiscriminately in all arguments regardless of whether the s/// command made changes there, I can't blindly use the undo trick to reverse an arbitrary replace operation. I've tried saving all buffers before doing the replace operation, but `:argdo u` undoes past the save (which generally pleases me greatly, but is unfortunate in this case :-)). I searched for "replace undo" in Vim's plugins and tips, but came up empty. Does anyone have a pointer to a plugin or other resource to allow blind undoing of multi-file replace operations? You can try using the gReplace Vim plugin: http://vim.sourceforge.net/scripts/script.php?script_id=1813 Even though this plugin doesn't provide the "undo" operation across multiple buffers, it allows you to interactively change a pattern across multiple buffers/files and you can abandon the changes before the buffers are updated with the change. For example, to replace a pattern across multiple buffers, you can do the following: 1. Execute the ":Gbuffersearch " command. This will open a buffer with all the lines containing in all the open buffers. 2. You can edit this buffer using the usual Vim editing commands. 3. Now you can execute the ":Greplace" command to incorporate all the changes made in the replace buffer back to the corresponding buffers. 4. To abandon making the changes, you can just close the replace buffer. - Yegappan
Re: undo & replace in multiple files
> echo "first file" > first.txt > echo "second file" > second.txt > gvim first.txt second.txt > > Suppose in first.txt I edit `first` to become `1st` using Vim editing > commands: > > cw1st > > Now I perform a search-and-replace to change `second` to `2nd`: > > :argdo %s/second/2nd/ge > > > Now I try to undo my most recent replace operation: > > :argdo u > > I'd like this to undo only the change(s) made by the s/// command, but > it also changes `1st` back to `first`. > > I've tried saving all buffers before doing the replace operation, but > `:argdo u` undoes past the save (which generally pleases me greatly, but > is unfortunate in this case :-)). If saving a snapshot of your buffers in a "good" state (using ":wall") before monkeying with argdo is acceptable (which by your comments, it is), you should be able to revert to the saved copy with something like: :wall :argdo %s/foo/bar/g [whoops! I meant "s/\/bar"!] :argdo e! :argdo %s/\/bar/g where "argdo e!" abandons any changes you've made and reloads the file. -tim
Re: undo & replace in multiple files
Tim Chase wrote: I think given those conditions (autowrite and nohidden), there seems to be no such way from my experimenting. However, if you switch to using hidden instead of nohidden, you can do :argdo u which will undo the last action in each argument. I've been casually looking for such a feature, but so far I've come up a little short. After a multi-file search-and-replace, I'd like to be able to undo the replace across the files. The `:argdo u` command almost does what I want, but I can't think how to restrict the undo operation to changes made only by the replace operation. For example, suppose I create two files and edit them together: echo "first file" > first.txt echo "second file" > second.txt gvim first.txt second.txt Suppose in first.txt I edit `first` to become `1st` using Vim editing commands: cw1st Now I perform a search-and-replace to change `second` to `2nd`: :argdo %s/second/2nd/ge Now I try to undo my most recent replace operation: :argdo u I'd like this to undo only the change(s) made by the s/// command, but it also changes `1st` back to `first`. Since the `u` is performed indiscriminately in all arguments regardless of whether the s/// command made changes there, I can't blindly use the undo trick to reverse an arbitrary replace operation. I've tried saving all buffers before doing the replace operation, but `:argdo u` undoes past the save (which generally pleases me greatly, but is unfortunate in this case :-)). I searched for "replace undo" in Vim's plugins and tips, but came up empty. Does anyone have a pointer to a plugin or other resource to allow blind undoing of multi-file replace operations? Thanks, Michael Henry
Re: undo & replace in multiple files
Let's say I do multi-file replace like 'argdo %s/foo/bar/g', and I 'autowrite' and 'nohidden' options are set. So the argdo will replace and write files, because of 'autowrite' is on. My question is, is there any trick to do 'undo' after that that would undo all changed files in this situation ? I think given those conditions (autowrite and nohidden), there seems to be no such way from my experimenting. However, if you switch to using hidden instead of nohidden, you can do :argdo u which will undo the last action in each argument. I prefer to use hidden and then, if I like the changes that went through, issue a ":wall" to write the changed files. This allows the undo as well as allowing me to proof that all the changes went through as expected. If you set 'autowriteall' in addition to 'hidden', you can get some of the benefits of 'autowrite' but still have the recovery options of 'hidden'. Just a few ideas... -tim
undo & replace in multiple files
Let's say I do multi-file replace like 'argdo %s/foo/bar/g', and I 'autowrite' and 'nohidden' options are set. So the argdo will replace and write files, because of 'autowrite' is on. My question is, is there any trick to do 'undo' after that that would undo all changed files in this situation ? Thanks Yakov