On Nov 10, 1:06 pm, Marcio Gil <[email protected]> wrote:
> When I edit a DOS batch (for example), I always need to put ':e +
> +enc=cp850'.
>
> Can I add in my _vimrc file a auto command for this? Example:
>
> autocmd FileType dosbatch setlocal fileencoding=cp850
> (don't works!)
>
This doesn't work because changing fileencoding doesn't have any
effect on the characters in the buffer. You actually know how to do
this already, just change your :setlocal command, to an :e ++enc
command.
Here's why it fails. Without a specific encoding specified with ++enc,
Vim uses 'fileencodings' (note the 's' at the end) to determine which
encodings to try when reading a file. The first one in the list which
does not result in invalid data, is used (roughly...there's some other
considerations like a BOM for Unicode files). Vim then uses the
determined encoding to interpret the bytes contained in the file while
reading it in, setting the 'fileencoding' option to reflect the
choice. Using this encoding, Vim creates a buffer which is internally
stored in the encoding specified by the 'encoding' option, doing
conversions as necessary between 'fileencoding' and 'encoding'. Once
you have the buffer of text in 'encoding', you can change
'fileencoding' to anything you want with no effect on the buffer
content. The only further effect 'fileencoding' will have, is when
writing the file. When you finally write the file, Vim will do a
conversion from the charactes in the buffer (stored as bytes according
to 'encoding') to bytes in your chosen 'fileencoding'.
What :e ++enc=cp850 does, is to tell Vim to short-circuit the encoding
detection, and just use cp850 when reading. Everything else stays the
same.
While this should work:
autocmd FileType dosbatch e ++enc=cp850
I actually have something a bit more complex (I've removed some
irrelevant stuff for your immediate problem, if some of this is
confusing as-is). I use a different method, by changing
'fileencodings' prior to loading the file, so that Vim automatically
detects my desired fileencoding:
" Don't detect utf-8 without a BOM by default, I don't use UTF-8
normally
" and any files in latin1 will detect as UTF. Detect cp1252 rather
than
" latin1 so files are read in correctly. Fall back to latin1 if
system does
" not support cp1252 for some reason.
exec 'set fileencodings=ucs-bom,'.s:windows_enc.',latin1'
if has('autocmd')
augroup fenc_detect
au!
" batch files need to use the encoding of the cmd.exe prompt in
Windows
if has('win32') || has('win64')
" get the cmd.exe encoding by asking for it
let g:batcp = substitute(system('chcp'), '^\c\s*Active code
page: \(\d\+\)\s*[^[:print:]]*$', 'cp\1', '')
if g:batcp =~? '^cp\d\+$'
autocmd BufReadPre *.bat exec 'set fileencodings='.g:batcp
autocmd BufNewFile *.bat exec 'setlocal
fileencoding='.g:batcp
endif
endif
" restore default fileencodings after loading the files that use
a special
" value to force specific encodings
exec 'autocmd BufReadPost *.bat set fileencodings=ucs-
bom,'.s:windows_enc.',latin1'
augroup END
endif
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php