问题
Let's say I have a file called FooBar.java. When I open the file in a powershell window with
> vim foobar.java
and write the file after editing it, the file gets renamed FooBar.java -> foobar.java.
If I open the file with
> vim FOOBAR.java
it gets renamed FooBar.java -> FOOBAR.java.
So Vim changes the name to be case sensitive to how I spelled it when I opened the file with a powershell command. This is undesired behaviour for me, and I did not have this problem earlier, before I did a clean install of my OS (Windows 10). I am using the exact same .vimrc as before. This is a huge pain in the ass since java is case sensitive with it's filenames.
However, it seems to only happen with .java files. With .cpp or .txt or .csv files, the capitalization of the file remains the same regardless of how I type it.
My question is in two parts:
- Why is Vim suddenly doing this and wasn't doing it before with an identical .vimrc?
- How do I disable this behaviour for good, preferably with a .vimrc line?
There is a 3 month old duplicate, but as I cannot comment to it because of my low internet-points, and it has 0 answers, I made a new one.
Vim save file in lower case
Here are the contents of my .vimrc:
"Created for Vim version 8.2
set clipboard+=unnamed
set backspace=indent,eol,start
set noswapfile
set undofile
set undodir=~\vimfiles\undodir
set encoding=utf8
set fileencoding=utf8
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set hlsearch
set incsearch
set ignorecase
set smartcase
set number
set ruler
set linebreak
set showbreak=+
"set listchars=tab:>-,trail:.,extends:>
"set list
:nnoremap U :echo " < < ===== C H E C K C A P S L O C K ===== > > "<CR>
syntax on
set cursorline
hi cursorline ctermbg=darkblue
hi cursorline ctermfg=white
hi colorcolumn ctermbg=darkblue
hi linenr ctermfg=blue
hi preproc ctermfg=magenta
hi type ctermfg=yellow
hi statement ctermfg=magenta
hi constant ctermfg=darkgreen
hi special ctermfg=green
hi comment ctermfg=darkgrey
hi number ctermfg=red
filetype on
autocmd filetype cpp set colorcolumn=80,101
autocmd filetype cpp set nowrap
autocmd filetype hpp set colorcolumn=80,101
autocmd filetype hpp set nowrap
autocmd filetype c set colorcolumn=80,101
autocmd filetype c set nowrap
autocmd filetype h set colorcolumn=80,101
autocmd filetype h set nowrap
autocmd filetype java set colorcolumn=80,101
autocmd filetype java set nowrap
autocmd filetype python set colorcolumn=80
autocmd filetype python set nowrap
autocmd filetype gitcommit set colorcolumn=51,73
autocmd filetype gitcommit set nowrap
回答1:
You can fix this with:
set backupcopy=yes
With this option set to its default value, Vim ends up backing up the original file by renaming it and then creating a new file with the same name as the one you're editing. But in filesystems with case-insensitive filenames (typically, Windows) and if you refer to files by a name with a different case, Vim might end up creating the new file with the different case.
Using backupcopy=yes
will have Vim create the backup as a new copy (instead of a rename of the original file) and then it will overwrite the original file. When that operation is performed, the case of the filename won't change, since Vim isn't really creating a new file but writing to an existing one, so the filesystem will preserve the original name (and any attributes the of the original file.)
See :help 'backupcopy' for more details.
来源:https://stackoverflow.com/questions/65963602/vim-overwrites-filename-when-writing