micro Text Editor

repository·master·Indexed 12 days ago

https://github.com/micro-editor/micro

A terminal-based text editor designed as a modern, feature-rich successor to nano. Distributed as a dependency-free static binary, micro supports multiple cursors, mouse support, and extensibility via Lua plugins. It includes a YAML-based syntax highlighting system and a comprehensive Lua API for interacting with the editor's UI, buffers, shell, and configuration.

Tokens
21.7K
Snippets
74
Records
105
Agent score
93%

What's inside micro

  1. Define syntax rules using patterns and regions

    master

    Syntax highlighting is defined under the rules key using two primary constructs:

    Patterns

    Patterns match text on a single line. The order of patterns matters: patterns defined lower in the file will overwrite those defined above them.

    Regions

    Regions highlight text between a start and end marker, often spanning multiple lines.

    • Nested Rules: Regions can contain their own rules. Inner rules are matched first, and their matches are skipped when searching for the region's end marker. This is critical for handling escaped characters (e.g., " inside a string).
    • Skip Regex: You can use the skip key to define regexes that should be ignored during the matching process.
    • Includes: You can embed other languages within a region using the include keyword (e.g., embedding javascript inside an HTML <script> tag). Note that nested includes (an include within an include) are not currently supported.
    - constant.string:
        start: "\""
        end: "\""
        rules:
            - constant.specialChar: "%."
            - constant.specialChar: "\\\\[abfnrtv'\"\\\\]"
    
    - comment:
        start: "//"
        end: "$"
        rules:
            - todo: "(TODO|XXX|FIXME):?"
  2. Access micro internal functions in Lua plugins

    master

    Micro exposes its internal functionality through Lua packages. You can import these packages using the import function. Once imported, you can access values and call methods using Lua syntax.

    Note on Syntax: While Go uses the dot (.) syntax for method calls, Lua plugins must use the colon (:) syntax to call methods on an object.

    Example: micro.InfoBar().Message() in Go becomes micro.InfoBar():Message() in Lua.

    local micro = import("micro")
    micro.Log("Hello")
  3. How the Linter plugin works

    master

    The Linter plugin runs a compiler or linter on your source code and parses the output to display error messages and line numbers directly within micro.

    Triggering the linter:

    • Automatic: The linter runs in the background every time the current buffer is saved, provided the filetype is supported.
    • Manual: You can manually trigger a linting pass by executing the > lint command within micro.

    By default, the plugin supports a wide range of languages including C (gcc), Go (go build, go vet), Python (flake8, mypy, pyflakes, pylint, ruff), Rust (cargo clippy), and many others.

  4. Set global vs local settings

    master

    Settings in micro can be applied in two ways:

    1. Global Settings: The default behavior. Settings applied globally affect all buffers.
    2. Local Settings: Settings that only apply to the current buffer. Use the setlocal command instead of set to apply an option locally.

    Important Exceptions:

    • The colorscheme option is global only.
    • The filetype option is local only.

    In the settings.json file, you can also define local settings for specific file types or file patterns (see 'Configure filetype and glob-specific settings' for details).

  5. Define filetype detection logic

    master

    To ensure Micro correctly identifies a file, use the detect block in your syntax file. You can use one or more of the following mechanisms:

    • filename: A regex matched against the filename. This takes precedence over header.
    • header: A regex matched against the first line of the file. Useful when extensions are missing (e.g., detecting YAML via a %YAML directive).
    • signature: An optional regex used to resolve ambiguities when multiple syntax files match a file (e.g., distinguishing C++ from C headers). Micro matches this against the first few lines (up to the detectlimit option).

    Precedence Order: filename > header. If both match, filename wins. If multiple files match, the one with a matching signature is preferred.

    detect:
        filename: "\\.ya?ml$"
        header: "%YAML"
  6. Configure pane-specific keybindings

    master

    You can scope keybindings to specific types of panes. This allows you to have different behavior when interacting with a text buffer versus the command bar or a terminal pane.

    Supported pane types:

    • buffer: The standard text editing pane.
    • command: The command bar used for entering commands.
    • terminal: A terminal pane.

    To apply a binding only to the command bar, wrap the binding in a command object within your configuration.

    {
        "command": {
            "Ctrl-w": "WordLeft"
        }
    }
  7. Understand Alt and modifier key syntax

    master

    Micro supports various ways to specify modifier keys in configuration:

    1. Alt Keys: Use Alt-key (e.g., Alt-a) or AltUp.
    2. Optional Hyphen: Micro supports an optional - between modifiers. Alt-ShiftLeft is equivalent to AltShiftLeft. Note that case matters for Alt bindings.
    3. Backspace Compatibility: On some older terminal emulators or Windows machines, use Ctrl-h for backspace if the standard Backspace key does not work as expected.
  8. How micro syntax files work

    master

    Micro uses YAML files to define syntax highlighting. Each file contains instructions for:

    1. Detection: Identifying the filetype via file extensions or the file header (the first line).
    2. Ambiguity Resolution: Using a signature to prioritize a specific filetype when multiple patterns match.
    3. Highlighting: Defining patterns and regions that are mapped to specific highlight groups.

    Note: When creating or porting syntax files, avoid using Nano's icolor syntax. Instead, use the case-insensitive flag (?i) within your regular expressions.

  9. Use the Command Prompt and Shell Commands

    master

    Micro provides a command prompt for executing internal editor commands and running external shell commands.

    • Open Command Prompt: Use Ctrl-e to open the prompt. You can then type commands (e.g., vsplit or hsplit to create splits).
    • Autocomplete: Press Tab within the command prompt to autocomplete commands.
    • Run Shell Command: Use Ctrl-b to run a shell command. Note that this will close micro while your command executes.
    Ctrl-e  # Open command prompt
    Tab      # Autocomplete in prompt
    Ctrl-b   # Run shell command
  10. Create a micro plugin

    master

    Micro plugins are implemented using Lua and must be placed in the ~/.config/micro/plug directory. Each plugin must reside in its own subdirectory and contain at least one Lua file and a repo.json file.

    A typical plugin directory structure looks like this:

    ~/.config/micro/plug/my-plugin/
        my-plugin.lua
        repo.json
        help/
            my-plugin.md

    The repo.json file

    The repo.json file provides metadata about the plugin, such as its name, website, description, version, and dependencies. This file is required for the plugin to be recognized.

    Runtime files

    Plugins can include additional runtime files that micro will automatically load. Supported types include:

    • Colorschemes
    • Syntax files
    • Help files
    • Plugin files
    • Syntax header files

    While no specific directory structure is enforced for these files, it is recommended to keep them in their own subdirectories within the plugin folder.

    {
      "name": "go-plugin",
      "website": "https://github.com/micro-editor/updated-plugins",
      "description": "A plugin for Go support",
      "version": "1.0.0"
    }