Vim command to reset to the oldest change? - vim

I am a Vim user and sometimes I made several changes in a file but then want to get back to the beginning and have to type :u a lot of times, so I ask you if there is a shortcut to get to the "oldest change"?
Cheers!
Rodrigo Alves Vieira
http://rodrigo3n.com | #rodrigo3n

The keystroke to undo is just u; if you're pressing :u then that's too many keys (although it works, it's four keys instead of one). Usually I would just hold down u and let auto-repeat do the trick (note that Ctrl+R is the opposite of u if you go too far)..
You can reload from the last saved version on disk using :e!.

Does ':e!' (followed by return) work for you? It gets back to the state of the edited file when it was last saved, or when you started if you've not saved yet...

You also have the :earlier command. :help :ea

Using :e! will get you back to the oldest change as saved. Aside from typing just u instead of :u, also see :h undo-redo for help on all the undo commands.
Other options might be use a vim plugin, such as undo_tags.

You know you can use repeat counts on just about any VIM command, right?
My favorite way:
10000u
ten-thousand-undos.
Zips me back in time, usually to where I opened the file. I can see how things USED to be.
then
10000^R
then-thousand-redos
And I'm back at the end again.
You're welcome.

Related

How to back to editing file after :Ex

I'm editing a new file in NeoVim and I don't remember the path of the directory I want to save my file to, so I want to look at the directory tree before saving. I do this by typing :Ex, which takes me to Netrw.
I'm done looking at the directory tree and I want to go back to my file and and finish my work. However I don't know the command to get back from Netrw to the file, and none of the numerous solutions I looked up online worked. The help file didn't help either. The intuitive command would be :q, which works fine for help but doesn't work for this situation. I have also tried q, Q, gq, gQ, :visual and :vi as suggested by people online.
Right, after making this post I finally stumbled upon a working solution in a comment by another.anon.coward on How to go back when I run :Ex command in Vim
You could try :bunload to unload current buffer & go back to previous
I think you can just press Ctrl-6 to go back to the previous buffer. Also check the nvim help: :help CTRL-6.
You can use those default vim mappings for that:
CTRL-O - Go to older cursor position in jump list
CTRL-I - Go to newer cursor position in jump list
So when you'll be at netrw press <C-o> couple of times and it will bring you back to the place where you started.
To read more about jump commands: :help jump-motions

How do I cancel a paste operation in Vim?

I had rather a lot of text on my clipboard whenever I accidentally right clicked inside Putty (with Vim open), and Vim has initiated a paste operation which has been going for around ten minutes now.
I don't want to lose my unsaved work, is there a way to instruct Vim to stop pasting text?
If you're in normal mode, Ctrl-C aborts the current command in progress. Then press u to undo anything that changed before you stopped it.
Depending os your vim configuration, there's chances that you have a swap file (backup) in .nameOfTheOpenedFile.swp (substitute nameOfTheOpenedFile with the name for your file).
To recover the file :
vim -r .nameOfTheOpenedFile.swp
I know this is really old but the top answer is not right and I was clearly having a similar issue to OP. (accidentally pasted like a million lines of json into vim)
Keep in mind this may not allow you to save your work (but you can probably salvage something from the .swp file)
All you need to do is open a new terminal window and enter pkill vim into the command line.

Go to back directory browsing after opening file in vim

When I open a directory browsing in vim, I can open a file by moving the cursor to a file name and pressing Enter.
Now, is there any command to go back to the previous directory browsing?
If not, is there a way to open a file without closing the directory browsing mode?
You can go back to the last buffer using :b#.
If you just opened a file, then it will bring you just back to the directory browsing.
Update:
Since this answer happened to be accept as the correct answer and is thus on the top, I'd like to summarize a bit the answers, including the one by #romainl that imho is the correct one.
:Rex[plore]: Return to Explorer (by #romainl) vimdoc.sourceforge
:Explorer: opens the Explorer, same as :E (if no other command starting with E is defined (see stackoverflow), or as :Ex (see vim.wikia) (by #drug_user841417).
:b#: goes back to the "previously edited buffers". See vim.wikia
:e# or Ctrl-6 (or Ctrl-^): goes back to the "previously edited file" (by #aman-jain). See vim.wikia
Ctrl-O: jump back to the previous (older) location, not necessarily a buffer (by #Peyman). See vim.wikia
You can use Ctrl-O to go back to the previous buffer.
The proper command for returning to the netrw listing is :Rex[plore]. You can run it even after you have jumped to and from dozens of files.
:Ex will take you back (and show you what you edited and didn't save).
I rely on Ctrl ^ to go back and forth b/w two buffers.
I usually just use :E. It's easy to remember and gets you back to the directory listing of the file you had open.
I'd like to use the Jump-motions shortcut keys to do any 'go back' or 'go forward' actions in Vim. It makes vim like a web browser.
You can search for Vim's help for more details.
Vim :h jump-motions
So like as mentioned in other answers ctrl+O works cool, and hope vim's help-file let you learn more commands like ctrl+I .
Ctrl + I : Go Forward
Ctrl + O : Go Back

Vim (terminal) - copy to x clipboard and paste while suspeneded

I have vimx installed, so I can copy in vimx to the x clipboard by using "+y and the like, which works well as long as I can keep the current vimx running.
However, I also love to be able to switch to the current running shell with ctrl-z and be able to paste what I copied from vim into the shell. Does anyone know how to do this, because as soon as I suspend vim with ctr-z the x-clipboard becomes empty, until I put vim into the fg again.
ok,
After about an hour of head bashing this issue, I have decided to use a different approach to do the same thing.
I will detail what I did here for the benefit of those with the same issue.
If you have wxcopy on your system that is enough to get copy/paste support on your system between vim and other apps. If you want to copy from vim, select the text using v or V or whatever method you prefer, then use the command :tee >(wxcopy). This will copy that code into the oldschool X11 cut buffer. This buffer has the added bonus of not requiring the parent application to be running for pasting to work, hence my problem goes away.
I realize it may be hard to get your hands on wxcopy, under fedora the package that provides it is:
WindowMaker
This isn't an answer as much as a push in the right direction, but start here:
:help quoteplus
:help x11-cut-buffer
Specifically the second help text has some interesting notes about suspending vim. It claims to write the buffer into PRIMARY and CUT_BUFFER0 so you can still use middle click after you've suspended. Maybe you've deleted those or a plugin is interfering? Maybe you are running an old version of vim (I'm running 7.2.182).
My suggestion is to disable all plugins you've manually installed and try vanilla vim. If that doesn't work, consider using vim's -V (verbose) flag and see if it will try to put the buffer into PRIMARY or CUT_BUFFER0 before it actually gets suspended.
I don't have vim compiled with X (vimx) so I can't actually test this myself. Hopefully that gets you in the right direction.

Vim: Maintain the history of a file after it has been changed and reloaded

If I'm editing a file in Vim, then some external program changes the file, Vim tells me:
W11: Warning: File "test.erl" has changed since editing started
See ":help W11" for more info.
[O]K, (L)oad File:
If I Load the file, though, all the undo history is lost.
Is there any way to avoid that?
Update: it appears that this is one of the new features in Vim 7.3: https://groups.google.com/group/vim_announce/browse_thread/thread/66c02efd1523554b
I don't believe this is possible.
There is a very useful patch available for the vim source code available here that keeps the undo history after exiting vim and restarting. However, I've just tried it and it seems to fail when the file is edited externally. It might be worth contacting the author or reporting a bug on the patch website to see if this can be overcome.
G'day,
I'm not sure but does setting autoread, i.e. entering :set autoread leave the undo history for the file when it changes?
Hmmmm. I'm thinking probably not because the change history is maintained as line numbers and vim doesn't know if those line numbers are still relevant to the changed file.
BTW WTF are you editing a file that is being changed by external forces anyway? Sounds dangerous to me. (-:
This is a workaround I used before Vim 7.3:
" :e usually clears undo history, so we don't really do :e any more.
" Instead we delete the contents of the buffer, then read the file in, which
" is an operation we can undo. We must delete the top (empty) line also.
:map :e<Enter> :%d<Enter>:r<Enter>:0<Enter>dd
When you see the warning prompt, you would have to hit ok instead of load, and then perform the load yourself: :e<Enter>
There are two disadvantages (although I found the tradeoff acceptable):
You lose the line you were on. Your cursor is left sitting at the top of the file.
Vim still thinks the buffer is out of sync with the file, so when you next save, you may need to do :w! instead of the normal :w, and you will need to hit y to confirm the overwrite.
Edit: There might be a workaround for the second problem.
Edit: The first problem could be addressed with a bit more scripting (see :h line.)
I don't see how vim could keep track of something it didn't do.
So, as to the question, I would suggest - source control ... but that's probably not the answer you're looking for.

Resources