Documentation
¶
Overview ¶
Package vimtea provides a Vim-like text editor component for terminal applications
Package vimtea provides a Vim-like text editor component for terminal applications ¶
Package vimtea provides a Vim-like text editor component for terminal applications ¶
Package vimtea provides a Vim-like text editor component for terminal applications ¶
Package vimtea provides a Vim-like text editor component for terminal applications ¶
Package vimtea provides a Vim-like text editor component for terminal applications built with Bubble Tea. It supports normal, insert, visual, and command modes with key bindings similar to Vim.
Package vimtea provides a Vim-like text editor component for terminal applications ¶
Package vimtea provides a Vim-like text editor component for terminal applications ¶
Package vimtea provides a Vim-like text editor component for terminal applications built with Bubble Tea (github.com/charmbracelet/bubbletea).
Features ¶
- Vim-like modal editing with normal, insert, visual, and command modes
- Familiar key bindings for Vim users (h,j,k,l navigation, d/y/p for delete/yank/paste, etc.)
- Command mode with colon commands
- Visual mode for selecting text
- Undo/redo functionality
- Line numbers (regular and relative)
- Syntax highlighting
- Customizable styles and themes
- Extensible key binding system
Getting Started ¶
Create a new editor with default settings:
editor := vimtea.NewEditor()
Or customize it with options:
editor := vimtea.NewEditor(
vimtea.WithContent("Initial content"),
vimtea.WithEnableStatusBar(true),
vimtea.WithDefaultSyntaxTheme("catppuccin-macchiato"),
vimtea.WithRelativeNumbers(true),
)
Use it in a Bubble Tea application:
p := tea.NewProgram(editor)
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
Extending Functionality ¶
Add custom key bindings:
editor.AddBinding(vimtea.KeyBinding{
Key: "ctrl+s",
Mode: vimtea.ModeNormal,
Description: "Save file",
Handler: func(buf vimtea.Buffer) tea.Cmd {
// Your save logic here
return nil
},
})
Add custom command:
editor.AddCommand("write", func(buf vimtea.Buffer, args []string) tea.Cmd {
// Your save logic here
return nil
})
Styling ¶
Customize the appearance with style options:
customStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#333333"))
editor := vimtea.NewEditor(
vimtea.WithTextStyle(customStyle),
vimtea.WithLineNumberStyle(numberStyle),
vimtea.WithCursorStyle(cursorStyle),
)
Package vimtea provides a Vim-like text editor component for terminal applications
Index ¶
- func QuitCmd(buf Buffer) tea.Cmd
- func SetStatusMsg(msg string) tea.Cmd
- type BindingRegistry
- func (r *BindingRegistry) Add(key string, cmd Command, mode EditorMode, help string)
- func (r *BindingRegistry) FindExact(keySeq string, mode EditorMode) *internalKeyBinding
- func (r *BindingRegistry) GetAll() []internalKeyBinding
- func (r *BindingRegistry) GetForMode(mode EditorMode) []internalKeyBinding
- func (r *BindingRegistry) IsPrefix(keySeq string, mode EditorMode) bool
- type Buffer
- type Command
- type CommandFn
- type CommandMsg
- type CommandRegistry
- type Cursor
- type Editor
- type EditorMode
- type EditorModeMsg
- type EditorOption
- func WithBlinkInterval(interval time.Duration) EditorOption
- func WithCommandStyle(style lipgloss.Style) EditorOption
- func WithContent(content string) EditorOption
- func WithCurrentLineNumberStyle(style lipgloss.Style) EditorOption
- func WithCursorStyle(style lipgloss.Style) EditorOption
- func WithDefaultSyntaxTheme(theme string) EditorOption
- func WithEnableModeCommand(enable bool) EditorOption
- func WithEnableStatusBar(enable bool) EditorOption
- func WithFileName(fileName string) EditorOption
- func WithFullScreen() EditorOption
- func WithHideLineNumbers() EditorOption
- func WithLineNumberStyle(style lipgloss.Style) EditorOption
- func WithRelativeNumbers(enable bool) EditorOption
- func WithSelectedStyle(style lipgloss.Style) EditorOption
- func WithStatusStyle(style lipgloss.Style) EditorOption
- func WithTextStyle(style lipgloss.Style) EditorOption
- type KeyBinding
- type QuitMsg
- type TextRange
- type UndoRedoMsg
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func QuitCmd ¶
QuitCmd matches the KeyBinding.Handler signature and returns a tea.Cmd that dispatches a QuitMsg carrying the current buffer text. Use it when wiring a quit key (typically esc in ModeNormal) so parent models can react.
editor.AddBinding(vimtea.KeyBinding{
Key: "esc",
Mode: vimtea.ModeNormal,
Handler: vimtea.QuitCmd,
})
func SetStatusMsg ¶
SetStatusMsg creates a command that sets the status message This can be used by external components to update the editor's status message
Types ¶
type BindingRegistry ¶
type BindingRegistry struct {
// contains filtered or unexported fields
}
BindingRegistry manages key bindings for the editor It supports exact matches and prefix detection for multi-key sequences
func (*BindingRegistry) Add ¶
func (r *BindingRegistry) Add(key string, cmd Command, mode EditorMode, help string)
Add registers a new key binding with the registry It automatically builds prefix maps for multi-key sequences
func (*BindingRegistry) FindExact ¶
func (r *BindingRegistry) FindExact(keySeq string, mode EditorMode) *internalKeyBinding
FindExact looks for an exact match for the given key sequence in the specified mode It can handle numeric prefixes by ignoring them when looking for the command
func (*BindingRegistry) GetAll ¶
func (r *BindingRegistry) GetAll() []internalKeyBinding
GetAll returns all registered key bindings
func (*BindingRegistry) GetForMode ¶
func (r *BindingRegistry) GetForMode(mode EditorMode) []internalKeyBinding
GetForMode returns all key bindings for the specified mode
func (*BindingRegistry) IsPrefix ¶
func (r *BindingRegistry) IsPrefix(keySeq string, mode EditorMode) bool
IsPrefix checks if the key sequence is a prefix of any registered binding This is used to determine if we should wait for more input
type Buffer ¶
type Buffer interface {
// Text returns the entire buffer content as a string
Text() string
// Lines returns all lines in the buffer as a string slice
Lines() []string
// LineCount returns the number of lines in the buffer
LineCount() int
// LineLength returns the length of the line at the given row
LineLength(row int) int
// VisualLineLength returns the visual length of the line at the given row
// counting tabs as tabWidth spaces
VisualLineLength(row int) int
// InsertAt inserts text at the specified position
InsertAt(row, col int, text string)
// DeleteAt deletes text between the specified positions
DeleteAt(startRow, startCol, endRow, endCol int)
// Undo reverts the last change and returns a command with the new cursor position
Undo() tea.Cmd
// Redo reapplies a previously undone change
Redo() tea.Cmd
// CanUndo returns whether there are changes that can be undone
CanUndo() bool
// CanRedo returns whether there are changes that can be redone
CanRedo() bool
// Clear removes all content from the buffer and resets to empty state
Clear() tea.Cmd
}
Buffer defines the interface for text buffer operations It provides methods for manipulating text content and undo/redo functionality
type Command ¶
Command is a function that performs an action on the editor model and returns a bubbletea command
type CommandFn ¶
CommandFn is a function that can be executed when a command is run in command mode It takes a buffer reference and command arguments, and returns a bubbletea command
type CommandMsg ¶
type CommandMsg struct {
Command string // Command name without arguments
}
CommandMsg is sent when a command is executed from command mode It contains the command name that should be looked up in the CommandRegistry
type CommandRegistry ¶
type CommandRegistry struct {
// contains filtered or unexported fields
}
CommandRegistry stores and manages commands that can be executed in command mode Commands are invoked by typing ":command" in command mode
func (*CommandRegistry) Get ¶
func (r *CommandRegistry) Get(name string) Command
Get retrieves a command by name, returning nil if not found
func (*CommandRegistry) GetAll ¶
func (r *CommandRegistry) GetAll() map[string]Command
GetAll returns all registered commands as a map
func (*CommandRegistry) Register ¶
func (r *CommandRegistry) Register(name string, cmd Command)
Register adds a command to the registry with the given name
type Editor ¶
type Editor interface {
// Implements the bubbletea.Model interface
tea.Model
// AddBinding registers a new key binding
AddBinding(binding KeyBinding)
// AddCommand registers a new command that can be executed in command mode
AddCommand(name string, cmd CommandFn)
// GetBuffer returns the current buffer
GetBuffer() Buffer
// GetMode returns the current editor mode
GetMode() EditorMode
// SetMode changes the current editor mode
SetMode(mode EditorMode) tea.Cmd
// SetStatusMessage sets the status message displayed in the status bar
SetStatusMessage(msg string) tea.Cmd
// SetSize updates the editor's dimensions when the terminal window is resized
SetSize(width, height int) (tea.Model, tea.Cmd)
// Tick sends a tick message to the editor
Tick() tea.Cmd
// Reset restores the editor to its initial state
Reset() tea.Cmd
}
Editor defines the interface for interacting with the editor component
func NewEditor ¶
func NewEditor(opts ...EditorOption) Editor
NewEditor creates a new editor instance with the provided options
type EditorMode ¶
type EditorMode int
EditorMode represents the current mode of the editor
const ( // ModeNormal is the default mode for navigation and commands ModeNormal EditorMode = iota // ModeInsert is for inserting and editing text ModeInsert // ModeVisual is for selecting text ModeVisual // ModeCommand is for entering commands with a colon prompt ModeCommand )
func (EditorMode) String ¶
func (m EditorMode) String() string
String returns the string representation of the editor mode
type EditorModeMsg ¶
type EditorModeMsg struct {
Mode EditorMode
}
type EditorOption ¶
type EditorOption func(*options)
EditorOption is a function that modifies the editor options
func WithBlinkInterval ¶
func WithBlinkInterval(interval time.Duration) EditorOption
WithBlinkInterval sets the cursor blink interval duration
func WithCommandStyle ¶
func WithCommandStyle(style lipgloss.Style) EditorOption
WithCommandStyle sets the style for the command line
func WithContent ¶
func WithContent(content string) EditorOption
WithContent sets the initial content for the editor
func WithCurrentLineNumberStyle ¶
func WithCurrentLineNumberStyle(style lipgloss.Style) EditorOption
WithCurrentLineNumberStyle sets the style for the current line number
func WithCursorStyle ¶
func WithCursorStyle(style lipgloss.Style) EditorOption
WithCursorStyle sets the style for the cursor
func WithDefaultSyntaxTheme ¶
func WithDefaultSyntaxTheme(theme string) EditorOption
WithDefaultSyntaxTheme sets the syntax highlighting theme Available themes include "catppuccin-macchiato" and others
func WithEnableModeCommand ¶
func WithEnableModeCommand(enable bool) EditorOption
WithEnableModeCommand enables or disables command mode (:commands)
func WithEnableStatusBar ¶
func WithEnableStatusBar(enable bool) EditorOption
WithEnableStatusBar enables or disables the status bar at the bottom
func WithFileName ¶
func WithFileName(fileName string) EditorOption
WithFileName sets the filename for syntax highlighting
func WithFullScreen ¶
func WithFullScreen() EditorOption
func WithHideLineNumbers ¶
func WithHideLineNumbers() EditorOption
WithHideLineNumbers hides the entire line-number gutter. Useful when the editor is embedded as a lightweight single/multi-line input widget where the gutter would look out of place.
func WithLineNumberStyle ¶
func WithLineNumberStyle(style lipgloss.Style) EditorOption
WithLineNumberStyle sets the style for line numbers
func WithRelativeNumbers ¶
func WithRelativeNumbers(enable bool) EditorOption
WithRelativeNumbers enables or disables relative line numbering When enabled, line numbers show the distance from the current line
func WithSelectedStyle ¶
func WithSelectedStyle(style lipgloss.Style) EditorOption
WithSelectedStyle sets the style for selected text
func WithStatusStyle ¶
func WithStatusStyle(style lipgloss.Style) EditorOption
WithStatusStyle sets the style for the status bar
func WithTextStyle ¶
func WithTextStyle(style lipgloss.Style) EditorOption
WithTextStyle sets the style for regular text
type KeyBinding ¶
type KeyBinding struct {
Key string // The key sequence to bind (e.g. "j", "dd", "ctrl+f")
Mode EditorMode // Which editor mode this binding is active in
Description string // Human-readable description for help screens
Handler func(Buffer) tea.Cmd // Function to execute when the key is pressed
}
KeyBinding represents a key binding that can be registered with the editor This is the public API for adding key bindings
type QuitMsg ¶
type QuitMsg struct {
Content string
}
QuitMsg is emitted by QuitCmd to signal the embedding parent model that the user is done editing. The parent is responsible for consuming this message (e.g. closing the editor modal) and reading Content.
type TextRange ¶
type TextRange struct {
Start Cursor // Starting position (inclusive)
End Cursor // Ending position (inclusive)
}
TextRange represents a range of text with start and end positions
type UndoRedoMsg ¶
type UndoRedoMsg struct {
NewCursor Cursor // New cursor position after undo/redo
Success bool // Whether the operation succeeded
IsUndo bool // True for undo, false for redo
}
UndoRedoMsg is sent when an undo or redo operation is performed It contains the new cursor position and operation status
