Help us shape the future of Go! Take the 2026 Go Developer Survey

vim

package
v0.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package vim provides vim-style modal editing for BubbleTea text input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer provides vim-style operations on a text buffer.

func NewBuffer

func NewBuffer(content string) *Buffer

NewBuffer creates a new buffer from text content.

func (*Buffer) CharUnderCursor

func (b *Buffer) CharUnderCursor() rune

CharUnderCursor returns the character under the cursor.

func (*Buffer) Content

func (b *Buffer) Content() string

Content returns the buffer content as a single string.

func (*Buffer) CurrentLine

func (b *Buffer) CurrentLine() string

CurrentLine returns the current line.

func (*Buffer) CurrentLineLen

func (b *Buffer) CurrentLineLen() int

CurrentLineLen returns the length of the current line.

func (*Buffer) Cursor

func (b *Buffer) Cursor() Position

Cursor returns the current cursor position.

func (*Buffer) CursorOffset

func (b *Buffer) CursorOffset() int

CursorOffset returns the absolute offset of the cursor in the content.

func (*Buffer) DeleteChar

func (b *Buffer) DeleteChar() string

DeleteChar deletes the character under the cursor.

func (*Buffer) DeleteCharBefore

func (b *Buffer) DeleteCharBefore() string

DeleteCharBefore deletes the character before the cursor (backspace).

func (*Buffer) DeleteLine

func (b *Buffer) DeleteLine() string

DeleteLine deletes the current line.

func (*Buffer) DeleteRange

func (b *Buffer) DeleteRange(start, end Position) string

DeleteRange deletes text between two positions.

func (*Buffer) DeleteToEndOfLine

func (b *Buffer) DeleteToEndOfLine() string

DeleteToEndOfLine deletes from cursor to end of line.

func (*Buffer) FirstNonBlank

func (b *Buffer) FirstNonBlank()

FirstNonBlank moves cursor to first non-blank character on current line.

func (*Buffer) Insert

func (b *Buffer) Insert(text string)

Insert inserts text at the current cursor position.

func (*Buffer) JoinLines

func (b *Buffer) JoinLines()

JoinLines joins the current line with the next.

func (*Buffer) LineCount

func (b *Buffer) LineCount() int

LineCount returns the number of lines.

func (*Buffer) Lines

func (b *Buffer) Lines() []string

Lines returns the buffer lines.

func (*Buffer) NewLineAbove

func (b *Buffer) NewLineAbove()

NewLineAbove inserts a new line above and moves cursor.

func (*Buffer) NewLineBelow

func (b *Buffer) NewLineBelow()

NewLine inserts a new line below and moves cursor.

func (*Buffer) Paste

func (b *Buffer) Paste()

Paste inserts yanked text after the cursor.

func (*Buffer) PasteBefore

func (b *Buffer) PasteBefore()

PasteBefore inserts yanked text before the cursor.

func (*Buffer) ReplaceChar

func (b *Buffer) ReplaceChar(r rune)

ReplaceChar replaces the character under cursor.

func (*Buffer) SetContent

func (b *Buffer) SetContent(content string)

SetContent replaces the entire buffer content.

func (*Buffer) SetCursor

func (b *Buffer) SetCursor(pos Position)

SetCursor sets the cursor position with bounds checking. In normal mode, cursor is clamped to lineLen-1 (on last char).

func (*Buffer) SetCursorFromOffset

func (b *Buffer) SetCursorFromOffset(offset int)

SetCursorFromOffset sets cursor position from absolute offset.

func (*Buffer) SetCursorInsert

func (b *Buffer) SetCursorInsert(pos Position)

SetCursorInsert sets cursor position for insert mode (allows col == lineLen).

func (*Buffer) SetYank

func (b *Buffer) SetYank(content string)

SetYank sets the yank register content.

func (*Buffer) Yank

func (b *Buffer) Yank() string

Yank returns the current yank register content.

func (*Buffer) YankLine

func (b *Buffer) YankLine() string

YankLine copies the current line to the yank register.

func (*Buffer) YankRange

func (b *Buffer) YankRange(start, end Position) string

YankRange copies text between two positions to the yank register.

type Editor

