vimscript:为什么\S*\%>66c。*/\=替代?=~\\+”返回true - vimscript: why does '\S*\%>66c.*/\=substitute…'=~'\a\+' return true

- 此内容更新于:2015-12-20



Here is a mapping

map <silent> <2-LeftMouse> lB"gyE  
:if @g=~'ss\d\d\d\.png'  
  :call writefile([@g], "/home/g/xv.vss", "a")  
:elseif @g=~'http'  
  :call writefile([@g], "/home/g/dil.lo", "a")  
:elseif @g=~'_\d\+'  
  :let @/='^'.@g.'$'  
  :norm nzz  
:elseif @g=~'\a\+'  
  :vim /^\cg/ /mnt/1/dict/webster/all.txt  
  :norm 0y$:@"  

Here is the content of "g


Here are the messages produced when the mapping ran

E303: Unable to open swap file for "=substitute(submatch(0),'/", recovery impossible  
Cannot open file "=substitute(submatch(0),'/"  
"all.txt" 530590 lines --0%--  

Apparently vim considers the content of "g is equivalent to \a\+ because it duly opened all.txt and tried to find a match in it (webster's dictionary) to @g and the only route to that file is thru the mentioned test.

Could anyone explain to me why it does this, or an alternative interpretation?


(原文:Might I ask what you are doing because its rather hard to follow. But to answer your question =~ does not put anchors for you automatically. So anything with an alphabetic character matches. You want something like =~ '^\a\+$'.)


(原文:Thank you, that solved the problem, but how do I give you credit for it?)


(原文:I can post an answer later.)


=~ checks to see if the pattern matches. It does not add anchors for you so if any part of the string matches then the string counts as matched.

If you want to only accept alphabetic characters you want to use '^\a\+$' instead of \a\+.