(Remove html character entities) |
(link to AutoFenc and other tips, plus mention that the modeline doesn't work by default in case that is not clear) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
|id=1195 |
|id=1195 |
||
|previous=1194 |
|previous=1194 |
||
− | |next= |
+ | |next=1197 |
− | |created= |
+ | |created=2006 |
|complexity=basic |
|complexity=basic |
||
|author=Anatoli Sakhnik |
|author=Anatoli Sakhnik |
||
Line 9: | Line 9: | ||
|rating=11/7 |
|rating=11/7 |
||
|category1=Encoding |
|category1=Encoding |
||
− | |category2= |
+ | |category2=File Handling |
}} |
}} |
||
I often need to edit files with different encodings, which can't be detected automatically (for example, how distinguish between cp866 and cp1251?). So when the desired file was loaded with a wrong encoding, I used to type: |
I often need to edit files with different encodings, which can't be detected automatically (for example, how distinguish between cp866 and cp1251?). So when the desired file was loaded with a wrong encoding, I used to type: |
||
Line 29: | Line 29: | ||
if choice >= 0 && choice < len(encodings) |
if choice >= 0 && choice < len(encodings) |
||
execute 'e ++enc='.encodings[choice].' %:p' |
execute 'e ++enc='.encodings[choice].' %:p' |
||
+ | endif |
||
+ | endf |
||
+ | nmap <F8> :call ChangeFileencoding()<CR> |
||
+ | </pre> |
||
+ | |||
+ | Here is a version of this function which wraps the encodings instead of prompting the user: |
||
+ | <pre> |
||
+ | let g:enc_index = 0 |
||
+ | function! ChangeFileencoding() |
||
+ | let encodings = ['cp1251', 'koi8-u', 'cp866'] |
||
+ | execute 'e ++enc='.encodings[g:enc_index].' %:p' |
||
+ | if g:enc_index >=2 |
||
+ | let g:enc_index = 0 |
||
+ | else |
||
+ | let g:enc_index = g:enc_index + 1 |
||
endif |
endif |
||
endf |
endf |
||
Line 36: | Line 51: | ||
Further, it can be improved by populating the list of encodings from &fileencodings. |
Further, it can be improved by populating the list of encodings from &fileencodings. |
||
− | You can also [[VimTip911|set file encoding automatically from a modeline]]. |
+ | You can also configure Vim to [[VimTip911|set file encoding automatically from a modeline]], which doesn't work as intended by default because the modeline takes effect '''after''' the file is read into memory, or you can install a plugin like {{script|id=2721|text=AutoFenc}} to automatically detect the encoding in more situations. |
− | == |
+ | ==See also== |
+ | * [[Detect encoding from the charset specified in HTML files]] |
||
− | + | * [[VimTip690|Reloading a file using a different encoding]] |
|
+ | ==Comments== |
||
− | ---- |
Latest revision as of 19:42, 11 September 2012
created 2006 · complexity basic · author Anatoli Sakhnik · version 6.0
I often need to edit files with different encodings, which can't be detected automatically (for example, how distinguish between cp866 and cp1251?). So when the desired file was loaded with a wrong encoding, I used to type:
:e ++enc=<what_was_really_needed> %:p
The following function makes it easier:
function! ChangeFileencoding() let encodings = ['cp1251', 'koi8-u', 'cp866'] let prompt_encs = [] let index = 0 while index < len(encodings) call add(prompt_encs, index.'. '.encodings[index]) let index = index + 1 endwhile let choice = inputlist(prompt_encs) if choice >= 0 && choice < len(encodings) execute 'e ++enc='.encodings[choice].' %:p' endif endf nmap <F8> :call ChangeFileencoding()<CR>
Here is a version of this function which wraps the encodings instead of prompting the user:
let g:enc_index = 0 function! ChangeFileencoding() let encodings = ['cp1251', 'koi8-u', 'cp866'] execute 'e ++enc='.encodings[g:enc_index].' %:p' if g:enc_index >=2 let g:enc_index = 0 else let g:enc_index = g:enc_index + 1 endif endf nmap <F8> :call ChangeFileencoding()<CR>
Further, it can be improved by populating the list of encodings from &fileencodings.
You can also configure Vim to set file encoding automatically from a modeline, which doesn't work as intended by default because the modeline takes effect after the file is read into memory, or you can install a plugin like AutoFenc to automatically detect the encoding in more situations.
See also[]
- Detect encoding from the charset specified in HTML files
- Reloading a file using a different encoding