I'm editing code in a server using Vim.
I pressed J instead of j many times, because CapsLock was enabled.
How can I revert the changes made by Js? Pressing u says it is the oldest change.
See the undolevel settings in your session. Type below command to see current undolevels setting.
:set undolevels ?
You can change the setting either in the session or in .vimrc
:set undolevels=1001 # In vi/vim session
set undolevels=1001 # In .vimrc
In vi/vim, you can keep pressing u to undo the changes, based on the undolevels.
In vi/vim, if you want to undo all the changes, since you had last saved, you can do below things.
You can go to command mode by pressing Esc and then give below command
:e! or :edit!
It will clear all the changes in buffer. The file will return to last saved version.
Related
I can press ctrlO to go to the last jump point. However, if there are any changes in the current document it warns me with:
E37: No write since last change (add ! to override)
Is there a way to ignore that message and do ctrl-o and either (1) discard any changes; or (2) save any changes and do ctrl-o?
Discard any changes --- No.
Save any changes --- set autowrite or set autowriteall
Hide buffer but keep it in memory (can ask for saving it later) --- set hidden
Ask if you want to save a buffer --- set confirm
Note: (2), (3) and (4) are global options and change Vim behaviour for many(!) commands.
2) Here is a simple command for your .vimrc that will save the file before jumping:
noremap <C-O> :update<C-M><C-O>
EDIT: Use :update instead of :w to avoid re-writing the file when it was not previously modified.
1) I looked for the equivalent solution to discard the changes instead of saving them, using :e! instead of :w, but that modifies the list of jump points in a way that break <C-O> completely.
I am using vim editor. I was writing a program and did some of the changes in the file after which I "undo" all the changes and by mistake closed the file. Later I realized that the approach that I was doing in the code is right, So I again opened the file and tried to do "redo" operation,but as I have closed the session I think I have lost all the history. Can somebody please tell me if I can do redo by some means as retyping the whole thing again is quite tedious.
Is there some way in which I can redo changes after opening a new session of Vim file
For that you need to have :help persistent-undo enabled:
When unloading a buffer Vim normally destroys the tree of undos created for
that buffer. By setting the 'undofile' option, Vim will automatically save
your undo history when you write a file and restore undo history when you edit
the file again.
Unfortunately, 'undofile' is off by default, so unless you've configured it (and if you've tried redo and failed, that looks like it's off), it's of no use for your current problem.
If the swap-file is still lying around (unlikely as there was a clean exit of Vim), you might be able to grab small incoherent bits of your edits from it.
soap box
To preempt the typical comments: You probably have to accept the loss right now, but take this as an opportunity to rethink your approach. Persistent undo is a really nice feature. With a modern revision control systems (like Git or Mercurial), you can commit often and only locally, and revising your edits is easy to do. Even if that's not an option, there are plugins for Vim (like my writebackup plugin) that make it very easy to frequently save "snapshots" of important editing states. Any of these could save you from the data loss next time!
Another approach, in case you have executed some commands, is recovering your command history by redirecting it into a register.
:redir #h
:history
:redir END
:set paste
"hp
:redir #h ............. start redirecting output to register "h"
:history .............. outputs all commands history
:redir END ............ stops redirecting
:set paste ............ to avoid wrong indentation
"hp ................... puts the "h" register in the current position
you can control where to put it by doing...
0put h .............. pastes the `h` register at the line zero
Once you have a series of commands into a file or register it makes easier to build a function like:
function! Helo()
echo "hello world"
endfunction
If by any change you have used a macro, let's say q, you can retrive it or even edite it, because macros are regular registers that you can reassign as you want. For example, on insert mode you can type:
Ctrl-r q .................... pastes register q
:let #q= ................... starts reassingning macro q
:let #q= ctrl-r q .......... pastes the q register on its reassignment
setting register q to "hello vim"
:let #q = "ihello\<Return>vim\<Esc>"
OBS: Using double quotes you can use control keys as seen above
Any complex command can be saved into the clipboard
:let #+ = #: .............. places last command on the clipboard
:#+ ......................... uses clipboard as command
Since I updated to Vim 8 pressing j or L on the last visible row makes the screen scroll down. I can't have the cursor be on the last row.
I did not have this behaviour with Vim 7.4, so it looks like something changes?
You're experiencing the effects of
set scrolloff=5
Vim 8.0 introduced more sensible (compared to vi compatibility) defaults when the user does not have his own ~/.vimrc file.
This is explained at :help defaults.vim. If you want your old behavior back, just create an (empty) ~/.vimrc file. If you like the new defaults (most make a lot of sense), put the following into the file:
unlet! skip_defaults_vim
source $VIMRUNTIME/defaults.vim
" Undo the new scrolloff setting, I don't like it.
set scrolloff=0
Suppose state A in my document, I make change to B, C then D.
Now I typed 'u', the state goes to C.
I type 'u' again it goes back to D. (the second undo looks like redo to me).
In my understanding, undo means that I undo it once it will return to C, then undo again to it will return to B then undo again it will return to A.
Also, I know 'u3' can work here but in most cases I have no way to keep track of the number of state changes.
How can I achieve unlimited undo in Vim?
You have only 1 level of undo if you are in Vi compatible mode. You are missing out on a number of features by being in 'compatible' mode. Just create a ~/.vimrc file and you will automatically disable 'compatible' mode.
What wonderful undo features do you get by using 'nocompatible'?
Unlimited undo
Persistent undo
Undo branches (like an undo tree)
I like the old vi undo/redo commands which allows undo (u) or redo (CTRL-R) to be pressed multiple times rather than browsing history to figure out the number of changes to undo.
The vi command uses the ~/.exrc file, not ~/.vimrc so just add "set nocompatible" to the file.
Note that I like using a single undo/redo to jump back to the last place in a file that I changed, and "u" followed by CTRL-R does the trick. And I can always hold down the "u" until all changes are undone including those I might have already saved.
I want to save a list of open files (:ls), quit gvim, start gvim, reopen all previously opened files. I don't want to use :mksession because it doesn't work correctly (probably due to some of the plugins I'm using)
Maybe the bug/conflict with :mksession is only if you have parts of :mksession enabled that you don't care about.
Try this:
:set sessionoptions=buffers
:mksession
Another answer suggests session manager plugins, which are great. But I have a very lightweight alternative that I'll leave here for posterity. (Since it has been quite a while since this question was active.)
The code below automatically saves the current session on shutdown, and sets a key command to reload it manually (handy for when I really do want a new session with new files). If you really do want to unconditionally restore the previous session whenever vim is started up again, uncomment the 'VimEnter' line.
As a bonus, this also enables manually saving and restoring a separate session at will with a keypress.
Somewhere in ~/.vim/vimrc
" Automatically save the current session whenever vim is closed
autocmd VimLeave * mksession! ~/.vim/shutdown_session.vim
" <F7> restores that 'shutdown session'
noremap <F7> :source ~/.vim/shutdown_session.vim<CR>
" If you really want to, this next line should restore the shutdown session
" automatically, whenever you start vim. (Commented out for now, in case
" somebody just copy/pastes this whole block)
"
" autocmd VimEnter source ~/.vim/shutdown_session.vim<CR>
" manually save a session with <F5>
noremap <F5> :mksession! ~/.vim/manual_session.vim<cr>
" recall the manually saved session with <F6>
noremap <F6> :source ~/.vim/manual_session.vim<cr>
The user can define for herself what is in a session with the 'sessionoptions' option, mentioned above: help sessionoptions
There are a couple plugins that take vim's session management a little bit further.
SessionMan and Autosess provide some commands and auto saving features that you might like.
Another one is the following: http://jaredforsyth.com/blog/2010/apr/9/vim-crash-recovery/
Very short, probably a vimrc kind of thing.
I've found setting viminfo in your .vimrc can also save the last known buffer list.
Here's what I have.
"Set viminfo to save info upon exit
"help usr_21.txt
"'5 : marks will be remembered up to 10 previous files
""50 : will save up to 100 lines for each register
":20 : up to 20 lines of command-line history will be remembered
"% : saves and restores the buffer list
set viminfo='5,f1,\"50,:20,%,n~/.vim/viminfo