特定のエンコードのファイルを読み込む
作者: 小見 拓
—
最終変更
2012年01月08日 12時10分
特定のエンコードのファイルを読み込む
- ファイルを読込後、エンコーディングを変換する
:let inputfile = "test.txt" :let lines = readfile(inputfile) :let i = 0 :for str in lines :let lines[i] = iconv(str, "utf-8", "cp932") :let i += 1 :endfor :echo join(lines, "n") "# => utf-8エンコードのファイルの読み込み、cp932エンコードに変換後、出力する
- &encodingでvimエディタ内部で使用しているエンコーディングを取得できる
:let inputfile = "test.txt" :let lines = readfile(inputfile) :let i = 0 :for str in lines :let lines[i] = iconv(str, "utf-8", &encoding) :let i += 1 :endfor :echo join(lines, "n") "# => utf-8エンコードのファイルの読み込み、vimエディタ内部で使用しているエンコーディングに変換して出力
- リストの一括変換を行うなら、mapを使用すると良い
:let inputfile = "test.txt" :let lines = readfile(inputfile) :call map(lines, 'iconv(v:val, "utf-8", &encoding)') :echo join(lines, "n") "# => utf-8エンコードのファイルの読み込み、vimエディタ内部で使用しているエンコーディングに変換して出力
- cp932の文字列を文字化けしないように読み込む
:let cp932text = ..... :echo iconv(cp932text, "cp932", &encoding)
- euc-jpの文字列を文字化けしないように読み込む
:let eucjptext = ..... :echo iconv(eucjptext, "euc-jp", &encoding)
- utf-8の文字列を文字化けしないように読み込む
:let utf8text = ..... :echo iconv(utf8text, "utf-8", &encoding)
- エンコードを指定して、バッファに読み込む
:let inputfile = "test.txt" :execute ":e ++enc=utf-8 " . inputfile "# => utf-8エンコードのファイルをバッファに読み込む
Recent Comments
ありがとうございます!
http://nanasi.jp/articles/howto/editing/visualcursor-endtoend.html · 8 years ago
知りませんでした。有難うございました。
http://nanasi.jp/articles/howto/file/open-with-format.html · 9 years ago
<c-f>1ページ分、下にスクロールする<c-b>1ページ分、上にスクロールする
どっちも逆です。
http://nanasi.jp/articles/howto/user-manual/user-manual-motion.html · 10 years ago
set 使用時に : で閉じるのを忘れて右往左往してました。
http://nanasi.jp/articles/howto/file/modeline.html · 11 years ago
やっぱり日本語の方が早いっす。
http://nanasi.jp/articles/howto/help/help_ja.html · 12 years ago