Cannot write to backup file - vim

Since I have upgraded to Catalina, I cannot edit my dotfiles but get a message saying "Cannot write to backup file...".
I have checked the permissions on the file and my user account is the owner and has read and write permissions. If I edit the files with BBedit, I can save the files so the issue just seems to be editing within neovim.
I can also edit files using neovim in other directories but not within my dotfile directory.
Additional information added:
I have noticed that the files that I can't edit have the following permission structure:
.rwxrwxrwx# 1.4k pdd 30 Aug 2017 plugin_manager.vim
I have now removed the # attribute using xattr but that has not made any difference.
Additional Information Update
I have narrowed it down to something in my vimrc file. If I start neovim without my vimrc, I can save files in my vimrc directory.
I have the following code in my vimrc relating to backup files:
if exists('$SUDO_USER')
set nobackup " don't create root-owned files
set nowritebackup " don't create root-owned files
else
set backupdir=~/local/.config/nvim/tmp/backup
set backupdir+=~/.config/nvim/tmp/backup " keep backup files out of the way
set backupdir+=.
endif
If I comment these lines out, I can then save files in my vimrc directory. I am a little confused as I have had these in my vimrc for some time and have not had a problem.
I am probably missing something simple. Does anyone have any suggestions?

The original backup I used for vim was found here: https://vim.fandom.com/wiki/Incremental_backup_in_central_backup_directory created July 22, 2005 author Sylvain Lafleur
It doesn't work in neovim, but I was able to get it working:
I'm using KDE Neon. The original looks like it might work on windows, but I have not tested it.
A few things to note:
You will need to manually edit the g:backupdir and
g:this_root_backkup_dir (This may be reduced, but I didn't mess with it.)
I was unable to make $HOME work, so use /home/username on linux.
This is not my actual directory structure, but should suffice as an
example, in that /home/neon/vim existed before I used this code.
This is very redundant backup. Every time the file is saved it will
create a backup of the filename with date/time stamp.
In the .vimrc put your vim specific code in if !has('nvim')
and endif blocks. I put the following code in my init.vim file.
set backup
set backupcopy=yes
function Write_backup()
let g:backupdir='/home/neon/vim/vim_backups'
let &backupdir=g:backupdir
let g:backupext = strftime("_%Y-%m-%d_%H-%M-%S")
let &backupext=g:backupext
let g:this_root_backup_dir = '/home/neon/vim/vim_backups'
let g:this_dir = substitute(expand("%:p:h")," ","_","g")
let g:this_filename = substitute(expand("%")," ","_","g")
let g:this_drive = strpart(g:this_dir, 0, 1)
let g:this_backup_dir_drive = g:this_root_backup_dir . g:this_drive
let g:this_backup_dir = g:this_backup_dir_drive . strpart(g:this_dir, 1)
"--make DRIVE directory if it doesn't exist
if !filewritable(g:this_backup_dir_drive)
silent! execute expand('!mkdir -p ' . g:this_backup_dir_drive)
endif
"--make directory under DRIVE if it doesn't exist
if !filewritable(g:this_backup_dir)
silent! execute expand('!mkdir -p ' . g:this_backup_dir)
endif
"--set new backup dir
let g:backupdir = g:this_backup_dir
let &backupdir=g:backupdir
endfunction
call Write_backup()
inoremap <ESC> <ESC>:call Write_backup()<CR><C-l>
" note the <C-l> clears the output so remove if testing with echo

Related

Vim ctrlp only works with a git repo

The vim plugin ctrl p only works for me into git repo.
Why does it need a .git file?
Be work i mean it is searching my entire machine when no .git file is found.
my settings
let g:ctrlp_working_path_mode = 'ra'
let g:ctrlp_match_window = 'top,order:ttb,min:1,max:30,results:30'
let g:ctrlp_custom_ignore = 'node_modules\|vendor/|DS_Store\|git'
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.idea/*,*/.DS_Store,*/vendor
Also tried:
let g:ctrlp_working_path_mode = 'r'
let g:ctrlp_working_path_mode = 'c'
I'm not entirely sure what you are after, it sounds like you want ctrlp to pick up all files system wide and not just the "project" root directory.
The g:ctrlp_working_path_mode flags you set would either tell ctrlp c - only show me files from current loaded buffers directory including subdirectories, or r the first project root directory as identified with some sort of hidden repository directory like .git. Neither of which suggests what I think you are asking for...
So I'll go out of a limb and suggest that you can probably update the global variable:
let g:ctrlp_cmd = 'CtrlP /'
To look for all files, on a linux system. As detailed in the reader:
Run :CtrlP or :CtrlP [starting-directory] to invoke CtrlP in find file mode.
Or test the above with CtrlP / in command mode first.
Again, I've guessed what you wanted here...

How can I source files relative to file?

I'm trying to split my vimrc up into multiple files - init.vim, keybindings.vim, ui.vim, etc. - but I can't get Vim to source files relative to init.vim (it instead sources relative to where I launch Vim from.
This is what I've got at the top of init.vim:
source keybindings.vim
source ui.vim
If I run vim from the same directory as those files, it works fine; if I run it from any other directory, I get the following errors:
Error detected while processing /path/to/vimrc:
line 1:
E484: Can't open file keybindings.vim
line 2:
E484: Can't open file ui.vim
Press ENTER or type command to continue
Edit: It's worth noting that I'm using NixOS, so I don't know what the absolute paths will be, nor if they would be constant if I found out.
I think you can use
runtime keybindings.vim
Source needs the full path, you can however simplify it using something like this :
let path = expand('%:p:h')
exec 'source' path . '/keybindings.vim'
You can have a look at mine here - https://github.com/dhruvasagar/dotfiles/blob/master/vim/vimrc for reference.
If the order is not important, you can just put your scripts into ~/.vim/plugin/, and they will be sourced after ~/.vimrc. You can check :scriptnames output to see what gets sourced when.
You can influence the ordering somewhat via the plugin filenames. For example, I have a ~/.vim/plugin/00plugin-configuration.vim that configures Vim plugins; the 00... ensures this is sourced first.
To get finer control, I would instead put the scripts into ~/.vim/. Vim will ignore them there, but they can easily be addressed via :runtime, which looks in all runtimepaths, and ~/.vim/ typically is included in 'runtimepath':
# .vimrc
runtime init.vim
runtime keybindings.vim
...
Relevant help pages: :help .vimrc and :help load-plugins.
Building on Dhruva's answer, you can make a function to help out with this
function! SourceLocal(relativePath)
let root = expand('%:p:h')
let fullPath = root . '/'. a:relativePath
exec 'source ' . fullPath
endfunction
You then use it like
call SourceLocal ("yourScript.vim")
I have met exactly the same issue with you in Neovim. I split my large init.vim file into several small vim scripts and I want to source them inside init.vim.
This is what I get finally based on #Dhruva Sagar's links:
let g:nvim_config_root = stdpath('config')
let g:config_file_list = ['variables.vim',
\ 'options.vim',
\ 'autocommands.vim',
\ 'mappings.vim',
\ 'plugins.vim',
\ 'ui.vim'
\ ]
for f in g:config_file_list
execute 'source ' . g:nvim_config_root . '/' . f
endfor
As none of the solutions works as a real substitute for source working globally (on any script, even sourced from vimrc), I ended up with this solution and decided to share here, which can be used as a substitute for source with relative support, as simple as:
Rsource /home/me/.vim/your/file/path
Rsource $HOME/.vim/your/file/path
Rsource your/file/path
Rsource ../your/file/path
To use it, this must be defined on your vimrc or any file sourced by it before you can use Rsource:
if !exists('g:RelativeSource')
function! g:RelativeSource(file)
let file = expand(a:file)
" if file is a root path, just source it
if stridx(file, '/') == 0
exec 'source ' . file
return
endif
let sfile = expand('<sfile>:p:h')
" If this is called outside this script, it will contains this script
" name, this function name, a script_marker then the executing script name
" In this case we extract just the last part, the script name which called
" the this function
let script_marker = '..script '
let path_index = strridx(sfile, script_marker)
if path_index == -1
let path_index = 0
else
let path_index += len(script_marker)
endif
let path = strpart(sfile,path_index)
let absolute_path = resolve(path . '/'. file)
exec 'source ' . absolute_path
endfunction
command! -nargs=1 Rsource :call g:RelativeSource(<q-args>)
endif
This is safe to be used in any script or plugin.
These are all great solutions, and this is what I ended up using.
let home = expand('~')
exec 'source' home . '/.config/nvim/prettierConfig.vim'

Workaround for Vim's inability to support `backupdir` full paths

Recently I tried to switch to having backup and swap files in a directory other than the working directory. With swp files, you can just put double-slashes at the end of the path:
set directory=~/.vim/swp//
However, this does not work with backupdir. I found an answer with a workaround however:
autocmd BufWritePost * :execute ':w! ' ."$HOME/.vim/backups/" . escape(substitute(expand('%:p'), "/", "%", "g"), "%")
I have a few questions regarding this workaround:
I know it saves files in $HOME/.vim/backups/[filename with path], but how does it do this?
How can I modify it so it saves the file as %full%file%path%filename~ (the difference is the ~ at the end)
Is it possible to reformat this command so it doesn't cross the preferred 80 char mark?
Thanks
My workaround was
" this is a default. to be updated per file type
autocmd BufNewFile,BufNew,BufRead * let b:buDir = "/work/BACKUP/vimBackups/all/"
then later, on a per-file-type basis I did
let b:buDir = "/work/BACKUP/vimBackups/text/"
or
let b:buDir = "/work/BACKUP/vimBackups/coding/"
Then, I call my backup on every exit
" the backup command
" currently, overwrite whatever is there: a new file will be made each second
" if necessary
autocmd VimLeavePre,BufWritePre * silent execute ":write! >> ".b:buDir."vim_bu_".strftime('%Y-%b-%d__%H:%M:%S')."_".b:buFilename.".bu.txt "
Then, in a daily clean-up script, run from cron (anacron), I delete out my old backups
# vim backups older than a month old
find $VIM_BU_DIR -not -newerat "1 month ago" -delete ;

How to make Syntastic search for javac config file in project

I'm using Syntastic with vim and I've added external libraries to its classpath (using SyntasticJavacEditClasspath). This creates a file in the current working directory (which was my project folder). This is all fine. However, whenever I restart vim, Syntastic seems to search for the .syntastic_javac_config file in the current working directory only, and the cwd is, of course, randomly whereever I left it in my last operation. So it doesn't find the file and I get a bunch of incorrect import errors. Can Syntastic be told to search the file's parents for the config file? If not, is there a way of using vim where this typically works? (I've only been using vim for a couple of months so I might be clueless.)
According to the official manual, the snippet should solve the problem. Put it in your .vimrc, change the javascript, jscs, etc. to required values
function! FindConfig(prefix, what, where)
let cfg = findfile(a:what, escape(a:where, ' ') . ';')
return cfg !=# '' ? ' ' . a:prefix . ' ' . shellescape(cfg) : ''
endfunction
autocmd FileType javascript let b:syntastic_javascript_jscs_args =
\ get(g:, 'syntastic_javascript_jscs_args', '') .
\ FindConfig('-c', '.jscs', expand('<afile>:p:h', 1))

Change Vim swap/backup/undo file name

Is it possible to change the way Vim names its swap/backup/undo files?
To avoid clutter, I've set options in my ~/.vimrc to dump these files in ~/.vim/tmp/{swap,backup,undo}; however, as I routinely edit files in different directories with the same name, I often end up with lots of otherwise indistinguishable files and Vim sometimes has trouble recovering.
Ideally, I'd like to use the naming scheme that the persistent undo has (%path%to%file.undo) for all these auxiliary files; there's no obvious way to set it, but can it be done with Buf{Read,Write} macros?
I have this in my .vimrc and it names the swap files with full path names and percent signs just as you describe:
" Store swap files in fixed location, not current directory.
set dir=~/.vimswap//,/var/tmp//,/tmp//,.
The key is the // at the end of the directories. See this note from :help dir:
For Unix and Win32, if a directory ends in two path separators "//"
or "\\", the swap file name will be built from the complete path to
the file with all path separators substituted to percent '%' signs.
This will ensure file name uniqueness in the preserve directory.
Here's part of my .vimrc from github.
This sets the undodir (and turns it on), sets the backupdir, and directory (used for .swp files). Note that it creates the directories if they don't already exist.
" Save your backup files to a less annoying place than the current directory.
" If you have .vim-backup in the current directory, it'll use that.
" Otherwise it saves it to ~/.vim/backup or .
if isdirectory($HOME . '/.vim/backup') == 0
:silent !mkdir -p ~/.vim/backup >/dev/null 2>&1
endif
set backupdir-=.
set backupdir+=.
set backupdir-=~/
set backupdir^=~/.vim/backup/
set backupdir^=./.vim-backup/
set backup
" Save your swap files to a less annoying place than the current directory.
" If you have .vim-swap in the current directory, it'll use that.
" Otherwise it saves it to ~/.vim/swap, ~/tmp or .
if isdirectory($HOME . '/.vim/swap') == 0
:silent !mkdir -p ~/.vim/swap >/dev/null 2>&1
endif
set directory=./.vim-swap//
set directory+=~/.vim/swap//
set directory+=~/tmp//
set directory+=.
" viminfo stores the the state of your previous editing session
set viminfo+=n~/.vim/viminfo
if exists("+undofile")
" undofile - This allows you to use undos after exiting and restarting
" This, like swap and backup files, uses .vim-undo first, then ~/.vim/undo
" :help undo-persistence
" This is only present in 7.3+
if isdirectory($HOME . '/.vim/undo') == 0
:silent !mkdir -p ~/.vim/undo > /dev/null 2>&1
endif
set undodir=./.vim-undo//
set undodir+=~/.vim/undo//
set undofile
endif
Hopefully, it's commented well enough to understand what's going on. If not, add a comment and I'll fix it.
Ciao!
Update [07/16/2012]
I got an email from Rob Kine asking these questions about the backupdir section that I wanted to answer for everyone:
It looks like you are removing the current directory, and then re-adding it. what does that do?
What does the ^= operator do?
How does the order of precedence in the use of folders checked work in Vim? (Like is the last folder added the first one it checks for?)
The first thing is to describe the different operators. These operators have different meanings for non-string-list options, so be warned!
-= removes the value from a string list;
+= appends the value to a string list;
^= prepends the value to a string list.
So the backupdir has the following operations applied:
Remove the current directory from the list.
Append the current directory to the list (this ensures it is the last thing checked).
Remove the home directory from the list (I don't like stuff being saved there).
Prepend ~/.vim/backup/.
Prepend ~/.vim-backup/.
When Vim looks for where to save the backups, it checks from first to last; so it'll check for ~/.vim-backup/, then check for ~/.vim/backup, then check the default list (except for . and ~/ which were removed), and finally check .
You can get help for all these in Vim by using (for example) :help ^= or :help backupdir.
Create the directory undo
$ mkdir ~/.vimundo
Set up your .vimrc file
set undodir=~/.vimundo

Resources