What is the line to add to vimrc for storing .swp files in /tmp on Ubuntu - linux

I feel like I've literally tried everything, but no matter what I add to vimrc, I can't get it to store its annoying .swp files in /tmp. I'm using Ubuntu 16.04. I've obviously looked up this issue extensively, but again, no command I enter seems to work. I always end up with:
E510: Can't make backup file
(add ! to override)
Things I've tried:
set backupdir=$~/tmp//
set directory=$~/tmp//
set backupdir=~/tmp//
set backupdir=$HOME~/tmp//
set backupdir=/tmp//
I mean you name it, I've tried it. So, explicitly, what is the exact code I need to type in vimrc to make it so vim saves it's .swp files in my temporary folder, instead of cluttering my workspace?
Thank you.

Adding the following line to my ~/.vimrc puts .swp files for currently open buffers under /tmp
set directory=/tmp
If you've tried this without issues are you able to verify that vim is reading your vimrc at all?

It appears the issue was I didn't really understand what ~ means. I created a folder called 'tmp' in my home directory, and from there used:
set backupdir=~/tmp//

Related

vim changes modification time of folder on saving file in it under windows 7

I have been developing a perl application under Windows 7 that has a subroutine which lists all source files in a given directory. To spare time in the next run source files that had been found are written to a cache (plain txt file). Then this cache is intended to be updated only if there were changes under the source folders. I assumed that I can check modification time of folders to check if files were removed or added and update cache file only if needed.
It seemed to work well until I edited some of the source files with vim which updated the modification date of the containing folder as well. First I thought it is because vim adds a swap file to the directory where the edited source resides. So I set directory in _vimrc file to point to an other location.
set directory=~/.vim/swap " directory to place swap files in
After setting it there were no added swap file in the source folder but the modification time of directory changed after saving the file.
I'm using gVim 8.0.586 for Windows but gVim nightly (8.0.1605), neovim (v0.2.2-1173-gc46d6f8da), cygwin vim (8.0 1-1376) gave me the same result even with a "clean" _vimrc that only sets directory.
Editing the same source file with Notepad++ or Atom changes only the modification time of the file and not the modification time of the directory containing it.
Does anybody have any clue what can cause this behavior of vim?
EDIT
I have also these two lines in may _vimrc:
set backup " make backup files
set backupdir=~/.vim/backup " where to put backup files
As Wumpus suggested i tried to switch off both backup and writebackup and it did work but i did not have any backupfile. :(
:set nobackup nowritebackup
writebackup is not set in _vimrc, so it is on by default.
It's probably the creation and deletion of the backup file.
vim creates a backup before writing the new version of the file, so you can get the old version back in case of a crash.
You can turn off backups with :set nobackup nowritebackup or change the location with :set backupdir=someplaceelse
The backupcopy option may also have some effect. It can try to rename the new file into place. Try yes and no for backupcopy and see if there is a difference. But note there are other side effects... see :help backupcopy. And the behavior may depend on whether your backupdir is on the same filesystem.

MacVim not letting me save files without giving warning

Each time I save (:w) this warning comes up:
E510 can't make backupfile.
/.vimrc includes
set backupdir=/.vim/backups
set dir=/.vim/backups
Those locations exist and I have write permissions to those files.
:verbose set backupdir? says
Last set from ~/.vimrc
I went into the /usr/local/Cellar/macvim/8.0-137/MacVim.app/Contents/Resources/vim directory, made sure I had permissions there in case that was an issue, looked at the vimrc file there and saw nothing about backupdir.
I am tempted to uninstall and reinstall mccvim with home-brew but I am not sure if that will do the job. I prefer not to
set: no backup
but essentially that is what I have now since I can only save with :w!
I have Vim version 8.0 on Mac Sierra 10.12.6
You seem to be confusing / and ~. And a bunch of other things…
The former represents the root of your filesystem, to which you almost certainly not have write privileges if you are not root.
The latter represents your "home" directory, to which you have write privilege. Your "home" can be represented with:
$HOME
~
/Users/username
So, taking your question one part at a time…
Each time I save (:w) this warning comes up:
E510 can't make backupfile.
It means that you can't write to the directory where Vim stores its backup files.
/.vimrc includes
set backupdir=/.vim/backups
set dir=/.vim/backups
Those locations exist and I have write permissions to those files.
You may have a ~/.vimrc (a .vimrc in your "home") but almost certainly not a /.vimrc (a .vimrc at the root of your filesystem).
The right place for your vimrc is either:
~/.vimrc,
~/.vim/vimrc (no dot!).
Your settings tell Vim that it should write backup files in /.vim/backups but there are a few problems with that.
Problems include:
/.vim/ almost certainly doesn't exist,
/.vim/backups/ is therefore very unlikely to exist,
your normal user almost certainly doesn't have the right to write in / anyway.
Assuming the ~/.vim/ directory exists, those options should be:
set backupdir=~/.vim/backups
set dir=~/.vim/backups
:verbose set backupdir? says
Last set from ~/.vimrc
Yeah, no problem here.
I went into the /usr/local/Cellar/macvim/8.0-137/MacVim.app/Contents/Resources/vim directory, made sure I had permissions there in case that was an issue, looked at the vimrc file there and saw nothing about backupdir.
What makes you think anything in Vim's runtime files has anything to do with your issue? Vim told you Last set from ~/.vimrc so there's nowhere else to look.
I am tempted to uninstall and reinstall mccvim with home-brew but I am not sure if that will do the job. I prefer not to
set: no backup
but essentially that is what I have now since I can only save with :w!
Reinstalling MacVim won't change anything. Taking an introductory course on the UNIX shell is definitely a better idea.
Also, did you mean :set nobackup? And I'm not sure "since I can only save with :w" means anything.

How to edit _vimrc file?

I am having trouble saving my settings on vim. I know that in order to do that I need to open the vimrc file but when I do that it says that the file is readonly and won't let me edit it, preventing me from saving my settings.
It says that in order to force save it or override it, I need to add the ! mark after :w in the command section, which I do, but it still does not work.
I can't find any other solution to this problem.
Note: I am using Windows 8.1.
On Windows, you may need to run your editor as administrator to write to your system's _vimrc file.
You may also create a _vimrc in your home directory (e.g. C:\Users\Piet\_vimrc, see the output of :echo $HOME) without administrator access.
You can solve it this way:
Instead of :w, type :w !sudo tee %. I guess it should work.
(my answer is based on this tutotial where the whole problem is well-described)
You may be editing the one global vimrc in /etc/vimrc to which you don't have access.
Make sure you're editing ~/.vimrc and check that you have read and write priviledges to the file (ls -l ~/.vimrc). Since the file is in your home folder you should have those privilages but it won't hurt to verify.

Change location of vimrc

I'm trying to unclutter $HOME, and want to move my .vimrc into ~/.vim. I've already looked at this and this, but none of the solutions presented are what I'm looking for.
I do not want to alias vim with the -u flag because it won't be appropriately set if it is opened in some other way than from the shell.
I do not want to use a symbolic link because it still shows up in $HOME.
I will not compile it from source, I'd rather just use a symlink (which is what I'm currently doing) than deal with that mess.
Can anyone offer some new ideas?
If you are using 7.4, you can simply move your ~/.vimrc file into your ~/.vim/ directory:
~/.vim/vimrc <--- "vimrc", not ".vimrc"
Try this method if you don't use 7.4.

