​原文​​​ 如用​​\u​​打开​​url​​.而​​\ui]​​打开​​[]​​中​​url​​.而​​\u$/\uu/等​​.

"从unimpaired.vim改编"
function! s:DoAction(algorithm,type)
" 备份设置"
let sel_save = &selection
let cb_save = &clipboard
"使选择和剪贴板按我们需要的方式工作"
set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
"备份未命名的寄存器"
let reg_save = @@
"复制相关文本,并设置视觉选区,重用文本,需要更换"
if a:type =~ '^\d\+$'
" 数字,则选择那么多行"
silent exe 'normal! V'.a:type.'$y'
//从选择行开始.
elseif a:type =~ '^.$'
"类型是'v','V',或'<C-V>'(i.e.0x16),则重选"
silent exe "normal! `<" . a:type . "`>y"
elseif a:type == 'line'
"行动作"
silent exe "normal! '[V']y"
elseif a:type == 'block'
"块动作"
silent exe "normal! `[\<C-V>`]y"
else
"符动作"
silent exe "normal! `[v`]y"
endif
"调用用户定义的函数,传递未命名寄存器内容给它"
let repl = s:{a:algorithm}(@@)
"如果函数返回值,则替换文本"
if type(repl) == 1
" 将替换文本放入无名寄存器中,并根据复制类型,设置为按字符,按行或按块选择,"
call setreg('@', repl, getregtype('@'))
"重选可视区,并粘贴"
normal! gvp
endif
"恢复保存的设置和寄存器值"
let @@ = reg_save
let &selection = sel_save
let &clipboard = cb_save
endfunction

function! s:ActionOpfunc(type)
return s:DoAction(s:encode_algorithm, a:type)
//监听后,调用
endfunction

function! s:ActionSetup(algorithm)
let s:encode_algorithm = a:algorithm
//算法动作.
let &opfunc = matchstr(expand('<sfile>'), '<SNR>\d\+_').'ActionOpfunc'
//后续命令.
endfunction

function! MapAction(algorithm, key)
exe 'nnoremap <silent> <Plug>actions' .a:algorithm.'?:<C-U>call <SID>ActionSetup("'.a:algorithm.'")<CR>g@'
//安装程序.最后发送g@,然后vim监听动作命令.
exe 'xnoremap <silent> <Plug>actions' .a:algorithm.'?:<C-U>call <SID>DoAction("'.a:algorithm.'",visualmode())<CR>'
//可视模式
exe 'nnoremap <silent> <Plug>actionsLine'.a:algorithm.'?:<C-U>call <SID>DoAction("'.a:algorithm.'",v:count1)<CR>'
//行模式.
//创建内部映射.将之映射到键序列.
exe 'nmap '.a:key.' <Plug>actions'.a:algorithm
exe 'xmap '.a:key.' <Plug>actions'.a:algorithm
exe 'nmap '.a:key.a:key[strlen(a:key)-1].' <Plug>actionsLine'.a:algorithm
endfunction

​MapAction(函数,键)​​​,​​<leader>r反转串​​​.​​<leader>u​​​打开链接.​​<leader>M​​​计算​​md5​​.

function! s:ReverseString(str)
let out = join(reverse(split(a:str, '\zs')), '')
"反转串"
let out = substitute(out, '^\n', '', '')
return out
endfunction
call MapAction('ReverseString', '<leader>r')
//
function! s:OpenUrl(str)
silent execute "!firefox ".shellescape(a:str, 1)
redraw!
endfunction

call MapAction('OpenUrl','<leader>u')
//
function! s:ComputeMD5(str)
let out = system('md5sum |cut -b 1-32', a:str)
"删除尾空行"
let out = substitute(out, '\n$', '', '')
return out
endfunction
call MapAction('ComputeMD5','<leader>M')