In GVIM, I'd like to save the current file with either \r\n or \n line endings. Depending on the opened file, the appropriate line endings should be chosen automatically. I had a look at https://unix.stackexchange.com/questions/79827/make-vim-act-as-notepad-on-mixed-line-endings but it doesn't quite do what I want.
My preferred behavior would be:
- If the file in the current buffer contains NO carriage returns (\r), save the whole file with line feed (\n) line endings
- If the file in the current buffer contains ANY carriage returns, save the whole file with \r\n line endings
However, I can't get quite this behavior. My current .vimrc
looks like this:
" Some settings in my .vimrc that may be important
set encoding=utf-8
set list
set ffs=unix
source $VIMRUNTIME/mswin.vim
behave mswin
" My current status
function! CorrectLineEndings()
" Carriage return
let @g = "^M"
" Testing for carriage return - doesn't work
if match(getreg('<reg>'),@g) == @g
" convert to \r\n line endings -> HOW?
else
" convert to \n line endings
:%s/\r//g<CR>
endif
endfunction
autocmd BufWriteCmd *.* call CorrectLineEndings()
There are two problems with this code - the testing for existing \r endings doesn't work and I'm not sure how to replace all line endings with \r\n. Any help would be greatly appreciated! Thanks in advance!
ff=dos
files recently I noticed the correct line endings were used)ffs
default value is likely to mess the automagic behaviour of Vim.. Otherwise, I'd have listenedFileRead
event to setff
accordingly -- in case the default behaviour didn't work.FileRead
what line endings there are? With amatch
as indicated above or another way?