I would like to create backup file in several directories after :w in vim, if statement is true. Vim :help says, that you need to put commas between directories and nothing else. But it's not working for me. It reads only the first directory. I tried different ways, such as usingset backupdir+=, or ~/. instead of ..
set backup
set nowritebackup
set backupdir=~/Dropbox
if expand("%")==".vimrc"
set backupdir=.,~/.vim/backUpDir/,~/Dropbox
endif
In .vimrc expand returns
:echo expand("%")==".vimrc"
1
vim --version
VIM - Vi IMproved 7.4
MacOS X (unix) version
I do not believe this is possible without custom plugin. If you read the vim help carefully, it says that the backup file will be created in the first directory in the list where this is possible. So the behavior you are seeing is by design.
*'backupdir'* *'bdir'*
'backupdir' 'bdir' string (default for Amiga: ".,t:",
for MS-DOS and Win32: ".,c:/tmp,c:/temp"
for Unix: ".,~/tmp,~/")
global
{not in Vi}
List of directories for the backup file, separated with commas.
- The backup file will be created in the first directory in the list
where this is possible. The directory must exist, Vim will not
create it for you.
If you really want to be able to backup to multiple directories, I would suggest writing a function to do this, and attaching it to BufWritePre.
Related
When I try to open a file in Vim (Linux) for editing, when I press TAB, Vim autocompletes filename only with filenames from the current directory. However, having searched on the Web, I suppose that from version 7 Vim should support bash-like filename autocompletion using filenames from all the directories in the search path.
Say, there is a file file1 in a directory dir1 (which directory is also in the environment variable PATH).
I type the following commands in Vim:
set path=/dir1
set wildmode=list:longest
And then, when I type:
:e fil<TAB>
The filename is not autocompleted. How to enable this feature in Vim?
Tab-completion works. You just expect it to do something it is not actually supposed to do.
:e[dit] and its siblings (:sp[lit], :vs[plit], :tabe[dit]) don't use the path option at all, no matter what version of Vim you have.
Use :fin[d] fil<Tab> instead (and :sf[ind], :vert sf[ind], :tabf[ind]).
Use set path=/dir1/** to make :find recursive.
See :help 'path' and :help :find.
edit
It is generally considered "good practice" to start Vim from the root of your project:
$ cd /path/to/project
$ vim somefile
The main advantage being that it sets Vim's "current directory" to a usable value that allows you to browse your project relatively easily or use external programs on your project in a clean and intuitive way.
I've found that while using Vim on Windows Vim saves the file, a .ext.swp file that's deleted on closing the Vim window and a .ext~ file.
I assume the .ext.swp file is a session backup in case Vim crashes. What's the purpose of the .ext~ file however? Is this a permanent backup file? It's annoying as I'd like to copy all the files I'm working on to my host, without these duplicates. How can I turn this off or, if it's there for a good reason, hide the files?
I think the better solution is to place these lines in your vimrc file
set backupdir=~/vimtmp//,.
set directory=~/vimtmp//,.
The first line is for backup files, the second line for swap files. The double slash at the end ensures that there is no conflict in case of two files having the same name, see comments (at the time of this edit this option is only honored for swap files, not yet for backup files). The ,. allow vim to use the current directory if the former doesn't exist.
You have to create a directory in your home directory called vimtmp for this to work. Also, check that backups are enabled in your config (add set backup if not).
That way you get the benefit of both worlds, you don't have to see the files, but if something does get futzed you can go get your backup file from vimtmp. Don't forget to clean the directory out every now and then.
The *.ext~ file is a backup file, containing the file as it was before you edited it.
The *.ext.swp file is the swap file, which serves as a lock file and contains the undo/redo history as well as any other internal info Vim needs. In case of a crash you can re-open your file and Vim will restore its previous state from the swap file (which I find helpful, so I don't switch it off).
To switch off automatic creation of backup files, use (in your vimrc):
set nobackup
set nowritebackup
Where nowritebackup changes the default "save" behavior of Vim, which is:
write buffer to new file
delete the original file
rename the new file
and makes Vim write the buffer to the original file (resulting in the risk of destroying it in case of an I/O error). But you prevent "jumping files" on the Windows desktop with it, which is the primary reason for me to have nowritebackup in place.
To turn off those files, just add these lines to .vimrc (vim configuration file on unix based OS):
set nobackup #no backup files
set nowritebackup #only in case you don't want a backup file while editing
set noswapfile #no swap files
:set nobackup
will turn off backups. You can also set a backupdir if you still want those backup files but in a central folder. This way your working dir is not littered with ~ files.
You find more information on backups under :he backup.
And you can also set a different backup extension and where to save those backup (I prefer ~/.vimbackups on linux). I used to use "versioned" backups, via:
au BufWritePre * let &bex = '-' . strftime("%Y%m%d-%H%M%S") . '.vimbackup'
This sets a dynamic backup extension (ORIGINALFILENAME-YYYYMMDD-HHMMSS.vimbackup).
Put this line into your vimrc:
set nobk nowb noswf noudf " nobackup nowritebackup noswapfile noundofile
In windows that would be the:
C:\Program Files (x86)\vim\_vimrc
file for system-wide vim configuration for all users.
Setting the last one noundofile is important in Windows to prevent the creation of *~ tilda files after editing.
I wish Vim had that line included by default. Nobody likes ugly directories.
Let the user choose if and how she wants to enable advanced backup/undo file features first.
This is the most annoying part of Vim.
The next step might be setting up:
set noeb vb t_vb= " errorbells visualbell
to disable beeping in vim as well :-)
You're correct that the .swp file is used by vim for locking and as a recovery file.
Try putting set nobackup in your vimrc if you don't want these files. See the Vim docs for various backup related options if you want the whole scoop, or want to have .bak files instead...
The only option that worked for me was to put this line in my ~/.vimrc file
set noundofile
The other options referring to backup files did not prevent the creation of the temp files ending in ~ (tilde)
I had to add set noundofile to ~_gvimrc
The "~" directory can be identified by changing the directory with the cd ~ command
In my environment, I share vim configuration with other developers (and have my own configuration additions as well) in various .vimrc files. Some places in the environment, I edit a file in vim and automagically a copy of that file with a trailing tilde suffix appears. What vim options control this? I'd like to turn the feature off, as it just clutters up my directories and spoils auto-completion on the command line.
Thanks.
If you just want to turn off backups entirely, the :set nobackup suggestion from #GregHewgill is the way to go.
However, I've been saved by Vim's backups often enough that I'd like to recommend an alternative. I have these settings in my .vimrc:
set backupdir-=.
set backupdir^=~/tmp,/tmp
The first line removes the current directory from the backup directory list (to keep the ~ backups out of your working directories). The second tells Vim to attempt to save backups to ~/tmp, or to /tmp if that's not possible. (The paths can be adjusted for your particular environment; I use set backupdir^=$TEMP on Windows, for example.)
In other words, this puts the backup files out of sight (and out of your way), but doesn't turn them off entirely.
It sounds like you're looking for the nobackup option:
:set nobackup
Backups are not turned on by default, so you've probably got a config file somewhere that's turned them on.
On windows, I had set nobackup in C:\Users\my-user-name\_vimrc file; but that didn't seem to work. I then added set nobackup to C:\Program Files (x86)\Vim\_vimrc file. Still didn't work!
I later figured that C:\Program Files (x86)\Vim\_vimrc loads a sample vimrc script from $VIMRUNTIME/vimrc_example.vim and that script has this snippet:
if has("vms")
set nobackup " do not keep a backup file, use versions instead
else
set backup " keep a backup file (restore to previous version)
if has('persistent_undo')
set undofile " keep an undo file (undo changes after closing)
endif
endif
Commenting that out worked for me! So it really comes down to figuring out the order in which these scripts are getting executed, thereby overriding anything you might have done.
Two files will be created after editing. A file with "~" suffix, and another one with "un~".
Add the lines below in the _vimrc or .vimrc file:
set nobackup
set noundofile
I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins
I've found that while using Vim on Windows Vim saves the file, a .ext.swp file that's deleted on closing the Vim window and a .ext~ file.
I assume the .ext.swp file is a session backup in case Vim crashes. What's the purpose of the .ext~ file however? Is this a permanent backup file? It's annoying as I'd like to copy all the files I'm working on to my host, without these duplicates. How can I turn this off or, if it's there for a good reason, hide the files?
I think the better solution is to place these lines in your vimrc file
set backupdir=~/vimtmp//,.
set directory=~/vimtmp//,.
The first line is for backup files, the second line for swap files. The double slash at the end ensures that there is no conflict in case of two files having the same name, see comments (at the time of this edit this option is only honored for swap files, not yet for backup files). The ,. allow vim to use the current directory if the former doesn't exist.
You have to create a directory in your home directory called vimtmp for this to work. Also, check that backups are enabled in your config (add set backup if not).
That way you get the benefit of both worlds, you don't have to see the files, but if something does get futzed you can go get your backup file from vimtmp. Don't forget to clean the directory out every now and then.
The *.ext~ file is a backup file, containing the file as it was before you edited it.
The *.ext.swp file is the swap file, which serves as a lock file and contains the undo/redo history as well as any other internal info Vim needs. In case of a crash you can re-open your file and Vim will restore its previous state from the swap file (which I find helpful, so I don't switch it off).
To switch off automatic creation of backup files, use (in your vimrc):
set nobackup
set nowritebackup
Where nowritebackup changes the default "save" behavior of Vim, which is:
write buffer to new file
delete the original file
rename the new file
and makes Vim write the buffer to the original file (resulting in the risk of destroying it in case of an I/O error). But you prevent "jumping files" on the Windows desktop with it, which is the primary reason for me to have nowritebackup in place.
To turn off those files, just add these lines to .vimrc (vim configuration file on unix based OS):
set nobackup #no backup files
set nowritebackup #only in case you don't want a backup file while editing
set noswapfile #no swap files
:set nobackup
will turn off backups. You can also set a backupdir if you still want those backup files but in a central folder. This way your working dir is not littered with ~ files.
You find more information on backups under :he backup.
And you can also set a different backup extension and where to save those backup (I prefer ~/.vimbackups on linux). I used to use "versioned" backups, via:
au BufWritePre * let &bex = '-' . strftime("%Y%m%d-%H%M%S") . '.vimbackup'
This sets a dynamic backup extension (ORIGINALFILENAME-YYYYMMDD-HHMMSS.vimbackup).
Put this line into your vimrc:
set nobk nowb noswf noudf " nobackup nowritebackup noswapfile noundofile
In windows that would be the:
C:\Program Files (x86)\vim\_vimrc
file for system-wide vim configuration for all users.
Setting the last one noundofile is important in Windows to prevent the creation of *~ tilda files after editing.
I wish Vim had that line included by default. Nobody likes ugly directories.
Let the user choose if and how she wants to enable advanced backup/undo file features first.
This is the most annoying part of Vim.
The next step might be setting up:
set noeb vb t_vb= " errorbells visualbell
to disable beeping in vim as well :-)
You're correct that the .swp file is used by vim for locking and as a recovery file.
Try putting set nobackup in your vimrc if you don't want these files. See the Vim docs for various backup related options if you want the whole scoop, or want to have .bak files instead...
The only option that worked for me was to put this line in my ~/.vimrc file
set noundofile
The other options referring to backup files did not prevent the creation of the temp files ending in ~ (tilde)
I had to add set noundofile to ~_gvimrc
The "~" directory can be identified by changing the directory with the cd ~ command