Merge pull request #3 from lohhiiccc/feature/begin-end
Feature/begin end
This commit is contained in:
commit
05083fcc0b
4 changed files with 93 additions and 21 deletions
32
README.md
32
README.md
|
|
@ -39,10 +39,16 @@ git clone https://tpope.io/vim/repeat.git
|
|||
|
||||
## Usage
|
||||
|
||||
By default, the plugin provides two mappings:
|
||||
By default, the plugin provides four mappings:
|
||||
> [optional param]
|
||||
> (param)
|
||||
|
||||
- `<Leader>i` - Insert a character at the cursor position
|
||||
- `<Leader>a` - Insert a character after the cursor position
|
||||
> `[count]<bind>(char)`
|
||||
|
||||
- `[count]<Leader>i(char)` - Insert a character at the cursor position
|
||||
- `[count]<Leader>a(char)` - Insert a character after the cursor position
|
||||
- `[count]<Leader>I(char)` - Insert a character at the beginning of the line
|
||||
- `[count]<Leader>A(char)` - Insert a character at the end of the line
|
||||
|
||||
After pressing the mapping, you'll see a prompt asking for the character to insert. Press any character and it will be inserted without entering insert mode.
|
||||
|
||||
|
|
@ -63,8 +69,13 @@ You can customize the plugin by setting these variables in your vimrc:
|
|||
|
||||
```vim
|
||||
" Change the mappings (before loading the plugin)
|
||||
let g:singlechar_map_insert_at = '<Leader>i' " Default
|
||||
let g:singlechar_map_insert_after = '<Leader>a' " Default
|
||||
let g:singlechar_map_insert_at = '<Leader>i' " Default
|
||||
let g:singlechar_map_insert_after = '<Leader>a' " Default
|
||||
let g:singlechar_map_insert_begin = '<Leader>I' " Default
|
||||
let g:singlechar_map_insert_end = '<Leader>A' " Default
|
||||
|
||||
" Set the static cursor (no cursor move during singlechar actions)
|
||||
let g:singlechar_static_cursor = 1
|
||||
|
||||
" Change the prompt message
|
||||
let g:singlechar_prompt = 'Press the character to insert - Press Esc to cancel...' " Default
|
||||
|
|
@ -84,9 +95,14 @@ let g:singlechar_warning_message = 'Only the first character will be taken: {cha
|
|||
|
||||
The plugin provides these commands:
|
||||
|
||||
- `:InsertCharAt [count]` - Insert a character at the cursor position
|
||||
- `:InsertCharAfter [count]` - Insert a character after the cursor position
|
||||
> [optional param]
|
||||
> (param)
|
||||
|
||||
- `:InsertCharAt (count) [char]` - Insert a character at the cursor position
|
||||
- `:InsertCharAfter (count) [char]` - Insert a character after the cursor position
|
||||
- `:InsertCharBegin (count) [char]` - Insert a character at the beginning of the line
|
||||
- `:InsertCharEnd (count) [char]` - Insert a character at the end of the line
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
[MIT License](https://github.com/lohhiiccc/vim-singlechar/blob/main/LICENSE)
|
||||
|
|
|
|||
|
|
@ -19,11 +19,10 @@ export def InsertChar(mode: string, count: number, input_char: string = ''): voi
|
|||
return
|
||||
endif
|
||||
|
||||
# Determine whether to insert before (i) or after (a) cursor
|
||||
var insert_command = (mode ==# 'at') ? 'i' : 'a'
|
||||
var insert_command = GetInsertCommand(mode)
|
||||
var insert_text = repeat(first_char, (count == 0) ? 1 : count)
|
||||
|
||||
execute 'normal! ' .. insert_command .. insert_text .. "\<Esc>"
|
||||
DoSingleChar(insert_command, insert_text, g:singlechar_static_cursor)
|
||||
|
||||
# Save last used values
|
||||
g:last_singlechar_key = first_char
|
||||
|
|
@ -38,6 +37,31 @@ export def InsertChar(mode: string, count: number, input_char: string = ''): voi
|
|||
endif
|
||||
enddef
|
||||
|
||||
def GetInsertCommand(mode: string): string
|
||||
if mode ==# 'at'
|
||||
return 'i'
|
||||
elseif mode ==# 'after'
|
||||
return 'a'
|
||||
elseif mode ==# 'begin'
|
||||
return 'I'
|
||||
elseif mode ==# 'end'
|
||||
return 'A'
|
||||
else
|
||||
return 'i'
|
||||
endif
|
||||
enddef
|
||||
|
||||
def DoSingleChar(command: string, text: string, resetCursor: number): void
|
||||
var pos = getpos('.')
|
||||
execute 'normal! ' .. command .. text .. "\<Esc>"
|
||||
if resetCursor == 1
|
||||
if (command ==# 'i')
|
||||
pos[2] = pos[2] + 1
|
||||
endif
|
||||
setpos('.', pos)
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
Author: lohhiiccc
|
||||
Version: 1.0.0
|
||||
License: MIT
|
||||
Last Change: 2025-08-19
|
||||
Last Change: 2025-08-21
|
||||
|
||||
CONTENTS *singlechar-contents*
|
||||
|
||||
|
|
@ -79,8 +79,10 @@ COMMANDS *singlechar-commands
|
|||
|
||||
The plugin provides two commands:
|
||||
|
||||
`:InsertCharAt [count]` - Insert a character at the cursor position
|
||||
`:InsertCharAfter [count]` - Insert a character after the cursor position
|
||||
`:InsertCharAt [count] [char]` - Insert a character at the cursor position
|
||||
`:InsertCharAfter [count] [char]` - Insert a character after the cursor position
|
||||
`:InsertCharBegin [count] [char]` - Insert a character at the beginning of the line
|
||||
`:InsertCharEnd [count] [char]` - Insert a character at the end of the line
|
||||
|
||||
The optional count parameter specifies how many times to insert the character.
|
||||
|
||||
|
|
@ -89,8 +91,10 @@ MAPPINGS *singlechar-mappings
|
|||
|
||||
By default, the plugin defines these mappings:
|
||||
|
||||
`<Leader>i` - Map to `:InsertCharAt`
|
||||
`<Leader>a` - Map to `:InsertCharAfter`
|
||||
`[count]<Leader>i` - Map to `:InsertCharAt [count]`
|
||||
`[count]<Leader>a` - Map to `:InsertCharAfter [count]`
|
||||
`[count]<Leader>I` - Map to `:InsertCharBegin [count]`
|
||||
`[count]<Leader>A` - Map to `:InsertCharEnd [count]`
|
||||
|
||||
===============================================================================
|
||||
CONFIGURATION *singlechar-configuration*
|
||||
|
|
@ -99,6 +103,8 @@ Customizing mappings: >
|
|||
" Must be set before the plugin is loaded
|
||||
let g:singlechar_map_insert_at = '<Leader>i' " Default
|
||||
let g:singlechar_map_insert_after = '<Leader>a' " Default
|
||||
let g:singlechar_map_insert_begin = '<Leader>I' " Default
|
||||
let g:singlechar_map_insert_end = '<Leader>A' " Default
|
||||
<
|
||||
Disabling default mappings: >
|
||||
" Disable default mappings
|
||||
|
|
@ -107,7 +113,13 @@ Disabling default mappings: >
|
|||
" Create your own mappings
|
||||
nnoremap <expr> <silent> ,i ":<C-u>InsertCharAt " .. v:count1 .. "<CR>"
|
||||
nnoremap <expr> <silent> ,a ":<C-u>InsertCharAfter " .. v:count1 .. "<CR>"
|
||||
nnoremap <expr> <silent> ,I ":<C-u>InsertCharBegin " .. v:count1 .. "<CR>"
|
||||
nnoremap <expr> <silent> ,A ":<C-u>InsertCharEnd " .. v:count1 .. "<CR>"
|
||||
<
|
||||
Set static cursor option: >
|
||||
" When enabled, cursor returns to its original position after insertion
|
||||
let g:singlechar_static_cursor = 1
|
||||
|
||||
Changing the prompt message: >
|
||||
let g:singlechar_prompt = 'Character to insert (Esc to cancel): '
|
||||
<
|
||||
|
|
|
|||
|
|
@ -30,15 +30,32 @@ if !exists('g:singlechar_map_insert_after')
|
|||
g:singlechar_map_insert_after = '<Leader>a'
|
||||
endif
|
||||
|
||||
# Mapping to insert a character at the endline
|
||||
if !exists('g:singlechar_map_insert_end')
|
||||
g:singlechar_map_insert_end = '<Leader>A'
|
||||
endif
|
||||
|
||||
# Mapping to insert a character at the startline
|
||||
if !exists('g:singlechar_map_insert_begin')
|
||||
g:singlechar_map_insert_begin = '<Leader>I'
|
||||
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
|
||||
|
||||
# Enable/disable static cursor feature
|
||||
if !exists('g:singlechar_static_cursor')
|
||||
g:singlechar_static_cursor = 0
|
||||
endif
|
||||
|
||||
# toggle warning
|
||||
if !exists('g:singlechar_keylen_warning')
|
||||
g:singlechar_keylen_warning = 1
|
||||
endif
|
||||
|
||||
# warning message
|
||||
if !exists('g:singlechar_warning_message')
|
||||
g:singlechar_warning_message = 'Only the first character will be taken: {char}'
|
||||
endif
|
||||
|
|
@ -54,6 +71,8 @@ g:last_singlechar_count = 1
|
|||
nnoremap <Plug>(singlechar-repeat) :call g:RepeatSingleChar()<CR>
|
||||
|
||||
# Direct command implementations
|
||||
command! -count=1 -nargs=? InsertCharBegin singlechar.InsertChar('begin', <count>, <q-args>)
|
||||
command! -count=1 -nargs=? InsertCharEnd singlechar.InsertChar('end', <count>, <q-args>)
|
||||
command! -count=1 -nargs=? InsertCharAt singlechar.InsertChar('at', <count>, <q-args>)
|
||||
command! -count=1 -nargs=? InsertCharAfter singlechar.InsertChar('after', <count>, <q-args>)
|
||||
|
||||
|
|
@ -61,13 +80,14 @@ command! -count=1 -nargs=? InsertCharAfter singlechar.InsertChar('after', <count
|
|||
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>"'
|
||||
execute 'nnoremap <expr> <silent> ' .. g:singlechar_map_insert_begin .. ' ":<C-u>InsertCharBegin " .. v:count1 .. "<CR>"'
|
||||
execute 'nnoremap <expr> <silent> ' .. g:singlechar_map_insert_end .. ' ":<C-u>InsertCharEnd " .. v:count1 .. "<CR>"'
|
||||
endif
|
||||
|
||||
# Usage:
|
||||
# <Leader>i - Insert character at cursor position
|
||||
# <Leader>a - Insert character after cursor position
|
||||
# [count]<Leader>i - Insert character at cursor position
|
||||
# [count]<Leader>I - Insert character at begin of the line
|
||||
# [count]<Leader>a - Insert character after cursor position
|
||||
# [count]<Leader>A - Insert character at end of the line
|
||||
#
|
||||
# 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.
|
||||
# ------------------------------------------------------------------------------ #
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue