Clear certain criteria from .viminfo file - vim

How can I clear certain criteria from my .viminfo file?
I want to clear the command line history, file marks, jumplist, etc.
However, I want to keep the search string history.
Is there any way to do this?

I can think of 3 ways to do this.
1. Automatically
Run Vim,
Type: :set viminfo='0,:0,<0,#0,f0
'0 means that marks will not be saved
:0 means that command-line history will not be saved
<0 means that registers will not be saved
#0 means that input-line history will not be saved
f0 means that marks will not be saved
no % means that the buffer list will not be saved
no / means that the search history will be saved
These options are correct in Vim 7.2, but might be different in other versions. For more details on the format of the viminfo string, run :h 'viminfo'
Quit Vim. It will save a new version of the .viminfo file, containing only the information you want to keep.
2. Manually
Open the .viminfo file in vim,
:set viminfo= to turn off the auto-saving of info to the .viminfo file. If you don't do this, Vim will overwrite all your changes when you quit,
Remove everything you don't want (perhaps by using Johnsyweb's answer, or just by deleting the lines with manual edit commands), save the file, and quit vim,
3. In a different editor
Edit the .viminfo file in a different text editor and simply delete everything you don't want,

Open the .viminfo file.
The following command will remove all lines that are neither blank, comment nor search history:
:v/^\([#/?]\|$\)/d
#Rich's answer will help you prevent these lines being repopulated.

You can use vim itself to modify the file, and your changes will stay put if you invoke it like this:
$ vim -i NONE ~/.viminfo
Starting vim with the -i option sets which viminfo file to use for that session. The special value NONE means that nothing is saved at all. This can be handy for editing other auto-generating files or sensitive data.
From man vim:
-i {viminfo}
When using the viminfo file is enabled, this option sets the filename
to use, instead of the default ~/.viminfo. This can also be used to
skip the use of the viminfo file, by giving the name NONE.

VIM seems not having a built-in command for this job. But you can do it in a shell with "grep".
For example, the following command will clear the Command Line History,File marks, Jumplist:
bash $ grep -v "^[:'-]" .viminfo > .viminfo_clear
bash $ cp .viminfo_clear .viminfo
If you open the .viminfo, you will find that the command line history is started with ":", the file mark is started with "'", and the jumplist is started with "-".

Copy useful part out and delete .viminfo, then open/close Vim. It will regenerate .viminfo. Then copy useful part back.

Related

Vim 7.4 on Windows 8.1 Creates Program and Files Folders Every Time I Open a File

When I open a file from Windows Explorer using right-click -> Edit with Vim, Vim creates a "Program" folder on the root, and a "Files" folder in the directory of the file I've opened. The Files folder includes Vim/vimfiles/doc. There are no files anywhere, just the directory tree.
This also happens when I run Vim without a file name as an argument.
A DOS Box flashes just before Vim opens, but it goes away too fast for me to read what it's saying.
I've used Vim for years and have never seen this behavior. This is the first time, however, that I've used it on Windows 8.1.
Any suggestions or ideas? I'm getting really tired of deleting Program and Files folders all over the place. Lol!
You could follow the steps on Vim-FAQ 2.5. Some relevant parts follows:
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
problem with my setup or with Vim? / Have I found a bug in Vim?
First, you need to find out, whether the error is in the actual
runtime files or any plugin that is distributed with Vim or whether it
is a simple side effect of any configuration option from your .vimrc
or .gvimrc. So first, start vim like this:
vim -u NONE -U NONE -N -i NONE
this starts Vim in nocompatible mode (-N), without reading your
viminfo file (-i NONE), without reading any configuration file (-u
NONE for not reading .vimrc file and -U NONE for not reading a .gvimrc
file) or even plugin.
If the error does not occur when starting Vim this way, then the
problem is either related to some plugin of yours or some setting in
one of your local setup files. You need to find out, what triggers the
error, you try starting Vim this way:
vim -u NONE -U NONE -N
If the error occurs, the problem is your .viminfo file. Simply delete
the viminfo file then. If the error does not occur, try:
vim -u ~/.vimrc --noplugin -N -i NONE
This will simply use your .vimrc as configuration file, but not load
any plugins. If the error occurs this time, the error is possibly
caused by some configuration option inside your .vimrc file. Depending
on the length of your vimrc file, it can be quite hard to trace the
origin within that file.
The best way is to add :finish command in the middle of your .vimrc.
Then restart again using the same command line. If the error still
occurs, the bug must be caused because of a setting in the first half
of your .vimrc. If it doesn't happen, the problematic setting must be
in the second half of your .vimrc. So move the :finish command to the
middle of that half, of which you know that triggers the error and
move your way along, until you find the problematic option. If your
.vimrc is 350 lines long, you need at a maximum 9 tries to find the
offending line (in practise, this can often be further reduced, since
often lines depend on each other).
If the problem does not occur, when only loading your .vimrc file, the
error must be caused by a plugin or another runtime file (indent
autoload or syntax script). Check the output of the :scriptnames
command to see what files have been loaded and for each one try to
disable each one by one and see which one triggers the bug. Often
files that are loaded by vim, have a simple configuration variable to
disable them, but you need to check inside each file separately.
There is additional information on the link if the steps above doesn't solves the problem.

Command line option to open mac formatted file in Vim

I have a file with mac format, I know I can use :e! ++ff=mac to change to the correct fileformat. What I'd like to know is if there is a command line option I can pass when I open the file to open it directly with the correct fileformat.
You can do this using the command line, try:
$ vim -c "set fileformat=mac"
-c <command> executes before loading the first file. As jammessan has noted, this will only affect the first file that is loaded, subsequent files will require that you change fileformat for each buffer.
If that doesn't work, you can also try:
$ vim -c "e ++ff=mac"
But it's better to set this up in your .vimrc on that machine:
set fileformats=mac,unix,dos
specifies which formats to try when starting a new buffer. This also can influence what fileformat is chosen for existing files.
See help file-formats, :help 'fileformat' and :help 'fileformats' for more details.
The better solution would be to make Vim automatically recognize files in that format. You can do this by updating the 'fileformats' option in your ~/.vimrc
set fileformats=unix,dos,mac
This will make Vim check all three formats when determining which format a file is in.

Vim record history

Vim stores the list of commands that we applied using : for the current execution.
But when I close vim and start it again, the vim command history is lost.
I tried set history = 1000 in the .vimrc file but that did not help.
Where does Vim maintain the local command history?
What is the command to retain command history?
Just an issue that caught me out the other day, which may or may not be your problem:
On some Linux systems (e.g. Ubuntu), if the very first time you run VIM, you run it as a super-user, then the $HOME/.viminfo file gets created with root owner and your local user does not have permissions to write to it. This explained why my VIM was not storing command history when it was all configured up correctly.
Bottom line: on a *nix system, locate your .viminfo file, and make sure you have read/write permissions on it.
To check whether Vim supports the 'viminfo' file (which stores the history), :echo has('viminfo'). The corresponding setting must not be empty: :set viminfo?, and :set history? should be greater than one.
If there's a problem writing the viminfo file (though Vim should complain in that case), you could try passing a different location via vim -i /tmp/viminfo
You should check the permissions of the .viminfo file. You might need to change owner of the file to your current user using chown or sudo chown.
Mr. Baint has given the answer.
Go to $HOME directory.
give ls -l .viminfo to check permissions.
change permission so that group and owner also can have write
permission. use:
sudo chown yourUserId $HOME/.viminfo
It should fix the issue.
You will have to set the viminfo option. Set it in your $MYVIMRC
Update To find out where the option was last set/changed:
:verbose set viminfo?
See http://vimdoc.sourceforge.net/htmldoc/starting.html#viminfo-file
If you exit Vim and later start it again, you would normally lose a lot of
information. The viminfo file can be used to remember that information, which
enables you to continue where you left off.
This is introduced in section |21.3| of the user manual.
The viminfo file is used to store:
The command line history.
The search string history.
The input-line history.
Contents of non-empty registers.
Marks for several files.
File marks, pointing to locations in files.
Last search/substitute pattern (for 'n' and '&').
The buffer list.
Global variables.
The viminfo file is not supported when the |+viminfo| feature has been
disabled at compile time.
You could also use Session files.
I went round in circles on this one a bit on Ubuntu and set viminfo solutions proposed above resulted in errors.
I eventually did the command "version" in the command mode and it came back with "-" for most stuff including:
-cmdline_hist
-cmdline_info
I ran the following command and it all worked fine again:
sudo apt install vim
I had the same problem. The issue was that I had vim-minimal installed (this is a default with Fedora), which does not support history. I had to uninstall it and install the full vim instead. I now have history... and syntaxic coloration !
If you are using a vimrc file, check your folders and files for existence and correctness
set history=200
set undolevels=128
set undodir=~/.vim/undodir/
set undofile
set undolevels=1000
set undoreload=10000
If the specified folder (undodir in my case) does not exist, the history will not be saved.

Vim: How can I create a file after "$ vim file.tgz"?

I want to create a file to tarball, without explicitly opening/extracting it but directly by using Vim. Is that possible?
$ vim file.tgz
:e someNewfile
:w! # how can I create here a file?
Vim handles tar and derived files (including .tgz) using a vimscript called tar.vim. You can see tar.vim's documentation by typing :help tar<CR> inside vim. According to that documentation:
When one edits a *.tar file, this plugin will handle displaying a
contents page. Select a file to edit by moving the cursor atop the
desired file, then hit the <return> key. After editing, one may also
write to the file.
Currently, one may not make a new file in tar archives via the plugin.
So you can edit a file which is already in the tar, but you cannot currently add new files using a vanilla setup of vim.

How to open a file in a list of files in Vim?

I have a longish list of files opened in vim that looks like this:
/dir1/file1
/dir2/file2
/dir2/file3
.....
How can I open all of them one by one the easiest way possible in the same session of vim either with split or edit?
I'd say with -p for tabs
vim -p `cat yourlistoffiles`
You can use quickfix mode, as following
:set errorformat=%f
:cf myfilelist
at this point you can use the normal quickfix shortcuts to go through your files, :cn for the next file, :cp for the previous one and :cr to go to the first again.
EDIT:
oh, if you want to read the list from the current buffer, use :cb instead of :cf in in the instructions above
You can do the following
cat file | xargs vim
Where "file" contains your list of files, this will open the files in the same vim session. As usual when opening multiple buffers, you can navigate forward with :bn and backward :bp.
I'm going to assume you have the file list open inside Vim, and want to simulate the "gf" command across the whole list...
Edit your .vimrc to include this function:
function Openall()
edit <cfile>
bfirst
endfunction
You can then highlight the entire file (or the set of paths you want to open) using visual mode (1G, Shift-V, G) and typing ":call Openall()". Afterwards the command row will show this:
:'<,'>call Openall()
This will run the new Openall() function across all highlighted lines.
Press Enter and all the files will be opened in background buffers. You can then access them using the usual buffer commands. :ls will display them as a list.
I suppose you want to select and list in vim. all the files of a certain extension. From your home directory or a particular source.
find . -name "*.sh" | vim -
Then within vim, you could search and view this potentially huge list.
(Another topic)
You found your file, now you want to open it in a split?
CTRL-W F *CTRL-W_F*
Split current window in two. Edit file name under cursor and
jump to the line number following the file name. See |gF| for
details on how the line number is obtained.
{not available when the |+file_in_path| feature was disabled
at compile time}
CTRL-W gf *CTRL-W_gf*
Open a new tab page and edit the file name under the cursor.
Like "tab split" and "gf", but the new tab page isn't created
if the file does not exist.
{not available when the |+file_in_path| feature was disabled
at compile time}
For my use case, where you already have a buffer open with the list of files in it, I have found the best way to do this. You can type this in vim cmd mode:
:% normal gf<C-v><C-o>
(where <C-v> is you literally typing Ctrl and v, same for <C-o>)
and it becomes:
:% normal gf^O
How this works:
% runs the command for every line in the file
normal runs everything after it in normal mode
gf opens the file under the cursor in a new buffer
<C-v> lets you enter a control-code (<C-o> in this case)
<C-o> goes back to the last location, which will always be this buffer with the list of files
Potential usage:
# open buffer with list of files from grep
$ grep -rl "new User()" mycode/ libraries/ | vim -
# then runs this in vim:
:% normal gf^O
Bonus: Make a keyboard shortcut
Add the following to your ~/.vimrc :
" Open all files in current buffer (e.g. after piping to `vim -`)
nnoremap <leader>oa :% normal gf^O<cr>
This maps \oa to run the command (since <leader> is \ for me).
An alternate method using a macro:
qogf<C-o>j
which runs gf (go file) then uses <C-o> to get back to the initial unnamed buffer, then just goes down one line with j, ready to be repeated.
Then see how many lines in the buffer (12) and run the macro like this:
12#o
I often need to open a changing list of files that had been modified in my SVN checkout.
This one liner works to open all modified files in vim tabs.
svn st | grep ^M | awk "{print($2)}" | xargs vim -p
Try this with bash:
$ vim -S <(sed "s/^/badd /" <your file name>)
But I don't know why the first line of the file is ignored... :-O
This script works as expected:
rm -f myfile
for i in `seq 10000`
do
touch $i
echo $i >> myfile
done
vi -c "badd `head -1 myfile`" -S <(sed "s/^/badd /" myfile)
http://vimdoc.sourceforge.net/htmldoc/starting.html#-S
http://vimdoc.sourceforge.net/htmldoc/windows.html#:bad
Maybe this could help:
http://vimdoc.sourceforge.net/htmldoc/windows.html
It's as simple as typing
vim /dir1/file1 /dir2/file1 /dir2/file2 ...
Once you're in vim, you can switch betwen then with ":n" to go to the next file, ":prev" to go to the previous file.
My searchInRuntime plugin has a :Sp and a :Vsp commands that'll do the trick when called banged. However, the files have to exist.

Resources