为Vim的命令行创建映射,该映射在插入之前转义寄存器的内容 [英] Create a mapping for Vim's command-line that escapes the contents of a register before inserting it

查看:133
本文介绍了为Vim的命令行创建映射,该映射在插入之前转义寄存器的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的文件,并且我想搜索URL的所有出现:

Suppose that I have a document like this, and I want to search for all occurences of the URL:

Vim resources: [http://example.com/search?q=vim][q]
...
[q]: http://example.com/search?q=vim

我不想全部输入,所以我将光标放在第一个URL上,并运行uyi [将其转换为'u'注册表,现在搜索它,我想将该注册表的内容粘贴到搜索字段通过运行:

I don't want to type it out in full, so I'll place my cursor on the first URL, and run "uyi[ to yank it into the 'u' register. Now to search for it, I'd like to just paste the contents of that register into the search field by running:

/\V<c-r>u<CR> 

这导致Vim搜索字符串http: - 因为'/'字符终止搜索字段。

This results in Vim searching for the string 'http:' - because the '/' character terminates the search field.

我可以通过运行它来解决问题:

I can get around the problem by running this instead:

/\V<c-r>=escape(@u, '\/')<CR><CR>

但是这是很多打字!

如何创建一个mappi为Vim的命令行简化了这个工作流程?

How can I create a mapping for Vim's commandline that simplifies this workflow?

我的理想工作流程如下:

My ideal workflow would go something like this:


  • / \V 以显示搜索提示,并使用非常的nomagic模式

  • 点击ctrl-x触发自定义映射(ctrl-x可用)

  • Vim监听下一个按键...(按&Esc。> 将取消)

  • 按'u'将转义'u'注册表的内容,并插入命令行

  • press /\V to bring up the search prompt, and use very nomagic mode
  • hit ctrl-x to trigger the custom mapping (ctrl-x is available)
  • Vim listens for the next key press... (pressing <Esc> would cancel)
  • pressing 'u' would escape the contents of the 'u' register, and insert on the command line

推荐答案

尝试这样:

cnoremap <c-x> <c-r>=<SID>PasteEscaped()<cr>
function! s:PasteEscaped()
  " show some kind of feedback
  echo ":".getcmdline()."..."

  " get a character from the user
  let char = getchar()

  if char == "\<esc>"
    return ''
  else
    let register_content = getreg(nr2char(char))
    return escape(register_content, '\/')
  endif
endfunction

顺便说一句,可能有用的知道(如果你还没有)您可以使用作为:s 的分隔符。这意味着你可以像这样写一个url的搜索替换:

By the way, something that might be useful to know (if you don't already) is that you can use ? as the delimiter for :s. Which means that you could write a search-and-replace for an url like so:

:s?http://foo.com?http://bar.com?g

这篇关于为Vim的命令行创建映射,该映射在插入之前转义寄存器的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