我希望替换部分文字,由视觉模式选择。例如:
我有一个简单的文本文件:
------------------
hello there hehe
She's not here
------------------
我需要把所有的“他”变成“她”。
我想要做的不是输入%s
命令,而是在可视模式下:
v
选择“他”- 按某个热键,vim 提示我输入新文本
- 输入新文本,按 Enter,完成。
我想我们可以用 vmap 来做到这一点?但如何实现呢?谢谢!
最佳答案
要获得解决方案,所有积分均归用户@ xolox来自他的answer我开发它是为了完成所需的任务:
vnoremap ; :call Get_visual_selection()<cr>
function! Get_visual_selection()
" Why is this not a built-in Vim script function?!
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
let selection = join(lines,'\n')
let change = input('Change the selection with: ')
execute ":%s/".selection."/".change."/g"
endfunction
您可以将映射 ;
更改为您想要的任何热键。
关于dictionary - 如何映射vim视觉模式来替换我选择的文本部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41238238/