How can I see which encoding is used in a file? (查看文件编码格式)
- vi模式下
查看
:set fileencoding
设置
:set fileencoding xxx
- file命令:
- check a file encoding
file -bi [filename]
Option | Description |
---|---|
-b, --brief | Don’t print filename (brief mode) |
-i, --mime | Print filetype and encoding |
- change a file fileencoding
$ iconv -f [encoding] -t [encoding] -o [newfilename] [filename]
Option | Description |
---|---|
-f, --from-code | Convert a file’s encoding from charset |
-t, --to-code | Convert a file’s encoding to charset |
-o, --output | Specify output file (instead of stdout) |
Change a file’s encoding from CP1251 (Windows-1251, Cyrillic) charset to UTF-8:
$ iconv -f cp1251 -t utf-8 in.txt
Change a file’s encoding from ISO-8859-1 charset to and save it to out.txt:
$ iconv -f iso-8859-1 -t utf-8 -o out.txt in.txt
Change a file’s encoding from ASCII to UTF-8:
$ iconv -f utf-8 -t ascii -o out.txt in.txt
Change a file’s encoding from UTF-8 charset to ASCII:
llegal input sequence at position: As UTF-8 can contain characters that can’t be encoded with ASCII, the iconv will generate the error message “illegal input sequence at position” unless you tell it to strip all non-ASCII characters using the -c option.
$ iconv -c -f utf-8 -t ascii -o out.txt in.txt
You can lose characters: Note that if you use the iconv with the -c option, nonconvertible characters will be lost.
- List All charsets
List all the known charsets in your Linux system:
iconv -l