Vim 直接打开网址的 map 设置(重点是字符转义)

这只是个小功能,但在转义符上也被卡了很久,导致打开的网址只有一部分,特此分享,主要步骤有:

  1. 选中网址
  2. 获取选中内容,可以使用如下函数,注释掉的是另一种方法
    "获取visual模式下选中的内容
    " from interestingwords
    " Why is this not a built-in Vim script function?!
    function! hy_string#get#select() abort
      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:]
      return join(lines, "
    ")
    "     let temp = @s
    "     normal! gv"sy
    "     let res = @s
    "     let @s = temp
    "     return res
    endfunction
  3. 指定浏览器访问选中内容,重点:转义符是 ^,而不是常规的 ,&符要转成^&即可 里面的g:browser 可以自行换成自己的浏览器路径
vnoremap <F3> :call SmartRun(hy_string#get#select())<cr>
function! SmartRun(str)
    if a:str =~? '^http'
        execute 'silent! !start /b '.g:browser.' '.escape(substitute(a:str,'&','^&','g'), '#')
    elseif a:str =~? 'v^{x{8}(-x{4}){3}-x{12}}$' "clsid
        execute '!start explorer.exe shell:::' . a:str
    else
        execute 'silent! !start ' . a:str
    endif
endfunction
原文地址:https://www.cnblogs.com/hyaray/p/14397346.html