Skip to content

Nano and Less Customization

Guillaume Nodet edited this page May 12, 2025 · 6 revisions

ℹ️ Info: The JLine wiki content has been integrated into the new web site. The wiki won't be updated anymore.

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

Supplier<Path> workDir = () -> Paths.get("");
Builtins builtins = new Builtins(workDir, configPath, null);
SystemRegistry systemRegistry = new SystemRegistry(parser, terminal, workDir, configPath);
systemRegistry.setCommandRegistries(builtins);

LineReader reader = LineReaderBuilder.builder()
                    .terminal(terminal)
                    .completer(systemRegistry.completer())
                    .parser(parser)
                    .build();

builtins.setLineReader(reader);
while (true) {
    try {
        systemRegistry.cleanUp();         // delete temporary variables and reset output streams
        String line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
        Object result = systemRegistry.execute(line);
        .....
        .....
        .....
    }
    catch (UserInterruptException e) {
        // Ignore
    }
    catch (EndOfFileException e) {
        return;
    }
    catch (Exception e) {
        systemRegistry.trace(e);          // print exception and save it to console variable
    }
 }

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

Theme system

JLine version > 3.21.0

Theme system allows highlight rules to be specified in terms of token type names, mixins and parser configurations, instead of hard-coded colors in nanorc files. Optional theme system configuration is set by adding 'theme theme-system-file' command in jnanorc/jlessrc configuration files.

Theme system idea has been copied from YSakhno/nanorc and adopted to JLine.

Syntax highlight

JLine nanorc SyntaxHighlighter 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