如何从 VIM 在新选项卡中打开终端?

当我使用 4 个文件时,所有文件都在选项卡(VIM)中打开。我想保存更改并编译它而不必关闭选项卡,即我想在新选项卡中打开一个终端以及现有的 4?

我应该如何在 VIM 中执行此操作?

stack overflow How to open a terminal in new tab from VIM?
原文答案
author avatar

接受的答案

Vim 8.1 now has a built in terminal that can be opened with the :term command. This provides much more complete integration with the rest of the Vim features.


Original Answer:

I would suggest looking at tmux or screen. I use tmux myself and along with vim-tmux-navigator moving between the terminal and vim is very easy.


答案:

作者头像
:tab ter

opens a terminal in a new tab instead of opening it in a new window as :ter does. You can also use the equivalent, longer :tab terminal form.

Credits to user wolloda in this Reddit post.

Extra information

  • Terminating the shell with Ctrl-d or exit closes the terminal buffer.

  • Ctrl-w N or Ctrl-\ Ctrl-n put the buffer in the terminal-normal mode: Keystrokes are not forwarded to the shell, but are used by Vim as in a normal buffer (although the shell job is still running). Then you can use gt to change tabs, type Ex commands such as :ls, etc. To bring the terminal buffer back to life, i, a, ...

  • If you are going to map this, I recommend using :tab ter++kill=hup, so that when you :qa the terminal job does not prevent Vim from quitting. And this is the signal normal terminal emulators send to its jobs when closed anyway.

  • For terminal mode mappings, use tnoremap, for example

    tnoremap <S-Tab> <C-W>:tabprevious<CR>
    tnoremap <C-N>   <C-W>N
    

More information on :help :terminal and :help :tab.

作者头像

In 2019, vim now has a Terminal mode.

:help terminal

For example, you can use it like this.

# go to terminal-job mode
:terminal

# go to terminal-normal mode
ctrl-w N

# go back to terminal-job mode
i
作者头像

A more vim like way of doing this would be to use :make

:make

  • :make will execute the 'makeprg'. It defaults to make which is great of C projects
  • After running :make the quickfix list will be contain any errors.
  • Set your compiler via the :compiler command.
  • Extra parameter can be passed like so :make foo-command
  • Current filename can be represented by %. e.g. :make %

quickfix list

  • Use :cnext and :cprev to move between your errors.
  • :copen to open up the quickfix list in a window (:cclose to close)
  • :cwindow to open quickfix list window only if there are errors
  • May want to use better mappings for :cnext and friends. I suggest Tim Pope's unimpaired plugin

Alternatives and Plugins

  • Just use <c-z> to suspend vim and run your build system. (Cons: loose out on the quickfix list)
  • Use :! to compile. (Same cons as suspending) e.g. :!make
  • Syntastic is a syntax checking system that checks files on save
  • Dispatch can be used to run things in the background. Great for test suites
  • As @brettanomyces mentioned you may want to consider terminal multiplexers like tmux or screen.
  • SingleComplile tries and takes some of the work out of using :make

Conclusion

If you are just starting out I would suggest you learn how to use :make and the quickfix list. There is a nice Vimcast episode that intros the quickfix list: Search multiple files with :vimgrep. Additionally Syntastic is a great way to get up and running with linters quickly.

Aside about tabs

Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. Therefore using features like the quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) use no tabs and use on average 1-2 splits without any issue. Bottom line: Learn to use buffers effectively.

For more help see the following:

:h :make
:h 'makeprg
:h quickfix
:h :cnext
:h :cope
作者头像

适用于使用 NeoVim 的任何人:

最高投票的答案使用 :tab ter 。这对Neovim不起作用(至少对我来说)。但是,它仍然很简单:

:tabe term://bash

tabe 是打开新的 tabe DIT文件。

term:// 是打开终端的新方法

bash 是您要使用的外壳(例如我使用ZSH,所以我的命令实际上是 :tabe term://zsh

我创建了一些有用的命令:

" open terminal
if has('nvim')
    command Terminal vsplit term://zsh
    command TerminalTab tabe term://zsh
else
    command Terminal vert term
    command TerminalTab tab ter
endif
作者头像

Another way

Ctrl-w :

That gets me to the command line, then one can enter tablast tabnext or tabprevious

or the short versions tabl, tabn, tabp

Or this way:

Ctrl-w gt and Ctrl-w gT (Next tab and Previous Tab)

Or Ctrl-w Number gt (for a specific tab)

That works too.