Let say I have messed up my vim & tmux configuration. Is it possible to delete or reset them without losing any configuration changes such as PATH etc? If so, then how?
At this point? There probably is no way to get those files back. But, as a preventative measure in the future, keep config files like those in some sort of version-control system (like git or mercurial).
However, if this is too tedious or you don't want to for whatever reason, you can also add the following lines to your .vimrc:
set undofile
set undodir=~/.vim/undodir
Don't forget to mkdir ~/.vim/undodir.
This configuration enables persistent undo history in vim (I assume you're using vim to edit these files). With this feature, even after you close the file, and reopen it, you can still undo changes you made in the previous editing session. So if you'd had this set, your problem would be solved simply by hitting u until the files were in a good state. See :help undofile for more info.
Also, check out the great plugin vim-mundo. It provides a visual "undo tree" and makes browsing vim's complicated undo history very easy. Plus it's compatible with neovim.
Related
Some irritating issue when doing git commit (for example, there are other non-git scenarios): what I'd like to type (really fast):
git commit -a --amendEnter:wqEntergit push -fEnter
My problem is that after :wqEnter I have to wait until vim exits, and as I have a big vimrc it takes some time. I know, I'm spoiled.
I'm pretty sure there is a flag to git commit that will not open vim at all, but I'd prefer a more generic vim solution that will make vim not eat my input.
My vimrc is here
Git
While the OP wants a vim-centric solution, I offer these git workarounds as
temporary band-aids (and useful information).
At the bottom, I provide resources for discovering the issue in vim.
no-edit flag
Amending with the --no-edit flag causes vim not to launch (which is a good
idea if you do not need to edit the commit message: this is what the flag is
designed for).
Use a "different" editor for commit messages
There are a couple of different configuration options here, and a couple of
different "vims" to try.
See also git(1), section ENVIRONMENT VARIABLES, variable GIT_EDITOR.
One-off configuration
This is good for occasional usage (you can alias it, of course).
Use the -c flag on git:
git -c core.editor='vim variant' commit...
You could also set GIT_EDITOR via your shell (e.g., env, export, or
bash-isms).
Permanent configuration
This is good if you cannot resolve the vim issue and need it permanently.
Edit your configuration file (e.g., git config --edit --global), and add
[core]
editor = vim variant
Vim variants
Vim's command line switches let us control various aspects of its behavior. You
might want to
disable plugins (--noplugin)
disable a vimrc (-u NORC)
disable both (-u NONE)
use a 'clean' vim (--clean)
use a completely different vimrc for git (-u DEFAULTS or -u ~/.mygitvimrc)
The vimrc option allows us to craft an extremely efficient and minimal vimrc for
git work, while keeping our original for full-time stuff.
Experimentation may show that -N (not compatible mode) is necessary with some
of these.
Once you've chosen a setup that works, simply use vim <args>... as your git
editor, in one of the spots above.
Vim
The first things to try on the vim side are binary-split debugging your
vimrc and profiling your
vimrc to
find out what the root cause is.
It helps to be comfortable debugging for these steps. If you need to debug
vimscript, see :help debug-scripts.
Once you've identified the root cause, the next step is to squash it. You may
need to
disable a plugin (possibly to be loaded on demand)
change an autocommand to not be so nasty (look especially for autocommands
that are accidentally triggered more than once such as those not in
augroup)
avoid expensive function calls in your vimrc
help convert a heavy plugin to autoloads
or a myriad of other performance-enhancing techniques.
As always, the key is to know where the problem lies first.
For OP, using pathogen and vundle together seems like a mistake (two different runtimepath/plugin managers?). Also, vundle provides vundle#begin as a possible time-saver over vundle#rc.
So my question is if I am writing some code in vim & then want to go back 20 minutes earlier, then I type :earlier 20m in vim. But this doesn't work if I exit the vim once or even reboot by system. I understand that it is stored into the temporary registers and once vim restarts, it cleans the whole register buffers. But is there any way I can save the last changes & apply some undo mechanisms. Actually I work on some big project files & if something wrong occurs, I can not go back.
In Vim 7.3 or later, you can use an undo file.
Put this in your .vimrc.
set undofile
By default this will save 100 actions to undo. If you want more, you can set it manually by adding this to your .vimrc, with whatever number you want:
set undolevels=100
Persistent undo will create undo files in the same directories as the files you actually work on. If you want to put these in a separate directory so they don't clutter your filesystem, add this to your .vimrc:
set undodir=~/.vim/undo
Then, you need to actually make the directory. From the command line:
mkdir ~/.vim/undo
I would look into using version control instead of relying on vim's persistent undo to track changes in your projects.
Im currently making a lot of changes in my .vimrc. At the same time Im also coding away on a project. To fix the new indentation and tabstop rules, and so on, I have to go through every line and re-indent them. This seems a little bit tedious.
Is there any way to apply the newest changes in my .vimrc to the whole source file?
Your problem seems to be that existing buffers do not get the updated (global) values of e.g. 'tabstop', because these are buffer-local options. Therefore, a simple :so % after editing the ~/.vimrc isn't sufficient.
There are several possible approaches:
Use sessions
With the current Vim state persisted via :mksession, you can safely :quit Vim, restart it, :so session.vim, and have the identical editing state, but with the new settings applied (this requires that you :set sessionoptions-=options)
Manually apply
You can e.g. :yank the changed :set tabstop=... commands that you've edited and reapply them to all existing buffers, e.g. by moving there and :#", or (if there are many) with the help of :bufdo.
Note that especially the indent settings can also be set from ftplugins, so your global settings may not be effective. You can check that for a buffer with :verbose set tabstop?.
I have enabled undodir, to keep track of changes I make even after I close my vim session.
I sometimes want to edit a file, make some changes, save it, then return to the original file of that session. (This might be one of the possible use cases)
If I had undodir disabled, I could simply keep hitting u until it showed me a message.
So I want undodir to be disabled in undo/redo by default, and I should have a command to enable it when needed.
All changes to the file must be tracked in either states, at all times.
Is this possible?
As far as I understand the documentation, only setting undodir does nothing unless undofile is set to true.
So, I assume that you want to activate undofile for certain files.
First thing that comes in mind is a modeline in order to set undofile to true for certain files. But unfortunately, this doesn't work.
The issue, however, is present on the vim developer mailing list and there was a fix provided in January: http://comments.gmane.org/gmane.editors.vim.devel/32896. This fix could be present in current sources; so if you'd like to try, grab the latest vim sources, build it and check if you could use a modeline for setting udf
Until there's an official version containing that fix, you could get around your issue using undofile.vim. Excerpt of it's description:
If you want 'undofile' only for certain files, you will notice that 'undofile' cannot be set in a modeline, or once the buffer is loaded (because an existing undo file will not be loaded then). Bram suggests to use a BufReadPre autocmd which sets 'undofile' before the buffer is loaded. This script does the steps for you.
On one of my dev machines I have unlimited undos, I can hold u and go all the way back to when I opened the file.
On another, pressing u toggles between the last two changes I made, no more.
The first dev machine I have Administrator access to and the vim installer had free reign. The second I do not and vim had to be installed off the normal OS paths and have to launch vim with a 'Sendto'. Perhaps this is related and I'm missing some rc commands.
Also noticed I have to run 'syn on' to get highlighting on that box. vimrc was also blank so now I'm sure it has something to do with it.
From other threads I don't believe this is related to the persistent undo feature, but simply a .swp or ~ issue (whatever those files are used for..)
Deadlines have prompted punting what is probably a simple issue.. How do I fix this?
vimrc was blank on the affected box.
I added these lines from my other dev box and everything was hapy again.
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
Looks like you need a vimrc file on the second box: see :he compatible-default.
If there is no vimrc file, vim runs in vi compatible mode, and there is no syntax highlighting etc...
Create one. Output of :version will show you more where the files are expected.
If undo still does not work have a look at :he undo-two-ways.