From 398c5a41d39417d2384651113015824e48b8524f Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 19 Aug 2025 09:09:34 +0200 Subject: [PATCH 1/6] feat: add vim-repeat support --- plugin/singlechar.vim | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/plugin/singlechar.vim b/plugin/singlechar.vim index 32e7dcf..fd32cc1 100644 --- a/plugin/singlechar.vim +++ b/plugin/singlechar.vim @@ -37,18 +37,26 @@ 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 -def InsertChar(mode: string, count: number): void - echo g:singlechar_prompt - - # Get character from user - var char = getchar() - var key = nr2char(char) - - redraw +# 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 ==# "\" return endif @@ -58,10 +66,24 @@ def InsertChar(mode: string, count: number): void var text = repeat(key, count) execute 'normal! ' .. cmd .. text .. "\" + #save + g:last_singlechar_key = key + g:last_singlechar_mode = mode + g:last_singlechar_count = count + + #set vim-repeat + legacy call repeat#set("\(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 # ------------------------------------------------------------------------------ # -# Commands and mappings +# Mappings +nnoremap (singlechar-repeat) :call g:RepeatSingleChar() # Direct command implementations command! -count=1 -nargs=0 InsertCharAt InsertChar('at', ) -- 2.47.3 From 629a204590eb5d782ad6085c7907ed0a16acc0fd Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 19 Aug 2025 09:14:57 +0200 Subject: [PATCH 2/6] style: remove unused 'import' keyword to functions --- plugin/singlechar.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/singlechar.vim b/plugin/singlechar.vim index fd32cc1..896a25d 100644 --- a/plugin/singlechar.vim +++ b/plugin/singlechar.vim @@ -46,7 +46,7 @@ g:last_singlechar_count = 1 # 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 +def InsertChar(mode: string, count: number, pkey: string = ''): void var key = pkey if pkey == '' @@ -75,7 +75,7 @@ export def InsertChar(mode: string, count: number, pkey: string = ''): void legacy call repeat#set("\(singlechar-repeat)") enddef -export def g:RepeatSingleChar(): void +def g:RepeatSingleChar(): void if g:last_singlechar_key != '' call InsertChar(g:last_singlechar_mode, g:last_singlechar_count, g:last_singlechar_key) endif -- 2.47.3 From 64bcd7a1a05f751396b064a91da66bd303a6b584 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 19 Aug 2025 10:16:33 +0200 Subject: [PATCH 3/6] feat: vim-repeat optional is now optional --- plugin/singlechar.vim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin/singlechar.vim b/plugin/singlechar.vim index 896a25d..76d8b2f 100644 --- a/plugin/singlechar.vim +++ b/plugin/singlechar.vim @@ -72,7 +72,9 @@ def InsertChar(mode: string, count: number, pkey: string = ''): void g:last_singlechar_count = count #set vim-repeat - legacy call repeat#set("\(singlechar-repeat)") + if exists('*repeat#set') + legacy call repeat#set("\(singlechar-repeat)") + endif enddef def g:RepeatSingleChar(): void -- 2.47.3 From 8463cdcf4bff5bc7ce3d488f844ced495163d6cb Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 19 Aug 2025 11:05:27 +0200 Subject: [PATCH 4/6] refactor: migrate core logic to autoload/singlechar.vim - Move main functions (InsertChar, RepeatSingleChar) to autoload/singlechar.vim, using Vim9script and export for modularity - Refactor plugin/singlechar.vim to import autoload module and update commands/mappings accordingly - Prepare for improved maintainability and compatibility with vim-repeat --- autoload/singlechar.vim | 43 ++++++++++++++++++++++++++++++++ plugin/singlechar.vim | 55 +++++------------------------------------ 2 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 autoload/singlechar.vim diff --git a/autoload/singlechar.vim b/autoload/singlechar.vim new file mode 100644 index 0000000..f14370e --- /dev/null +++ b/autoload/singlechar.vim @@ -0,0 +1,43 @@ +vim9script noclear + +# 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 ==# "\" + 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 .. "\" + #save + g:last_singlechar_key = key + g:last_singlechar_mode = mode + g:last_singlechar_count = count + + #set vim-repeat + #if exists('*repeat#set') + legacy silent! call repeat#set("\(singlechar-repeat)") + #endif +enddef + +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 diff --git a/plugin/singlechar.vim b/plugin/singlechar.vim index 76d8b2f..465158c 100644 --- a/plugin/singlechar.vim +++ b/plugin/singlechar.vim @@ -1,4 +1,5 @@ vim9script noclear +import autoload 'singlechar.vim' # ------------------------------------------------------------------------------ # # singlechar.vim - Insert single characters without entering insert mode # Author: lohhiiccc @@ -17,8 +18,8 @@ if exists('g:loaded_singlechar') || &cp || v:version < 900 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 = 'i' @@ -35,61 +36,18 @@ if !exists('g:singlechar_prompt') 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) -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 ==# "\" - 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 .. "\" - #save - g:last_singlechar_key = key - g:last_singlechar_mode = mode - g:last_singlechar_count = count - - #set vim-repeat - if exists('*repeat#set') - legacy call repeat#set("\(singlechar-repeat)") - endif -enddef - -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 nnoremap (singlechar-repeat) :call g:RepeatSingleChar() # Direct command implementations -command! -count=1 -nargs=0 InsertCharAt InsertChar('at', ) -command! -count=1 -nargs=0 InsertCharAfter InsertChar('after', ) +command! -count=1 -nargs=0 InsertCharAt singlechar.InsertChar('at', ) +command! -count=1 -nargs=0 InsertCharAfter singlechar.InsertChar('after', ) # Create default mappings unless disabled if !exists('g:singlechar_no_mappings') @@ -97,7 +55,6 @@ if !exists('g:singlechar_no_mappings') execute 'nnoremap ' .. g:singlechar_map_insert_after .. ' ":InsertCharAfter " .. v:count1 .. ""' endif - # Usage: # i - Insert character at cursor position # a - Insert character after cursor position -- 2.47.3 From 8596234462107e4b8eacec471503045cc899b64e Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 19 Aug 2025 11:17:17 +0200 Subject: [PATCH 5/6] docs: add vim-repeat integration details to README and help - Update README.md with instructions and notes for vim-repeat (`.`) support - Update doc/singlechar.txt: add requirements, usage, and tips for vim-repeat, including installation and dot-repeat behavior --- README.md | 17 ++++++++++++++++- doc/singlechar.txt | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f9f31f2..55aa968 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ A simple Vim plugin that lets you insert single characters without leaving norma - Stay in normal mode for efficient editing - Minimal workflow interruption - Works with Vim 9+ +- **Supports [`vim-repeat`](https://github.com/tpope/vim-repeat) for `.` repeat functionality** ## Installation @@ -26,6 +27,16 @@ cp -r vim-singlechar ~/.vim/pack/plugins/start/ cd - ``` +### Optional: Install [vim-repeat](https://github.com/tpope/vim-repeat) + +For seamless repeat (`.`) support, install [vim-repeat](https://github.com/tpope/vim-repeat): + +```bash +mkdir -p ~/.vim/pack/tpope/start +cd ~/.vim/pack/tpope/start +git clone https://tpope.io/vim/repeat.git +``` + ## Usage By default, the plugin provides two mappings: @@ -42,6 +53,10 @@ You can use a count before the mapping to insert the character multiple times: - `3i,` will insert three commas at the cursor position - `5a.` will insert five periods after the cursor position +### Repeat last insert with `.` + +If [vim-repeat](https://github.com/tpope/vim-repeat) is installed, you can repeat the last character insertion with `.` in normal mode. + ## Configuration You can customize the plugin by setting these variables in your vimrc: @@ -67,4 +82,4 @@ The plugin provides these commands: ## License -MIT License +MIT Licens diff --git a/doc/singlechar.txt b/doc/singlechar.txt index ca88bb4..17b0258 100644 --- a/doc/singlechar.txt +++ b/doc/singlechar.txt @@ -34,6 +34,7 @@ REQUIREMENTS *singlechar-requiremen - Vim 9.0 or newer - Vim compiled with `+eval` feature +- [vim-repeat](https://github.com/tpope/vim-repeat/) (optional, for dot-repeat support) =============================================================================== INSTALLATION *singlechar-installation* @@ -43,6 +44,13 @@ Manual installation: > mkdir -p ~/.vim/pack/plugins/start/ cp -r vim-singlechar ~/.vim/pack/plugins/start/ < + +To enable dot-repeat (`.`) for character insertions, install vim-repeat: > + mkdir -p ~/.vim/pack/tpope/start + cd ~/.vim/pack/tpope/start + git clone https://tpope.io/vim/repeat.git +< + =============================================================================== USAGE *singlechar-usage* @@ -60,6 +68,12 @@ You can use a count before the mapping to insert the character multiple times: > 5i- " Insert 5 hyphens at cursor position 3a. " Insert 3 periods after cursor position < + +Dot-repeat support (`.`): + +If [vim-repeat](https://github.com/tpope/vim-repeat/) is installed, +you can use the `.` key to repeat the last single character insertion (with count and position). + =============================================================================== COMMANDS *singlechar-commands* @@ -110,10 +124,15 @@ Adding multiple periods: > Action: Position cursor at the end, press 3a. After: Some text... < +Dot-repeat: > + Action: Press i? to insert a question mark, then press . to repeat the insertion at the next location. +< + =============================================================================== TIPS & TRICKS *singlechar-tips* - The plugin works well with Vim's macro recording feature +- For fast repetitive edits, use dot-repeat (`.`) if vim-repeat is installed =============================================================================== LICENSE *singlechar-license* -- 2.47.3 From cd5475bb207d2e8dff6d465f3dcd28fcdbd860bd Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 19 Aug 2025 11:19:55 +0200 Subject: [PATCH 6/6] typo(README): Licens -> License --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 55aa968..f0795d4 100644 --- a/README.md +++ b/README.md @@ -82,4 +82,4 @@ The plugin provides these commands: ## License -MIT Licens +MIT License -- 2.47.3