Skip to content

Nano and Less Customization

mattirn edited this page Oct 17, 2019 · 5 revisions

Customization

Both nano and less commands can be customized using nanorc like configuration files: jnanorc and jlessrc.

Java

ConfigurationPath configPath = new ConfigurationPath(Paths.get("/pub/myApp"),   // application-wide settings
                       Paths.get(System.getProperty("user.home"), ".myApp"));   // user-specific settings

LineReader reader = LineReaderBuilder.builder()
                    .terminal(terminal)
                    .completer(completer)
                    .parser(parser)
                    .build();
while (true) {
    try {
        String line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
        .....
        .....
        .....
        ParsedLine pl = reader.getParser().parse(line, 0);
        String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]);
        if ("nano".equals(pl.word())) {
            Commands.nano(terminal, System.out, System.err,
                                Paths.get(""),
                                argv,
                                configPath);
        }
        else if ("less".equals(pl.word())) {
            Commands.less(terminal, System.in, System.out, System.err,
                                Paths.get(""),
                                argv,
                                configPath);
        }
        .....
        .....
        .....
    }
    catch (HelpException e) {
        HelpException.highlight(e.getMessage(), HelpException.defaultStyle()).print(terminal);
    }
    catch (UserInterruptException e) {
        // Ignore
    }
    catch (EndOfFileException e) {
        return;
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
    }
 }

Options

The jnanorc/jlessrc file contains the default settings for nano/less. During startup, the command try first read the user-specific settings and then its application-wide settings. Application-wide settings are read only if user-specific settings does not exists.

The configuration file accepts a series of set and unset commands, which can be used to configure nano/less on startup without using the command line options.

In addition configuration file might have 'include syntaxfile' commands to read self-contained color syntaxes from syntaxfile. Syntax file parameter can have '*' and '?' wildcards in its file name.

In a case that neither of application-wide nor user-specific settings are not found at command startup then syntaxfiles are searched from a standard installation location: /usr/share/nano.

Example: Configuration file jnanorc. Note that historylog is saved in user-specific directory and the file can be shared between nano and less applications.

include /usr/share/nano/*.nanorc
set tabstospaces
set autoindent
set tempfile
set historylog search.log

Syntax highlight

JLine nanorc syntax highlighter works with standard nanorc syntaxfiles.

JLine supported keywords: the syntax, color, and icolor are used to define syntax highlighting rules for different text patterns. Other nanorc keywords are quietly ignored.

Rebinding keys

Not supported.

Clone this wiki locally