Change default location of vimrc

In Vim, is it possible to change the default location of the user vimrc file, i.e., from $HOME/.vimrc to some other location ?
Another solution might be to create a symlink to you preferred location. I have my .vimrc in $HOME/.vim/.vimrc and symlink to it. This way I can have it in a git repo and backup it.
You must start vim with the command vim -u ./path/to/your/vimrcfile
vim -u NONE is a good way to start Vim without any plugin or customisation.
See :help starting.txt for more information.
The VIMINIT variable is my preferred method. The problem with aliasing vim with the -u flag is that if vim is opened in some way other than from the shell command your configuration won't get pulled in. Setting $VIMINIT does not suffer from this drawback. Check this out for more information.
export VIMINIT='source $MYVIMRC'
export MYVIMRC='~/.vim/vimrc' #or any other location you want
Note that Vim normally sets the MYVIMRC variable, though I'm not sure exactly what it's used for. Based on my testing, using VIMINIT in this fashion will result in it not being automatically set on startup as it would normally be. This is why I'm setting it myself.
This works for neovim too!
On Windows, I have the _vimrc that's in my home directory contain one line, source c:\path\to\my.vimrc.
I have not yet worked out a good way to move the entirety of my vimfiles folder, but that's less critical as it's all stuff I've installed from elsewhere. I.e., it'd be easy to restore if I lost it. (I know that I can change runtimepath but my problem is more coming up with a "good" way to do so.)
Update
After six years I extended slightly from what I mention in the comments below; as I put stuff into 'after' and wanted to just keep rtp clean I got something that has been solid for a while now. Today in my %USERPROFILE%\_vimrc I do hardcode the actual paths to things and it changes on every machine I use (and I basically do the same thing on *nix) but this gets copied around mostly-manually when setting up a new PC. I also have a version which I can use to launch Vim on another connected machine on the network, e.g. a co-worker's machine, so I get my config and all that, but the gist is:
set runtimepath^=E:/dotfiles/vim
set runtimepath+=E:/dotfiles/vim/after
set runtimepath-=~/vimfiles
set runtimepath-=~/vimfiles/after
runtime vimrc
and then %USERPROFILE%\_gvimrc just has one line:
runtime gvimrc
(Both vimrc and gvimrc are in the /dotfiles/vim folder and also on Bitbucket.)
I see two options, depending on your needs.
Have ~/.vimrc import the other location
create an alias in your bashrc alias vim="vim -u otherlocation"
I edited
C:\Program Files\Vim\_vimrc
and changed both the runtimepath and sourced my own .vimrc.
I also use these settings in Cygwin (and have them version controlled). So it's this in practice (added at the bottom of the _vimrc file):
let &runtimepath = 'C:\cygwin\home\cygwinaccount\.vim,' . &runtimepath
source C:\cygwin\home\cygwinaccount\.vimrc
Bliss ! :)
In linux:
You can edit .bashrc or .zshrc startup script and add the following lines to change the default location of .vimrc file
export VIMINIT='source $MYVIMRC'
export MYVIMRC='~/.vim/.vimrc' # Note the . (dot) before vimrc. If that is what you have called it.
I feel like the simplest solution is to just have a single line in ~/.vimrc that loads the vimrc from the other location, i.e.:
source PATH/TO/OTHER/LOCATION/.vimrc

Resources