execute g:xx doesn't work.
let g:str="move to right win\t<C-w>h" let g:temp=escape(matchstr(g:str,'\(\t\)\@<=.*$'),'<') let g:xx= "normal ".g:temp execute g:xx
Can anyone explain why?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
let g:temp=eval('"'. g:temp .'"')
Extracting this into a
function! Literalize(string) " get already properly escaped sequences out of the way, so we don't double-escape them let s = eval('"'. a:string .'"') " escape remaining special key sequences let s = escape(s, '<') let s = eval('"'. s .'"') return s endfunction
The problem was that
escape() handles its input° and output strings as a literal-string (as in single quotes
''). So escaping the
"<C-w>" results in
'\<C-w>', not the desired
° If the input is given as a double quoted
"string", its escape sequences are evaluated by the vimscript parser, before it is passed to
In your first line, the
<C-w> is not escaped, so it appears to be matched as a literal string, and the
escape function doesn't convert it to the ^W character.
So to get what you want, the first line should actually read
let g:str="move to right win\t\<C-w>h"
In addition, according to the documentation here at sourceforge, the escape command doesn't work how you appear to think. It's going to escape just the '<' character, which does nothing.