Vim写代码时经常会用到两个单词切换,比如 true 转成 false,on 改成 off 等,
于是就偷懒写了个函数一键替换,内容完全自定义,并且会保留大小写方式。
定义一个 ["up","down"],还能实现 aUp→aDown,a_Up→a_Down,a_up→a_down的转换
vimrc 里增加两行内容
nnoremap <silent> <M-t> ciw<C-r>=hy_string#modify#toggleWord(@")<cr><esc>
xnoremap <silent> <M-t> s<C-r>=hy_string#modify#toggleWord(@")<cr><esc>
以下函数是写在 $Vimvimfilesautoloadhy_stringmodify.vim 文件内
"true/false, up/down切换
"NOTE lis大小写或首字母大写,全用小写,其他情况保留原大小写
function! hy_string#modify#toggleWord(word)
let lis = [
['!', '!'],
[',', ','],
['.', '。'],
['<^', 'LCtrl & '],
['>^', 'RCtrl & '],
['<+', 'LShift & '],
['>+', 'RShift & '],
['<!', 'LAlt & '],
['>!', 'RAlt & '],
['FireShot', 'mcbpblocgmgfnpjjppndjkmgjaogfceg'],
['Surfingkeys', 'gfbliohnnapiefjpjlpjnehglfpaknnc'],
['SwitchyOmega', 'padekgcemlokbadohgkifijomclgjgif'],
['Tampermonkey', 'dhdgffkkebhmkfjojejmpbldmpobfkfo'],
['from', 'to'],
['yes', 'no'],
['on', 'off'],
['down', 'up'],
['left', 'right'],
['top', 'bottom'],
['this', 'base'],
['read', 'write'],
['row', 'column'],
['rows', 'columns'],
['focus', 'blur'],
['lower', 'upper'],
['odd', 'even'],
['before', 'after'],
['max', 'min'],
['prev', 'next'],
['true', 'false'],
['enable', 'disable'],
['enabled', 'disabled'],
['user', 'pwd'],
['width', 'height'],
['horizontal', 'vertical'],
['username', 'password'],
['hkcr', 'HKEY_CLASSES_ROOT'],
['hkcu', 'HKEY_CURRENT_USER'],
['hklm', 'HKEY_LOCAL_MACHINE'],
['hku', 'HKEY_USERS'],
['hkcc', 'HKEY_CURRENT_CONFIG'],
['左', '右'],
['上', '下'],
['水平', '垂直'],
]
let word = a:word
"word前后内容
let idx0 = stridx(getline('.'), word, col('.')-strlen(word))
let strBefore = strpart(getline('.'), 0, idx0)
let strAfter = strpart(getline('.'), idx0+strlen(word))
"echom strBefore .'--'. strAfter .'--'. word
"lPair 转成 dic(相互为key,方便判断)
let dic = {}
for lPair in lis
let dic[lPair[0]] = lPair[1]
let dic[lPair[1]] = lPair[0]
endfor
"NOTE dic 额外增加单向的内容
let dic['int'] = 'integer'
let dic['integer'] = '^d+$'
"处理单词大小写
let upCase = hy_string#modify#upperCase(word)
let wordFixed = upCase > 0 ? tolower(word) : word "wordFixed 用来调整大小写
"echom word .' '. wordFixed
if !has_key(dic,wordFixed) "不在定义列表内
"echom 'not in dic'
"aUp → aDown
if word =~# '[A-Z]'
let char = ''
let l = hy_string#split#byUpper(word)
else
let char = matchstr(word, 'c[^a-z]')
let l = split(word, 'c[^a-z]')
endif
for i in range(len(l))
let upCase = hy_string#modify#upperCase(l[i])
let wordFixed = upCase > 0 ? tolower(l[i]) : word "wordFixed 用来调整大小写
if has_key(dic, wordFixed) "找到匹配
let res = dic[wordFixed]
"恢复大小写
if upCase == 1
let res = tolower(res)
elseif upCase == 2
let res = toupper(res)
elseif upCase == 3
let res = substitute(res, '.*', 'Lu&', '')
endif
"修改 l
let l[i] = res
return join(l, char)
endif
endfor
return word
else
let res = dic[wordFixed]
if upCase == 1
let res = tolower(res)
elseif upCase == 2
let res = toupper(res)
elseif upCase == 3
let res = substitute(res, '.*', 'Lu&', '')
endif
endif
return res
" execute "normal! ciw" . res
"call setline(line('.'), strBefore . res . strAfter)
endfunction
"获取字符串的大小写情况
"1为小写
"2为大写
"3为首字母大写
"0为其他情况(strUpper)
function! hy_string#modify#upperCase(str) abort
if a:str !~ 'a' "没字母
return 0
elseif a:str ==# tolower(a:str)
return 1
elseif a:str ==# toupper(a:str)
return 2
elseif a:str =~# 'v^ul*$'
return 3
else
return 0
endfunction
"abcUp转成 ['abc', 'Up']
function! hy_string#split#byUpper(str) abort
let char = '!'
let s = substitute(a:str, 'vC[A-Z]', char.'&', 'g') "大写字母前增加符号用来 split
if strcharpart(s, 0, 1) == char | let s = strcharpart(s, 1) | endif
return split(s, char)
endfunction