0

Running 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?

4
  • 2
    It's not clear what your question is. What are you expecting it to do? Commented Jan 11, 2017 at 4:37
  • hi Herb Wolfe,first there is a mistake above:Move to left. As picture show above,two of three situations work fine(execute command),but execute g:xx is not work Commented Jan 11, 2017 at 4:59
  • Can you post the actual file, and not just a screen shot? It's difficult to tell if that's the entire file, and it's easier to copy and paste text for testing. Commented Jan 11, 2017 at 5:03
  • sorry,i have posted the test code. Commented Jan 11, 2017 at 5:07

2 Answers 2

1
let g:temp=eval('"'. g:temp .'"')

after

let g:temp=escape(matchstr(g:str,'\(\t\)\@<=.*$'),'<')

Extracting this into a :function:

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

Explanation

The problem was that escape() handles its input° and output strings as a literal-string (as in single quotes ''). So escaping the < in "<C-w>" results in '\<C-w>', not the desired "\<C-w>"/'^W'.

° If the input is given as a double quoted "string", its escape sequences are evaluated by the vimscript parser, before it is passed to escape().

1
  • 1
    @tracyone, gladly! (Note I edited my answer to expand on it.) If you feel it has answered your question well, please ✔ accept and ↑ upvote it: vi.stackexchange.com/help/someone-answers Commented Jan 11, 2017 at 10:32
-1

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.

3
  • Is there a vim function can translate this keycode,I mean <c-w> to ^w. Commented Jan 11, 2017 at 6:11
  • Sorry, no idea. Commented Jan 11, 2017 at 6:20
  • Yes, eval() can do that 'with a little help' – see my answer. Commented Jan 11, 2017 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.