Turn Vim functions into operators easily. So OP.
- Turn any function into a Vim operator (note: the function will receive a text 1st argument)
- Automatically handles dot-repeat
- Helper for a quick operator mapping
- Want to map multiple functions into an operator? No problem. Use a popup menu helper to manage multiple operators
Your function should accept a string as its first argument:
function! MyFunction(text)
" Do something with text
echo "Processing: " .. a:text
endfunction
call Operatorify#Mapper('go', 'MyFunction')
The mapper follows the convention that repeating the last character of the key creates a line operator.
go
operates on a motiongoo
operates on the current linego
in visual mode operates on the selection
If you want to create your own set of mapping:
" Create mappings
nnoremap <expr> <Plug>MyFunction Operatorify#Wrapper('MyFunction')
nnoremap <expr> <Plug>MyFunctionLine Operatorify#Wrapper('MyFunction') .. '_'
" Map to keys
nmap go <Plug>MyFunction
nmap goo <Plug>MyFunctionLine
You can manage multiple operators through a popup menu. By default, this is mapped to gl
:
" Define your functions
function! ToUpper(text)
return toupper(a:text)
endfunction
function! ToLower(text)
return tolower(a:text)
endfunction
" Set up the list
let g:operatorify_list = ['ToUpper', 'ToLower']
" Default mapping is gl, but you can change it:
nnoremap <leader>o :call OpLister()<CR>
To disable the default mapping, add this to your vimrc:
let g:operatorify_no_mappings = 1
You can customize the appearance and behavior of the popup menu:
let g:operatorify_lister_options = {
\ 'callback': 'PopupCallback',
\ 'border': [0,0,0,0],
\ 'padding': [0,1,0,0],
\ 'pos': 'topleft',
\ 'moved': [0, 0, 0],
\ 'scrollbar': 1,
\ 'maxheight': 5,
\ 'fixed': 1,
\ 'highlight': 'Normal',
\ 'minwidth': 25
\ }
Plug 'iggredible/vim-operatorify'
use 'iggredible/vim-operatorify'
cd ~/.vim/bundle
git clone https://github.com/iggredible/vim-operatorify.git
:help operatorify
function! ToggleCase(text)
return a:text =~# '\u' ? tolower(a:text) : toupper(a:text)
endfunction
call Operatorify#Mapper('gt', 'ToggleCase')
" gt{motion} - toggle case of motion
" gtt - toggle case of current line
" gt - toggle case of visual selection
" Define text transformation functions
let g:operatorify_list = [
\ 'ToUpper',
\ 'ToLower',
\ 'Capitalize',
\ 'CamelCase',
\ 'SnakeCase'
\ ]
" Use default gl mapping
call Operatorify#Mapper('gl', 'Operatorify#Lister')
Distributed under the same terms as Vim itself. See :help license
.