【发布时间】:2010-11-16 17:15:18
【问题描述】:
我编写了这个脚本,将光标周围的许多空格替换为一个空格。但是,当我在光标周围没有空格的情况下使用它时,这不起作用。在我看来,Vim 不会在零宽度匹配上替换。
function JustOneSpace()
let save_cursor = getpos(".")
let pos = searchpos(' \+', 'bc')
s/\s*\%#\s*/ /e
let save_cursor[2] = pos[1] + 1
call setpos('.', save_cursor)
endfunction
nmap <space> :call JustOneSpace()<cr>
这里有几个例子(管道|是光标):
这一行
hello | world
变成
hello |world
但是这一行
hello wo|rld
不会变成
hello wo |rld
更新:通过将函数更改为以下内容,它适用于上述示例。
function JustOneSpace()
let save_cursor = getpos(".")
let pos = searchpos(' *', 'bc')
s/\s*\%#\s*/ /e
let save_cursor[2] = pos[1] + 1
call setpos('.', save_cursor)
endfunction
这一行
hello |world
变成
hello w|orld
问题是光标移动到下一个字符。它应该留在同一个地方。
任何提示和/或提示?
【问题讨论】: