[英]How to open a terminal in new tab from VIM?
When I'm working with lets say 4 files, all are open in tabs(VIM).当我使用 4 个文件时,所有文件都在选项卡(VIM)中打开。 I want to save the changes and compile it without having to close the tabs, ie I want to open a terminal in new tab along with the existing 4?我想保存更改并编译它而不必关闭选项卡,即我想在新选项卡中打开一个终端以及现有的 4?
How should I do this in VIM?我应该如何在 VIM 中执行此操作?
:tab ter
opens a terminal in a new tab instead of opening it in a new window as :ter
does.在新选项卡中打开终端,而不是像:ter
那样在新窗口中打开终端。 You can also use the equivalent, longer :tab terminal
form.您还可以使用等效的更长的:tab terminal
形式。
Credits to user wolloda in this Reddit post . 在此 Reddit 帖子中感谢用户 wolloda。
Extra information额外的信息
Terminating the shell with Ctrl-d
or exit
closes the terminal buffer.使用Ctrl-d
或exit
终止 shell 会关闭终端缓冲区。
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). Ctrl-w
N
或Ctrl-\ Ctrl-n
将缓冲区置于终端正常模式:击键不转发到 shell,但 Vim 使用正常缓冲区(尽管 shell 作业仍在运行)。 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
, ...然后,您可以使用gt
更改选项卡,键入 Ex 命令,例如:ls
等。要使终端缓冲区恢复活力, 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.如果你要映射它,我推荐使用:tab ter++kill=hup
,这样当你:qa
终端作业不会阻止 Vim 退出。 And this is the signal normal terminal emulators send to its jobs when closed anyway.这是正常终端仿真器在关闭时发送给其工作的信号。
For terminal mode mappings, use tnoremap
, for example对于终端模式映射,请使用tnoremap
,例如
tnoremap <S-Tab> <CW>:tabprevious<CR> tnoremap <CN> <CW>N
More information on :help :terminal
and :help :tab
.有关:help :terminal
和:help :tab
的更多信息。
In 2019, vim now has a Terminal mode. 2019 年,vim 现在有了终端模式。
: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
Vim 8.1 now has a built in terminal that can be opened with the :term command. Vim 8.1现在有一个内置终端,可以用 :term 命令打开。 This provides much more complete integration with the rest of the Vim features.这提供了与其他 Vim 功能更完整的集成。
Original Answer:原答案:
I would suggest looking at tmux or screen .我建议查看tmux或screen 。 I use tmux myself and along with vim-tmux-navigator moving between the terminal and vim is very easy.我自己使用 tmux,并且与vim-tmux-navigator一起在终端和 vim 之间移动非常容易。
A more vim like way of doing this would be to use :make
一种更像 vim 的方法是使用:make
:make
will execute the 'makeprg'
. :make
将执行'makeprg'
。 It defaults to make
which is great of C projects它默认make
哪个是 C 项目中的佼佼者:make
the quickfix list will be contain any errors.运行:make
后,quickfix 列表将包含任何错误。:compiler
command.通过:compiler
命令设置你的编译器。:make foo-command
可以像这样传递额外的参数:make foo-command
%
.当前文件名可以用%
表示。 eg :make %
例如:make %
:cnext
and :cprev
to move between your errors.使用:cnext
和:cprev
在错误之间移动。:copen
to open up the quickfix list in a window ( :cclose
to close) :copen
在窗口中打开快速修复列表( :cclose
关闭):cwindow
to open quickfix list window only if there are errors :cwindow
仅在出现错误时打开快速修复列表窗口:cnext
and friends.可能想为:cnext
和朋友使用更好的映射。 I suggest Tim Pope's unimpaired plugin我建议 Tim Pope 的unimpaired插件<cz>
to suspend vim and run your build system.只需使用<cz>
暂停 vim 并运行您的构建系统。 (Cons: loose out on the quickfix list) (缺点:在快速修复列表中松散):!
使用:!
to compile.编译。 (Same cons as suspending) eg :!make
(与暂停相同的缺点)例如:!make
:make
SingleComplile尝试使用:make
完成一些工作If you are just starting out I would suggest you learn how to use :make
and the quickfix list.如果您刚刚开始,我建议您学习如何使用:make
和 quickfix 列表。 There is a nice Vimcast episode that intros the quickfix list: Search multiple files with :vimgrep .有一个很好的Vimcast插曲介绍了快速修复列表: 使用 :vimgrep 搜索多个文件。 Additionally Syntastic is a great way to get up and running with linters quickly.此外,Syntastic 是一种快速启动和运行 linter 的好方法。
Vim's tabs are not like most text editors tab. Vim 的选项卡与大多数文本编辑器选项卡不同。 They are more like viewports into a group of windows/splits.它们更像是一组窗口/拆分的视口。 Additionally, Vim is buffer centric, not tab centric like most editors.此外,Vim 以缓冲区为中心,而不是像大多数编辑器那样以选项卡为中心。 Therefore using features like the quickfix list is often easier without tabs (See :h 'switchbuf
if you must use tabs).因此,在没有选项卡的情况下,使用 quickfix 列表之类的功能通常会更容易(如果必须使用选项卡,请参阅:h 'switchbuf
)。 Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. Vim 的选项卡经常妨碍使用拆分,因为有更好的窗口和缓冲区导航命令可用。 I personally have many files open (sometimes 100+) use no tabs and use on average 1-2 splits without any issue.我个人打开了许多文件(有时超过 100 个),不使用选项卡,平均使用 1-2 次拆分,没有任何问题。 Bottom line: Learn to use buffers effectively .底线:学习有效地使用缓冲区。
For more help see the following:如需更多帮助,请参阅以下内容:
:h :make
:h 'makeprg
:h quickfix
:h :cnext
:h :cope
I was particularly looking for something like this. 我特别在寻找这样的东西。
Open all the files in tabs. 在选项卡中打开所有文件。 Then switch to terminal mode by ctrl z
, play whatever you want and get back to the tabs mode by typing fg
in terminal. 然后通过ctrl z
切换到终端模式,播放所需内容,然后在终端中键入fg
返回到选项卡模式。
The highest voted answer uses :tab ter
.投票最高的答案使用:tab ter
。 This doesn't work on NeoVim (at least for me).这在 NeoVim 上不起作用(至少对我来说)。 However, it's still fairly simple:但是,它仍然相当简单:
:tabe term://bash
tabe
is open a new tab
and e
dit file. tabe
是打开e
新tab
并编辑文件。
term://
is a NeoVim way of opening a terminal term://
是一种 NeoVim 打开终端的方式
bash
is the kind of shell you want to use (eg I use zsh, so my command is actually :tabe term://zsh
) bash
是您想要使用的那种 shell(例如我使用 zsh,所以我的命令实际上是:tabe term://zsh
)
Some helpful commands that I created:我创建的一些有用的命令:
" 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 : Ctrl-w:
That gets me to the command line, then one can enter tablast tabnext or tabprevious这让我进入命令行,然后可以输入 tablast tabnext 或 tabprevious
or the short versions tabl, tabn, tabp或短版 tabl、tabn、tabp
Or this way:或者这样:
Ctrl-w gt and Ctrl-w gT (Next tab and Previous Tab) Ctrl-w gt 和 Ctrl-w gT(下一个选项卡和上一个选项卡)
Or Ctrl-w Number gt (for a specific tab)或 Ctrl-w Number gt(用于特定选项卡)
That works too.这也有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.