dictionary - 如何映射vim视觉模式来替换我选择的文本部分?

标签 dictionary select vim replace mode

我希望替换部分文字,由视觉模式选择。例如:

我有一个简单的文本文件:

------------------
hello there hehe
She's not here
------------------

我需要把所有的“他”变成“她”。

我想要做的不是输入%s命令,而是在可视模式下:

  1. v 选择“他”
  2. 按某个热键,vim 提示我输入新文本
  3. 输入新文本,按 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

您可以将映射 ; 更改为您想要的任何热键。

enter image description here

关于dictionary - 如何映射vim视觉模式来替换我选择的文本部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41238238/

相关文章:

javascript - jquery change() 事件表现得很奇怪

MYSQL 选择/分组依据 : How do I remove a group from the result based on a value of one of it's members?

javascript - JavaScript 中的单词词典及其翻译

java - 时间快照的数据结构

python - 使用 Python 中的单个键合并两个不同长度的字典列表

vim - 对 vim 配置警告进行故障排除

vim - 无法弄清楚某些 vimrc 设置

python - 为什么 Python repl 为 None 打印不同的东西?

MySQL SELECT WHERE IN 查询问题

Vim 重复点 (".") 命令缓冲区?