type Editor struct {
	// contains filtered or unexported fields
}

Editor is a vim-style text editor component.

func NewEditor

func NewEditor(content string) *Editor

NewEditor creates a new vim editor with the given content.

func (*Editor) CommandBuffer

func (e *Editor) CommandBuffer() string

CommandBuffer returns the current command buffer (for : commands).

func (*Editor) Content

func (e *Editor) Content() string

Content returns the current buffer content.

func (*Editor) Cursor

func (e *Editor) Cursor() Position

Cursor returns the current cursor position.

func (*Editor) CursorOffset

func (e *Editor) CursorOffset() int

CursorOffset returns the absolute cursor offset.

func (*Editor) EnsureCursorVisible

func (e *Editor) EnsureCursorVisible()

EnsureCursorVisible adjusts scroll offset to keep cursor visible.

func (*Editor) IsCommandMode

func (e *Editor) IsCommandMode() bool

IsCommandMode returns true if in command mode.

func (*Editor) Mode

func (e *Editor) Mode() Mode

Mode returns the current vim mode.

func (*Editor) SetContent

func (e *Editor) SetContent(content string)

SetContent replaces the buffer content.

func (*Editor) SetCursorInsert

func (e *Editor) SetCursorInsert(pos Position)

SetCursorInsert sets the cursor position for insert mode (allows col == lineLen).

func (*Editor) SetSize

func (e *Editor) SetSize(width, height int)

SetSize sets the editor dimensions.

func (*Editor) SetSyntax

func (e *Editor) SetSyntax(s *SyntaxStyler)

SetSyntax enables syntax highlighting for the editor content.

func (*Editor) State

func (e *Editor) State() *State

State returns the current vim state (for external inspection).

func (*Editor) Update

func (e *Editor) Update(msg tea.KeyMsg) (cmd string, quit bool)

Update handles key events and returns the updated editor.

func (*Editor) View

func (e *Editor) View(cfg ViewConfig) string

View renders the editor content with cursor and selection highlighting.

func (*Editor) VisualSelection

func (e *Editor) VisualSelection() (start, end Position, active bool)

VisualSelection returns the visual selection range, if in visual mode.

type Mode

type Mode int

Mode represents the current vim editing mode.

const (
	// ModeNormal is the default mode for navigation and commands.
	ModeNormal Mode = iota
	// ModeInsert allows text entry.
	ModeInsert
	// ModeVisual allows character-wise selection.
	ModeVisual
	// ModeVisualLine allows line-wise selection.
	ModeVisualLine
	// ModeCommand is for ex-style commands (e.g., :wq).
	ModeCommand
)

func (Mode) String

func (m Mode) String() string

String returns the string representation of the mode.

type Motion

type Motion struct {
	Start    Position
	End      Position
	Linewise bool // True for line-based motions (dd, yy, etc.)
}

Motion represents the result of a motion command.

func FindCharBackward

func FindCharBackward(b *Buffer, char rune, count int, till bool) Motion

FindCharBackward finds the count-th occurrence of char backward.

func FindCharForward

func FindCharForward(b *Buffer, char rune, count int, till bool) Motion

FindCharForward finds the count-th occurrence of char forward.

func MatchBracket

func MatchBracket(b *Buffer) Motion

MatchBracket implements vim's % motion: jump to matching bracket.

func MoveBigWordBackward

func MoveBigWordBackward(b *Buffer, count int) Motion

MoveBigWordBackward moves cursor backward by count WORDs.

func MoveBigWordEnd

func MoveBigWordEnd(b *Buffer, count int) Motion

MoveBigWordEnd moves cursor to end of current/next WORD.

func MoveBigWordForward

func MoveBigWordForward(b *Buffer, count int) Motion

MoveBigWordForward moves cursor forward by count WORDs (whitespace-delimited).

func MoveDown

func MoveDown(b *Buffer, count int) Motion

MoveDown moves cursor down by count lines.

func MoveLeft

func MoveLeft(b *Buffer, count int) Motion

MoveLeft moves cursor left by count characters.

func MoveParagraphBackward

func MoveParagraphBackward(b *Buffer, count int) Motion

MoveParagraphBackward moves to start of previous paragraph.

func MoveParagraphForward

