vim – 绕枢轴交换选择
(不是
Swap text around equal sign的可能重复:)
我经常发现自己在交换东西.这是屁股的痛苦. 我写完这段代码之后就说吧 tmp = realloc (words,sizeof (char **) * (*count + 1)); 我注意到连续的星号太多了,不喜欢它,并希望在乘法星号周围交换这两个因子. 或者,我写 #if !defined(_CONSOLE_H_) && defined (__MINGW32__) 但是我突然意识到定义的(__MINGW32__)必须首先出于某种原因. 我想如果我可以做这样的事情会很酷: (在字符x上,[x]表示光标位置.< S>代表这种假设的“交换”命令) #if [!]defined(_CONSOLE_H_) && defined (__MINGW32__) 命令:vf&< S> $=>选择从光标到枢轴(单词&&),并将选择与文本交换到行尾. 或者,对于前一个例子: tmp = realloc (words,[s]izeof (char **) * (*count + 1)); 命令:v3f *< S> f)=>从这里选择到第三个*,用文本向前交换到). 对我来说,这将是非常有用的.有没有这样的东西,或者我必须编写自己的插件? 谢谢! 编辑 – 作为@ib.他在回答的评论中说,我需要更具体地说明什么是支点. 枢轴也可以是一个角色,例如: static char ** tokenize_input (char * input,PDWORD count); 我可能想要围绕“,”交换两个参数.更确切地说,“,”. 所以也许我需要两个命令: <s> - char-wise - the pivot is the last character of the selection; <S> - word-wise - the pivot is the last word of the selection. 谢谢! 解决方法
这似乎在这里工作,让我知道它是如何做的:
function! Swap(...) "{{{ let start_v = col("'<") let end_v = col("'>") let mv = '' let isMv = 0 while !isMv let char = s:GetChar() if char == '<Esc>' return '' endif let mv .= char let isMv = s:IsMovement(mv) echon mv."r" endwhile if isMv == 2 return '' endif exec "normal! ".end_v.'|'.mv let lhs = '%'.start_v.'c(.{-}S)' if !a:0 let pivot = '(s*%'.(end_v).'c.s*)' else let pivot = '(s*'.a:1.'*%'.(end_v).'c'.a:1.'+s*)' endif let rhs = '(.*%#.)' exec 's/'.lhs.pivot.rhs.'/321/' endfunction "Swap }}} function! s:GetChar() "{{{ let char = getchar() if type(char) == type(0) && char < 33 return '<Esc>' elseif char let char = nr2char(char) endif return char endfunction "GetChar }}} function! s:IsMovement(mv) "{{{ let ft = a:mv =~ '^Cd*[fFtT].$' let ft_partial = a:mv =~ '^Cd*%([fFtT].?)?$' let right = a:mv =~ '^d*[l$|;,]|g[m$]$' let right_partial = a:mv =~ '^d*%([l$|;,]|g[m$]?)?$' if !right_partial && !ft_partial return 2 endif return ft || right endfunction "IsMovement2Right }}} " Use last char as pivot. e.g.: the comma in the given example. vmap <silent> <leader>s1 :<C-U>call Swap()<CR> " Use S+ (WORD) as pivot. e.g.: && vmap <silent> <leader>ss :<C-U>call Swap('S')<CR> " Use w+ as pivot. vmap <silent> <leader>sw :<C-U>call Swap('w')<CR> 现在无需按Enter键指定移动. ‘pivot’总是包含任何周围的空格,并且可以使用单个字符类来定义,该字符类作为映射中的Swap()的参数给出. 如果要突出显示,请检查https://gist.github.com/1921196 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |