5

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

export LESSCHARSET=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?

7
  • You could write a preprocessor (passed via LESSOPEN) based on iconv, maybe? Commented Feb 25, 2016 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? Commented Feb 25, 2016 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. Commented Feb 26, 2016 at 15:33
  • 1
    pipe through iconv -i latin2 -o utf-8. If you hate the extra buffering, look into stfbuf. Commented Mar 4, 2016 at 11:15
  • 1
    @Cyprian: thanks for the offer. @Arthur2e5: the correct switches are -f and -t, respectively. Commented Mar 6, 2016 at 21:15

1 Answer 1

1

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:
    #!/bin/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.

Limitation:
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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.