微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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, "\n")
    "     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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