0

<C-\><C-n> is working as (I'd) exepected, but not from a Vimscript.

I am trying to make a script that will:

  • open Neovim's :terminal
  • run a python script
  • exit from the terminal input mode
  • manipulate the terminal buffer just as text

I already do that manually everyday. That's why the script would help. (:

But I can't find a way to give the <C-\><C-n> to the terminal buffer.

Here is the most 'consistent piece' achieved so far:

function! AFunction(...)

let firstArg=a:0
let secondArg=a:1
let thirdArg=a:2
let fourthArg=a:3
let fifthArg=a:4

command! -nargs=* AFunctionCommand call Fetching(<f-args>) 
execute ":terminal python a-python-script.py" a:1 a:2 a:3 a:4

endfunction

It does work, but I can't find a way to pass the exit key. The first idea was to concatenate the strings, as mentioned here. Perhaps something that would allow to stablish where is the <cr> (after the arguments):

execute ":terminal python a-python-script.py" a:1 a:2 a:3 a:4 "\<cr>" . '<C-\><C-n>'

If try to do something like:

execute ":terminal python a-python-script.py" a:1 a:2 a:3 a:4
<C-\><C-n>

It doesn't work since terminal has 'nomodifiable' by the default. The same if trying to call a function with the escape keys.

Yes, it is a horrible Vimscript art, and any comment on the code would be really appreciated, but since this is not Code Review, I would be happy with just the reason why doesn't work, or where is my logic not working. (:

6
  • 1
    CTRL-\ CTRL-N is a normal mode command, so you would need :exe "norm! \<C-\>\<C-N>" and yes, you need to make sure to use double quotation marks Mar 27, 2017 at 11:33
  • Thanks for your answer @Christian-Brabandt, I didn't realized that the <C* should be escaped. But the problem is that after :exe ":term..." a:*.. it looks like the 'terminal' process was never done, and nothing below that will be parsed... I commented out the execute line and tested with these two in instead: execute ":terminal " and exe "norm! \<C-\>\<C-N>". Thanks again, it's getting close. (:
    – lsrdg
    Mar 27, 2017 at 12:02
  • Are you sure this isn't something that you want to run under 'makeprg'?
    – Vitor
    Mar 29, 2017 at 14:42
  • Nope, not sure about anything on this. Is there some information that could be added/rephrased to the question?
    – lsrdg
    Mar 29, 2017 at 18:03
  • 1
    Unfortunately :normal cannot be used while in terminal-mode (AKA "insert-mode in a terminal buffer"). Use feedkeys("\<C-\><C-N>") instead (and no the 2nd "\" is not escaped... yay vimscript) Mar 29, 2017 at 22:14

1 Answer 1

2

:normal cannot be used while in terminal-mode (AKA "insert-mode in a terminal buffer"). Instead feedkeys() can be used (the second \ is not escaped--yet another Vimscript quirk):

:call feedkeys("\<C-\><C-N>")

For your specific case, jamming a feedkeys() call immediately after the :execute doesn't seem to work (could be that "typeahead" is processed before :terminal initializes). Instead, use :stopinsert:

execute ":terminal python a-python-script.py" a:1 a:2 a:3 a:4
stopinsert

Note that the TermOpen autocmd is also available for terminal initialization.

Your Answer

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

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