func MoveParagraphForward(b *Buffer, count int) Motion

MoveParagraphForward moves to start of next paragraph.

func MoveRight

func MoveRight(b *Buffer, count int) Motion

MoveRight moves cursor right by count characters.

func MoveToDocumentEnd

func MoveToDocumentEnd(b *Buffer) Motion

MoveToDocumentEnd moves cursor to last line of document.

func MoveToDocumentStart

func MoveToDocumentStart(b *Buffer) Motion

MoveToDocumentStart moves cursor to start of document.

func MoveToFirstNonBlank

func MoveToFirstNonBlank(b *Buffer) Motion

MoveToFirstNonBlank moves cursor to first non-blank character.

func MoveToLine

func MoveToLine(b *Buffer, lineNum int) Motion

MoveToLine moves cursor to a specific line number (1-indexed).

func MoveToLineEnd

func MoveToLineEnd(b *Buffer) Motion

MoveToLineEnd moves cursor to end of line.

func MoveToLineStart

func MoveToLineStart(b *Buffer) Motion

MoveToLineStart moves cursor to start of line (column 0).

func MoveUp

func MoveUp(b *Buffer, count int) Motion

MoveUp moves cursor up by count lines.

func MoveWordBackward

func MoveWordBackward(b *Buffer, count int) Motion

MoveWordBackward moves cursor backward by count words.

func MoveWordEnd

func MoveWordEnd(b *Buffer, count int) Motion

MoveWordEnd moves cursor to end of current/next word.

func MoveWordForward

func MoveWordForward(b *Buffer, count int) Motion

MoveWordForward moves cursor forward by count words.

type Operator

type Operator int

Operator represents a pending vim operator (d, c, y, etc.).

const (
	OpNone Operator = iota
	OpDelete
	OpChange
	OpYank
)

func (Operator) String

func (o Operator) String() string

String returns the string representation of the operator.

type Position

type Position struct {
	Line int
	Col  int
}

Position represents a cursor position in the buffer.

type State

type State struct {
	Mode          Mode
	PendingOp     Operator
	Count         int  // Numeric count prefix (e.g., 3dw)
	PendingMotion bool // Waiting for a motion after operator
	FindChar      rune // Character for f/F/t/T motions
	FindForward   bool // Direction for find
	FindTill      bool // Stop before (t/T) vs on (f/F) the character
	LastFindChar  rune // Last character used in f/F/t/T
	LastFindFwd   bool
	LastFindTill  bool
	VisualStart   int // Start position for visual mode
	CommandBuffer string

	// Multi-key sequence state
	PendingKey      rune     // For g (gg), z (zz), etc.
	AwaitingChar    bool     // Waiting for character input (f/F/t/T)
	AwaitingReplace bool     // Waiting for replacement character (r)
	AwaitingTextObj bool     // Waiting for text object type (diw, ci", etc.)
	TextObjInner    bool     // Inner vs around for text objects
	TextObjOperator Operator // Operator to apply with text object
}

State holds the current vim editing state.

func NewState

func NewState() *State

NewState creates a new vim state in normal mode.

func (*State) AccumulateCount

func (s *State) AccumulateCount(digit int)

AccumulateCount accumulates a digit into the count prefix.

func (*State) ClearOperator

func (s *State) ClearOperator()

ClearOperator clears the pending operator.

func (*State) EnterCommand

func (s *State) EnterCommand()

EnterCommand switches to command mode.

func (*State) EnterInsert

func (s *State) EnterInsert()

EnterInsert switches to insert mode.

func (*State) EnterVisual

func (s *State) EnterVisual(cursorPos int)

EnterVisual switches to visual mode.

func (*State) EnterVisualLine

func (s *State) EnterVisualLine(cursorPos int)

EnterVisualLine switches to visual line mode.

func (*State) GetCount

func (s *State) GetCount() int

GetCount returns the current count and resets it.

func (*State) HasPendingOperator

func (s *State) HasPendingOperator() bool

HasPendingOperator returns true if there's a pending operator.

func (*State) IsVisual

func (s *State) IsVisual() bool

IsVisual returns true if in any visual mode.

func (*State) Reset

func (s *State) Reset()

Reset clears pending operations and returns to normal mode.

func (*State) SetOperator

func (s *State) SetOperator(op Operator)

SetOperator sets a pending operator and waits for motion.

func (*State) VisualRange

func (s *State) VisualRange(cursorPos int) (start, end int)

VisualRange returns the start and end of the visual selection. The returned range is always ordered (start <= end).

type SyntaxStyler

type SyntaxStyler struct {
	// contains filtered or unexported fields
}

SyntaxStyler tokenizes content and provides per-offset styles for rendering. It caches results and only re-tokenizes when content changes.

func NewMarkdownStyler

func NewMarkdownStyler() *SyntaxStyler

NewMarkdownStyler creates a SyntaxStyler configured for markdown.

func (*SyntaxStyler) StyleAt

func (s *SyntaxStyler) StyleAt(offset int) lipgloss.Style

StyleAt returns the style for the character at the given byte offset.

func (*SyntaxStyler) TokenAt

func (s *SyntaxStyler) TokenAt(offset int) chroma.TokenType

TokenAt returns the chroma token type at the given byte offset. Returns -1 for out-of-bounds offsets.

func (*SyntaxStyler) Update

func (s *SyntaxStyler) Update(content string, defaultStyle lipgloss.Style)

Update re-tokenizes content if it changed since the last call.

type TextObject

type TextObject struct {
	Start Position
	End   Position
	Found bool
}

TextObject represents a text selection from a text object.

func AroundBracket

func AroundBracket(b *Buffer, open, close rune) TextObject

AroundBracket selects content including brackets (a(, a), ab, a{, a}, aB, a[, a]).

func AroundQuote

func AroundQuote(b *Buffer, quote rune) TextObject

AroundQuote selects content including quotes (a" or a').

func AroundWord

func AroundWord(b *Buffer) TextObject

AroundWord selects the word under cursor plus surrounding whitespace (aw).

func InnerBracket

func InnerBracket(b *Buffer, open, close rune) TextObject

InnerBracket selects content inside brackets (i(, i), ib, i{, i}, iB, i[, i]).

func InnerQuote

func InnerQuote(b *Buffer, quote rune) TextObject

InnerQuote selects content inside quotes (i" or i').

func InnerWord

func InnerWord(b *Buffer) TextObject

InnerWord selects the word under cursor (iw).

type UndoEntry

type UndoEntry struct {
	Content string   // Buffer content before the change
	Cursor  Position // Cursor position before the change
}

UndoEntry represents a single undoable change.

type UndoStack

type UndoStack struct {
	// contains filtered or unexported fields
}

UndoStack manages undo/redo history.

func NewUndoStack

func NewUndoStack(maxSize int) *UndoStack

NewUndoStack creates a new undo stack with the given maximum size.

func (*UndoStack) CanRedo

func (s *UndoStack) CanRedo() bool

CanRedo returns true if there are entries to redo.

func (*UndoStack) CanUndo

func (s *UndoStack) CanUndo() bool

CanUndo returns true if there are entries to undo.

func (*UndoStack) Clear

func (s *UndoStack) Clear()

Clear clears both undo and redo stacks.

func (*UndoStack) Redo

func (s *UndoStack) Redo(currentContent string, currentCursor Position) *UndoEntry

Redo returns the next state, or nil if nothing to redo.

func (*UndoStack) Save

func (s *UndoStack) Save(content string, cursor Position)

Save saves the current state for undo.

func (*UndoStack) Undo

func (s *UndoStack) Undo(currentContent string, currentCursor Position) *UndoEntry

Undo returns the previous state, or nil if nothing to undo.

type ViewConfig

type ViewConfig struct {
	// Styles
	NormalText   lipgloss.Style
	CursorBlock  lipgloss.Style // For normal mode cursor (inverted)
	CursorInsert lipgloss.Style // For insert mode cursor position
	Selection    lipgloss.Style // For visual mode selection
	LineNumber   lipgloss.Style
	CommandLine  lipgloss.Style
	ShowLineNums bool
}

ViewConfig configures the editor view rendering.

func DefaultViewConfig

func DefaultViewConfig() ViewConfig

DefaultViewConfig returns default styles using the theme package for adaptive colors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL