Skip to main content

Overview

ZepMode_Vim implements Vim-style modal editing in Zep, providing familiar keyboard navigation and editing commands for Vim users. This mode supports Normal, Insert, Visual, and Ex modes with traditional Vim key bindings.

Class Definition

class ZepMode_Vim : public ZepMode
Inherits from ZepMode and implements Vim-style editing behavior.

Constructor

ZepMode_Vim(ZepEditor& editor)
Creates a new Vim mode instance.
editor
ZepEditor&
required
Reference to the parent ZepEditor instance

Core Methods

Init

virtual void Init() override
Initializes the Vim mode and sets up all key mappings. This is called once during mode creation.

Begin

virtual void Begin(ZepWindow* pWindow) override
Activates Vim mode for the specified window.
pWindow
ZepWindow*
required
The window to activate Vim mode in

Name

virtual const char* Name() const override
static const char* StaticName()
Returns the mode name “Vim”. Returns: "Vim"

DefaultMode

virtual EditorMode DefaultMode() const override
Returns the default editor mode for Vim (Normal mode). Returns: EditorMode::Normal

PreDisplay

virtual void PreDisplay(ZepWindow& win) override
Prepares the window for display, updating mode-specific visual elements.
win
ZepWindow&
required
The window being prepared for display

UsesRelativeLines

virtual bool UsesRelativeLines() const override
Indicates that Vim mode uses relative line numbering (like Vim’s relativenumber option). Returns: true
Relative line numbering displays distances from the current line, making it easier to use motion commands like 5j or 3k.

Key Mapping Methods

These methods set up various categories of Vim key bindings:

SetupKeyMaps

virtual void SetupKeyMaps()
Sets up all key mappings for Vim mode. This calls the other mapping methods to configure the complete Vim keymap.

AddNavigationKeyMaps

virtual void AddNavigationKeyMaps(bool allowInVisualMode = true)
Adds navigation key mappings (h, j, k, l, w, b, e, etc.).
allowInVisualMode
bool
default:"true"
Whether to enable these mappings in Visual mode

AddSearchKeyMaps

virtual void AddSearchKeyMaps()
Adds search-related key mappings (/, ?, n, N, *, #).

AddGlobalKeyMaps

virtual void AddGlobalKeyMaps()
Adds global key mappings that work across all modes (like Escape).

AddCopyMaps

virtual void AddCopyMaps()
Adds copy/yank key mappings (y, yy, Y).

AddPasteMaps

virtual void AddPasteMaps()
Adds paste key mappings (p, P).

AddOverStrikeMaps

virtual void AddOverStrikeMaps()
Adds overstrike/replace key mappings (r, R).

Vim Motions

The mode supports various motion types:
enum class VimMotion
{
    LineBegin,           // ^ - First non-whitespace character
    LineEnd,             // $ - End of line
    NonWhiteSpaceBegin,  // ^ - First non-whitespace
    NonWhiteSpaceEnd     // g_ - Last non-whitespace
}
These motions can be combined with operators like delete (d), change (c), and yank (y) to form complex commands.

Supported Vim Commands

Normal Mode

  • Navigation: h, j, k, l, w, b, e, ge, W, B, E, 0, ^, $, gg, G
  • Editing: x, X, dd, D, C, s, S, i, I, a, A, o, O
  • Copy/Paste: y, yy, Y, p, P
  • Undo/Redo: u, Ctrl+R
  • Search: /, ?, n, N, *, #
  • Visual: v, V

Insert Mode

  • Exit: Escape, Ctrl+[
  • Standard text input

Visual Mode

  • Selection: v (character), V (line)
  • Operations: d, c, y, >, <
  • All navigation commands

Ex Mode

  • Commands: :w, :q, :wq, :e, and more
  • Accessed via : from Normal mode

Usage Example

#include <zep/mode_vim.h>
#include <zep/editor.h>

// Create editor
auto editor = std::make_unique<ZepEditor>(display);

// Create and register Vim mode
auto vimMode = std::make_shared<ZepMode_Vim>(*editor);
vimMode->Init();
editor->RegisterMode(vimMode);

// Set Vim as the active mode
editor->SetMode(ZepMode_Vim::StaticName());

// The editor now responds to Vim commands
// For example, pressing 'i' enters Insert mode
// Pressing Escape returns to Normal mode

// Get current window and start using Vim mode
auto window = editor->GetActiveWindow();
vimMode->Begin(window);

// Mode automatically handles key presses
vimMode->AddKeyPress('i');  // Enter insert mode
vimMode->AddKeyPress('H');  // Type 'H'
vimMode->AddKeyPress('i');  // Type 'i'
vimMode->AddKeyPress(ExtKeys::ESCAPE);  // Back to normal mode
vimMode->AddKeyPress('d');  // Start delete operation
vimMode->AddKeyPress('d');  // Complete: delete line

Configuration Example

// Custom Vim mode with additional key mappings
class MyVimMode : public ZepMode_Vim
{
public:
    MyVimMode(ZepEditor& editor) : ZepMode_Vim(editor) {}
    
    void Init() override
    {
        // Initialize base Vim mappings
        ZepMode_Vim::Init();
        
        // Add custom mappings
        // Example: map 'jk' to escape in insert mode
        m_insertMap.AddKeyMapSequence("jk", 
            {ExtKeys::ESCAPE}, 
            StringId("ExitInsert"));
    }
};
Vim mode maintains separate key maps for Normal, Insert, and Visual modes, allowing mode-specific key bindings.

Editor Modes in Vim

Vim mode cycles through different EditorMode states:
  • EditorMode::Normal - Command mode (default)
  • EditorMode::Insert - Text insertion mode
  • EditorMode::Visual - Visual selection mode
  • EditorMode::Ex - Colon command mode
See ZepMode for the complete EditorMode enum definition.

See Also

Build docs developers (and LLMs) love