エディタVimの設定(備忘録)

リリースされた日本語版に関するもの

モデレータ: 暇人, YoN, nyu

返信する
アバター
YoN
Site Admin
記事: 1239
登録日時: 07/04/01(日) 08:00
お住まい: 信州

エディタVimの設定(備忘録)

投稿記事 by YoN »

エディタVimをパピーで使う時、cp932やeuc-jpなどのファイルを開くと文字化けします。文字化けを回避する設定例です。

コード: 全て選択

if &encoding !=# 'utf-8'
  set encoding=japan
  set fileencoding=japan
endif
if has('iconv')
  let s:enc_euc = 'euc-jp'
  let s:enc_jis = 'iso-2022-jp'

  if iconv("¥x87¥x64¥x87¥x6a", 'cp932', 'eucjp-ms') ==# "¥xad¥xc5¥xad¥xcb"
    let s:enc_euc = 'eucjp-ms'
    let s:enc_jis = 'iso-2022-jp-3'

  elseif iconv("¥x87¥x64¥x87¥x6a", 'cp932', 'euc-jisx0213') ==# "¥xad¥xc5¥xad¥xcb"
    let s:enc_euc = 'euc-jisx0213'
    let s:enc_jis = 'iso-2022-jp-3'
  endif

  if &encoding ==# 'utf-8'
    let s:fileencodings_default = &fileencodings
    let &fileencodings = s:enc_jis .','. s:enc_euc .',cp932'
    let &fileencodings = &fileencodings .','. s:fileencodings_default
    unlet s:fileencodings_default
  else
    let &fileencodings = &fileencodings .','. s:enc_jis
    set fileencodings+=utf-8,ucs-2le,ucs-2
    if &encoding =‾# '^¥(euc-jp¥|euc-jisx0213¥|eucjp-ms¥)$'
      set fileencodings+=cp932
      set fileencodings-=euc-jp
      set fileencodings-=euc-jisx0213
      set fileencodings-=eucjp-ms
      let &encoding = s:enc_euc
      let &fileencoding = s:enc_euc
    else
      let &fileencodings = &fileencodings .','. s:enc_euc
    endif
  endif

  unlet s:enc_euc
  unlet s:enc_jis
endif

if has('autocmd')
  function! AU_ReCheck_FENC()
    if &fileencoding =‾# 'iso-2022-jp' && search("[^¥x01-¥x7e]", 'n') == 0
      let &fileencoding=&encoding
    endif
  endfunction
  autocmd BufReadPost * call AU_ReCheck_FENC()
endif

set fileformats=unix,dos,mac

if exists('&ambiwidth')
  set ambiwidth=double
endif

set laststatus=2
set statusline=%<%f¥ %m%r%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l/%L,%v
上のファイルを /root/.vimrc と名前をつけて保存します。

次回、Vim起動時からeuc-jpなどのファイルが日本語端末urxvtで文字化けしないで表示、編集できます。画面下には2行のステータスラインも作られます。
[Puppy4.1.2-retro-JP] EPSON Endeavor MT-3500
Celeron 533MHz 256MB
[Lupu-5.0.1 LiveCD + lang_support_ja-1.4.sfs] SOTEC Whitney System CR Board
Celeron 766MHz 512MB
返信する