Monday, November 5, 2012

Re: Sum up some line code

On 05/11/12 16:37, niva wrote:
> Hi,
>
> I apply this code in order to convert encoding format of a file.
>
> " Encode file
> exe "tabnew ".FilePath
> set encoding=utf-8
> set fileencoding=utf-8
> exe "w!"
> exe "bd!"
>
>
> Is it possible to do the same thing without opening it in vim and sum up some lines of code?
>
> Thank you
>

If you know the original encoding of the file (let's say KOI8-R) and are
on a system where the iconv program (not just the iconv library) is
installed, it's easy:

iconv -f KOI8-R -t UTF-8 < filein.txt > fileout.txt

If you want your "lines of code" to detect the original encoding the way
Vim's 'fileencodings' heuristics do, then I don't know.

In fact, when I don't know a file's encoding, I try to open it
(interactively) in gvim with ++enc (q.v.) and when it looks like text
and not gobbledygook I save it (for instance as UTF-8).


In any case you could simplify your code as follows:

First, set 'encoding' to UTF-8 once and for all in your vimrc, see
http://vim.wikia.com/wiki/Working_with_Unicode — that option should not
be modified in a running Vim because it might scramble the text of any
files already loaded. Then:

exe "tabnew" FilePath
w ++enc=utf-8
q

(where FilePath is a variable). The exclamation mark in w! is only
useful if the file is read-only, but not on a readonly media, and you
still want to overwrite it. Te one in bd! is not necessary since we just
wrote the file (which is therefore not 'modified').

Note that :q in my modified example quits the current window (and the
current tab since it contains no other windows), but not Vim (which was
already open before, with at least one other tab).

Or to change the filename:

:e inputfile.txt
:saveas ++enc=utf-8 outputfile.txt
:q


Best regards,
Tony.
--
The District of Columbia has a law forbidding you to exert pressure on
a balloon and thereby cause a whistling sound on the streets.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: