1

I want to return to the open file after I executed command with :terminal <command>

First off, I am using neovim.

So to get to the situation, I want to improve: follow these steps :

  1. create a sample file with nvim foo
  2. write some text, e.g. hello you and save :w
  3. in vim command mode, run :terminal cat foo

Currently, what happens? I can see how the terminal returns my output hello you.

My question is, how do I return to my file foo?

When I type :q I quit vim. Then I could run :e foo, but I prefer a more elegant solution. Since when I run :! cat foo I get

:! cat foo Hello you Press ENTER or type command to continue 

So that a simple enter returns me to my file. Is there a similar way to do this with :terminal

Remark I prefer :terminal since it has the terminal colors which help in reading longer test results. Surely, I am using the cat command just as an example.

update

After some digging in the documentation link. I found that the terminal has its own mapping so that I can change the esc key to get into command mode with

:tnoremap <Esc> <C-\><C-n> 

I assumed that just adding :!q like :tnoremap <Esc> <C-\><C-n>:!q would quit but that does not work.

But when using :tnoremap <Esc> <C-\><C-n> in the command I type :!q I get

E37: No write since last change E162: No write since last change for buffer "[No Name]" Press ENTER or type command to continue 

That means I only need to add force stop into shortcut Does anybody know how to do that?

6
  • Why not just move to the previous split with <c-w>p? Maybe I don't fully understand your question... but do you really need to close the terminal window? Commented Aug 18, 2022 at 12:46
  • Any of C-w c or C-w w or C-w p would do… also see :help windows and :help terminal for details on using :terminal Commented Aug 18, 2022 at 13:13
  • 1
    I'd recommend keeping the terminal window open, and move between windows with <c-w> h/j/k/l or similar commands. Otherwise, you can close the terminal window with <c-\><c-n>:q! (notice it's :q! instead of :!q), or better, with <c-w><c-c>. Commented Aug 18, 2022 at 13:42
  • @husB I tried :tnoremap <Esc> <C-\><C-n>:q! and it did not close on <kbd>esc<\kbd>. Well I just want to reproduce the behaviour of from :terminal <cmd> similar to ` :! <cmd> Commented Aug 18, 2022 at 13:59
  • You need to terminate the command with <cr>, ie. :tnoremap <Esc> <C-\><C-n>:q!<cr> . Otherwise use <c-w><c-c>. It's much neater too --- :tnoremap <Esc> <C-w><C-c>. Commented Aug 18, 2022 at 15:08

2 Answers 2

0

If terminal process has already finished then Vim/Neovim is back in Normal mode. So you can do absolutely anything that is available in Normal mode for buffer switching. For example, :h ctrl-^. More info for beginners can be found in :h user-manual, specifically, the section "Editing more than one file" in :h usr_07.txt.

0

You could change the :shortmess=a option or try :silent !cat foo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.