How to open a terminal in new tab from VIM?

时间秒杀一切 提交于 2020-12-08 07:32:46

问题


When I'm working with lets say 4 files, all are open in tabs(VIM). I want to save the changes and compile it without having to close the tabs, i.e I want to open a terminal in new tab along with the existing 4?

How should I do this in VIM?


回答1:


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.




回答2:


:tab ter

should open 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.

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




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/26411645/how-to-open-a-terminal-in-new-tab-from-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!