如何在vim正则表达式中搜索元字符或其他字符?
示例:(我希望这与元字符'\ w'或字符' - '匹配1次或更多次
[-\w]\+
Run Code Online (Sandbox Code Playgroud)
但是那个正则表达式并不像我希望的那样有效.它只匹配 - ,w和.我试图逃避'\',但这不起作用.
我查看了vim文档,但没有这个例子.
我有以下映射,允许粘贴从缓冲区缓冲区中的单词.(cpw =更改粘贴字)
nmap <silent> cpw "_cw<C-R>"<Esc>
我想做的是允许如下命令
cpiw(更改粘贴在word中 - >喜欢'iw'动作)
cpaw(更改粘贴一个单词 - >像'aw'运动一样)
对于任何运动{m} cp {m}
这是否可以允许在映射中,所以我不必为我想要使用它的每个动作编写nmap?
提前致谢.
编辑:错字修复.我的解决方案如下
在努力研究地图操作员之后,我成功地完成了一个完全符合我想要的功能.对于任何感兴趣的人,如下:
"This allows for change paste motion cp{motion}
nmap <silent> cp :set opfunc=ChangePaste<CR>g@
function! ChangePaste(type, ...)
silent exe "normal! `[v`]\"_c"
silent exe "normal! p"
endfunction
Run Code Online (Sandbox Code Playgroud)
编辑 - 可能更好的新版本.
"This allows for change paste motion cp{motion}
nmap <silent> cp :set opfunc=ChangePaste<CR>g@
function! ChangePaste(type, ...)
if a:0 " Invoked from Visual mode, use '< and '> marks.
silent exe "normal! `<" . a:type …
Run Code Online (Sandbox Code Playgroud) 我的核心问题是如何创建允许计数和运动的自定义映射.但我希望伯爵能够推翻议案.为了澄清我想要以下工作:
[count][cmd]
- 在[count]行上做一些有用的事情,而不是等待[动作].
[cmd][motion]
- 在线条的[运动]范围内做一些有用的事情.
我的确切方案是尝试向行添加注释,但我会将此信息用于我的vimrc中的其他映射.这是我到目前为止所拥有的.
"comment motion of lines
nmap <silent> ,c :set opfunc=Comment<CR>g@
"comment count lines
nmap <silent> ,cc :s/^/\/\//<CR>:noh<CR>
function! Comment(...)
silent exe "'[,']s/^/\\/\\//"
silent exe "noh"
endfunction
Run Code Online (Sandbox Code Playgroud)
,c[motion]
按动议评论.[count],cc
按计数评论一行.
我想,c[motion]
和[count],c
工作.
这可能吗?
编辑:澄清了我的问题.将"范围"更改为"计数"