vim-singlechar/plugin/singlechar.vim
2025-08-19 09:09:34 +02:00

106 lines
3.7 KiB
VimL

vim9script noclear
# ------------------------------------------------------------------------------ #
# singlechar.vim - Insert single characters without entering insert mode
# Author: lohhiiccc
# Version: 1.0.0
# License: MIT
# Repository: https://github.com/lohhiiccc/vim-singlechar
# ------------------------------------------------------------------------------ #
# Description:
# This plugin allows you to quickly insert a single character without entering
# insert mode. Simply use the configured mapping, press the character you want
# to insert, and continue in normal mode. Supports count for repeating characters.
# ------------------------------------------------------------------------------ #
if exists('g:loaded_singlechar') || &cp || v:version < 900
finish
endif
g:loaded_singlechar = 1
# ------------------------------------------------------------------------------ #
# Configuration options
# Mapping to insert a character at cursor position (before cursor)
if !exists('g:singlechar_map_insert_at')
g:singlechar_map_insert_at = '<Leader>i'
endif
# Mapping to insert a character after cursor position
if !exists('g:singlechar_map_insert_after')
g:singlechar_map_insert_after = '<Leader>a'
endif
# Prompt message shown when waiting for character input
if !exists('g:singlechar_prompt')
g:singlechar_prompt = 'Press the character to insert - Press Esc to cancel...'
endif
# ------------------------------------------------------------------------------ #
# Core functionality
g:last_singlechar_key = ''
g:last_singlechar_mode = ''
g:last_singlechar_count = 1
# Function to handle character insertion
# Parameters:
# mode: 'at' to insert before cursor, 'after' to insert after cursor
# count: number of times to repeat the character
# pkey: character to insert (optional)
export def InsertChar(mode: string, count: number, pkey: string = ''): void
var key = pkey
if pkey == ''
echo g:singlechar_prompt
redraw
# Get character from user
var char = getchar()
key = nr2char(char)
endif
if key ==# "\<Esc>"
return
endif
# Determine whether to insert before (i) or after (a) cursor
var cmd = (mode ==# 'at') ? 'i' : 'a'
var text = repeat(key, count)
execute 'normal! ' .. cmd .. text .. "\<Esc>"
#save
g:last_singlechar_key = key
g:last_singlechar_mode = mode
g:last_singlechar_count = count
#set vim-repeat
legacy call repeat#set("\<Plug>(singlechar-repeat)")
enddef
export def g:RepeatSingleChar(): void
if g:last_singlechar_key != ''
call InsertChar(g:last_singlechar_mode, g:last_singlechar_count, g:last_singlechar_key)
endif
enddef
# ------------------------------------------------------------------------------ #
# Mappings <Plug>
nnoremap <Plug>(singlechar-repeat) :call g:RepeatSingleChar()<CR>
# Direct command implementations
command! -count=1 -nargs=0 InsertCharAt InsertChar('at', <count>)
command! -count=1 -nargs=0 InsertCharAfter InsertChar('after', <count>)
# Create default mappings unless disabled
if !exists('g:singlechar_no_mappings')
execute 'nnoremap <expr> <silent> ' .. g:singlechar_map_insert_at .. ' ":<C-u>InsertCharAt " .. v:count1 .. "<CR>"'
execute 'nnoremap <expr> <silent> ' .. g:singlechar_map_insert_after .. ' ":<C-u>InsertCharAfter " .. v:count1 .. "<CR>"'
endif
# Usage:
# <Leader>i - Insert character at cursor position
# <Leader>a - Insert character after cursor position
#
# You can use a count before the mapping to insert multiple copies
# of the same character. For example: 3<Leader>i will insert the
# character 3 times at the cursor position.
# ------------------------------------------------------------------------------ #