Vim Tips Wiki
(Change to TipImported template + severe manual clean)
Line 39: Line 39:
Also see

Revision as of 14:26, 14 January 2008

Tip 1195 Printable Monobook Previous Next

created April 4, 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 expample, how to distinguish cp866 from 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
  let choice = inputlist(prompt_encs)
  if choice >= 0 && choice < len(encodings)
    execute 'e ++enc='.encodings[choice].' %:p'
nmap <f8> :call ChangeFileencoding()<cr>

Further, it can be improved by populating the list of encodings from &fileencodings.

To set file encoding automatically from a modeline, see VimTip911.


Also see