Wezterm Lua Example

6 min read Oct 15, 2024
Wezterm Lua Example

wezterm lua examples: Customize Your Terminal Experience

wezterm, the modern and customizable terminal emulator, offers a powerful Lua scripting engine for extending its functionality and tailoring it to your specific needs. This allows you to go beyond basic configuration and create a truly personalized terminal environment.

If you're new to Lua scripting, or just want to see some examples of what's possible, this article will guide you through some common wezterm Lua customization scenarios.

Getting Started with Lua in wezterm

To use Lua scripting in wezterm, you need to create a Lua file in your wezterm configuration directory. By default, this directory is usually located at:

  • Linux: ~/.config/wezterm
  • macOS: ~/Library/Application Support/wezterm
  • Windows: %LOCALAPPDATA%\wezterm

Create a file named lua/init.lua within this directory. This is the primary file where you'll write your Lua code.

Example 1: Setting a Custom Background Color

Let's start with a simple example: setting a custom background color for your terminal.

-- Set background color to a dark blue
local colors = require 'wezterm.color'
wezterm.on_config_changed(function()
    wezterm.window.set_background_color(colors.hsl(200, 50, 20))
end)

Explanation:

  • wezterm.color: This module provides functions to work with color values in wezterm.
  • wezterm.window.set_background_color: This function sets the background color of the terminal window.
  • colors.hsl(200, 50, 20): This function creates a HSL color with the specified hue (200), saturation (50), and lightness (20).

Example 2: Auto-Launching a Command

You can use Lua to automate tasks like launching specific commands when wezterm starts.

-- Launch `top` command when wezterm starts
wezterm.on_start(function()
    wezterm.active_pane().send_command('top')
end)

Explanation:

  • wezterm.on_start: This function executes the provided code when wezterm starts.
  • wezterm.active_pane().send_command('top'): This sends the top command to the active pane in the terminal.

Example 3: Adding a Custom Keybinding

Keybindings allow you to perform actions with just a few keystrokes.

-- Bind Ctrl+Shift+R to restart wezterm
wezterm.on_config_changed(function()
    wezterm.register_key_binding('Ctrl+Shift+R', function()
        wezterm.restart()
    end)
end)

Explanation:

  • wezterm.register_key_binding: This function registers a new keybinding.
  • 'Ctrl+Shift+R': This is the key combination you want to bind.
  • wezterm.restart(): This function restarts wezterm.

Example 4: Manipulating Tabs

Lua lets you control the tab management in your terminal.

-- Create a new tab with a specific pane layout
wezterm.on_config_changed(function()
    local new_tab = wezterm.window.create_tab(
        {
            { pane_id = 1, pane_size = { rows = 50, cols = 50 } }, 
            { pane_id = 2, pane_size = { rows = 50, cols = 50 } }, 
        },
        { direction = "horizontal" }
    )
end)

Explanation:

  • wezterm.window.create_tab: Creates a new tab with the specified configuration.
  • pane_id: Identifies each pane within the tab.
  • pane_size: Defines the dimensions of each pane.
  • direction: Sets the layout of the panes (horizontal or vertical).

Advanced wezterm Lua Examples

The examples above are just a glimpse of what's possible with wezterm Lua. You can explore more advanced scenarios like:

  • Custom Themes: Define custom color schemes for different terminal modes.
  • Dynamic Pane Management: Create and resize panes based on user input.
  • Integrating with External Tools: Interact with other applications and services.

Finding More wezterm Lua Examples

For more inspiration and deeper dives into advanced wezterm Lua scripting, check the following resources:

  • Official wezterm Documentation:
  • GitHub Repository:
  • Community Forums: Search for wezterm Lua examples on online forums and communities.

Conclusion

wezterm Lua scripting provides a powerful platform to tailor your terminal experience to your specific needs. From simple color customizations to complex automation workflows, the possibilities are endless. Start exploring the world of wezterm Lua and create the terminal of your dreams!