I can tell less to output characters in UTF-8:


But then it tries to read files as UTF-8 as well.

Can I tell it to read files as ISO-8859-2 (latin2) but display them as UTF-8 like I do in vim with enc and fenc settings?

  • You could write a preprocessor (passed via LESSOPEN) based on iconv, maybe? – L. Levrel Feb 25 '16 at 12:48
  • How would that look like? AFAIU I'd loose the ability of less to display streamed data as soon as it gets it, wouldn't I? I would have to wait for the preprocessor to finish converting the file? – cprn Feb 25 '16 at 20:13
  • 2
    For you last two questions, I think so. How would that look like? You should be able to figure it out from the "INPUT PREPROCESSOR" section of the manpage. I'd use two custom variables in the script, like IN_ENC and OUT_ENC, to be able to call e.g. IN_ENC=latin2 OUT_ENC=utf8 less my_file. Tell me if you want more help I'll try to write an answer. – L. Levrel Feb 26 '16 at 15:33
  • 1
    pipe through iconv -i latin2 -o utf-8. If you hate the extra buffering, look into stfbuf. – Arthur2e5 Mar 4 '16 at 11:15
  • 1
    @Cyprian: thanks for the offer. @Arthur2e5: the correct switches are -f and -t, respectively. – L. Levrel Mar 6 '16 at 21:15

It looks like it's not possible. As a partial workaround, you may use iconv as an input preprocessor in the following way.

  1. Create the following executable script, say less_conv.sh:
    iconv -f $IN_ENC -t $OUT_ENC $1
  2. Define and export the LESSOPEN variable:
    export LESSOPEN="|-less_conv.sh %s"
  3. Invoke less this way:
    IN_ENC=latin2 OUT_ENC=utf8 less somefile
    You may also set your preferred values:
    export IN_ENC=latin2
    export OUT_ENC=utf8
    less somefile

The pipe character | in LESSOPEN saves the need for a temporary intermediate file. The dash - enables this preprocessor when less reads standard input.

The preprocessor is invoked only once, even if you hit F or R, so you won't be able to use this workaround on growing files or "streaming" standard input.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.