2

I am trying to return this :GrepperAck ''<Left> from a Python function and use it in the following mapping

def invoke_search_command():
  return ":GrepperAck ''<Left>"
:nnoremap <expr> <leader>f py3eval('invoke_search_command()')

But the <Left> is printed literally like :GrepperGit ''<Left>.

But if I use :nnoremap <expr> <leader>f py3eval('invoke_search_command()') . '<Left>', it works. I tried escaping the returned string like ":GrepperAck ''\<Left>", but it is still getting printed literally. What am I doing wrong?

  • 1
    @BLayer Sorry, that was my bad. – RajaRaviVarma Jun 28 at 11:40
0

You definitely need the backslash version \<Left>. Is use of py3eval necessary? Unless you need to translate data structures from Python to Vim or something it would be better to return a straight string. You need to make sure the string :GrepperAck ''\<Left> is passed literally to the command line. Then it will be processed as an expression due to <expr>.

I don't have a Python installation but I can demonstrate the fundamental workings without it. Given...

func! Foo()
    return ":GrepperAck ''\<Left>"
endfunc

And this mapping...

nnoremap <expr> <leader>f Foo()

Then when I enter leader key and f this is what I'll see on the command line..

:GrepperAck ''

...and the cursor will be inside the quotes.

If you must use py3eval then one thing you might try is to wrap that in a string() function call.

nnoremap <expr> <leader>f string(py3eval(..))
  • I will try the string function call and let you know if this works, and accept this answer if it works. Thanks. – RajaRaviVarma Jun 28 at 11:44
  • @RajaRaviVarma Thank you. If it doesn't work and there are any additional details that might help let me know. I like to see a question/answer through to the end if I'm able. – B Layer Jun 28 at 12:24
  • Wrapping the whole expression with string() throws Unknown mark error. Sorry that didn't work. – RajaRaviVarma Jun 28 at 14:01
  • I also tried returning the value of the py3eval(..) function, from another vim function. The result was the same, it printed the literal \<Left> instead of moving the cursor left.. – RajaRaviVarma Jun 28 at 14:06
  • 1
    Yup, the pure vim function worked fine. – RajaRaviVarma Jul 1 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.