How can I open a Shell inside a Vim Window? - vim

I can open a shell by using the :shell command in Vim, however I can't edit a file and at the same time use the shell.
Is there any way to split Vim in many Windows (or tabs), and have a shell opened in one of them?

Neovim and Vim 8.2 support this natively via the :ter[minal] command.
See terminal-window in the docs for details.

Well it depends on your OS - actually I did not test it on MS Windows - but Conque is one of the best plugins out there.
Actually, it can be better, but works.

:vsp or :sp - splits vim into two instance but you cannot use :shell in only one of them.
Why not display another tab of the terminal not another tab of vim. If you like the idea you can try it: Ctrl-shift-t. and move between them with Ctrl - pageup and Ctrl - pagedown
If you want just a few shell commands you can make any shell command in vim using !
For example :!./a.out.

You can use tmux or screen (second is able to do only horizontal splits without a patch) to split your terminal. But I do not know the way to have one instance of Vim in both panes.

If you haven't found out yet, you can use the amazing screen plugin.
Conque is also exceptional but I find screen much more practical (it wont "litter" your buffer for example and you can just send the commands that you really want after editing them in your buffer)

I guess this is a fairly old question, but now in 2017. We have neovim, which is a fork of vim which adds terminal support.
So invoking :term would open a terminal window. The beauty of this solution as opposed to using tmux (a terminal multiplexer) is that you'll have the same window bindings as your vim setup. neovim is compatible with vim, so you can basically copy and paste your .vimrc and it will just work.
More advantages are you can switch to normal mode on the opened terminal and you can do basic copy and editing. It is also pretty useful for git commits too I guess, since everything in your buffer you can use in auto-complete.
I'll update this answer since vim is also planning to release terminal support, probably in vim 8.1. You can follow the progress here:
https://groups.google.com/forum/#!topic/vim_dev/Q9gUWGCeTXM
Once it's released, I do believe this is a more superior setup than using tmux.

Shougo's VimShell, which can auto-complete file names if used with neocomplcache

Not absolutely what you are asking for, but you may be interested by my plugin vim-notebook which allows the user to keep a background process alive and to make it evaluate part of the current document (and to write the output in the document). It is intended to be used on notebook-style documents containing pieces of code to be evaluated.

With Vim 8.0 or later you can run a terminal emulator in a vim window by using the terminal feature. BTW if you want to simulate modern IDE terminal (like VSCode integrated terminal) in gVim or MacVim, you can put the following configuration in you vimrc.
set shell=/path/to/shell
" Make sure to replace `sh.exe` in BufNr("sh.exe") with your shell executable.
nnoremap <expr> <space> BufNr("sh.exe") > 0 ? (&buftype == 'terminal' ? '<c-^>' : ':b '. BufNr("sh.exe") . '<cr>') : ':terminal ++curwin<cr>'
function! BufNr(pattern)
let bufcount = bufnr("$")
let currbufnr = 1
let nummatches = 0
let firstmatchingbufnr = 0
while currbufnr <= bufcount
if(bufexists(currbufnr))
let currbufname = bufname(currbufnr)
if(match(currbufname, a:pattern) > -1)
let nummatches += 1
let firstmatchingbufnr = currbufnr
endif
endif
let currbufnr = currbufnr + 1
endwhile
return firstmatchingbufnr
endf
Now you can use space in normal mode (or whatever mapping you chosen) to:
Open a terminal in current window if terminal doesn't exists yet.
Switch to terminal buffer if current buffer is not a terminal type.
Switch to previous buffer if current buffer is a terminal buffer.

You may want to open a "screen" program,
split screen, open shell on one and vim on another.
Works for me.

I am currently using tmux.
Installation: sudo apt-get install tmux
Run it: tmux
Ctrl + b followed by Ctr + % : it splits your terminal window in two vertical halves.
Ctrl + "arrow left | arrow right" : moves between terminals.

Related

Vim windows navigation inside vim inside vim-terminal not working as expected

Vim windows navigation breaks for me once I perform these steps:
from (bash in my case) terminal open vim (8.1) in terminal mode
>vim -c ":term ++curwin"
then from the vim-terminal open vim
>vim
From inside vim, open a new split window
:sp
try navigating
<C-w><C-w>
and it doesn't work
Using Vim as a terminal emulator and Vim again inside of it, I see. Are you trying to create a black hole?
Anyway, if the built-in terminal allowed this it would function as a keyboard trap, effectively making it impossible to move the focus to another (host) Vim window.
It's really a classic case of the host capturing a keystroke and preventing the hosted program to receive it. Pretty much like screen using <C-a> as leader and thus preventing you from incrementing the number under the cursor in Vim.
And just like screen that lets you do <C-a>a to send an actual <C-a> to the hosted program, Vim's built-in terminal lets you do <C-w>. to send an actual <C-w>.
Therefore, you can do <C-w>.<C-w>. to achieve your goal (however misguided it might be).
See :help terminal-typing.

How to start Erlang shell from inside Vim

I want to use Vim as the editor for my Erlang coding.
I managed to install the plugin for Erlang in vim, and I want to know if it is possible to
compile the current buffer inside vim? In Emacs for example you can start Erlang shell using Ctrl C + Ctrl L
key combination. So is there a alternative in Vim to start the Erlang shell and then compile the current buffer?
For a quick shell command from inside Vim, use :!command -a -b arg1 (e.g. :!ls -l, :!erl %).
But Emacs is not Vim, they have different ideas behind both. Vim is just a text editor with programming extensions, it's not a place for terminal inside (just as for tetris and M-x doctor).
Vim maintainers refuse to add a full-blown terminal implementation into Vim itself (for more details, see :help shell-window ).
If you still want an in-buffer shell, you can take a look at ConqueTerm , but it does not work seamlessly. I tried it, but prefer using tmux / screen to split my console's screen estate into windows.
I also use the same vimerl plugin for erlang development in vim. However, I had to make a small change to it to make some improvements to the way it compiles for checking.
Once I'd made these changes, writing the file (e.g. with :w) caused vimerl to compile that source file and put the warnings/errors in the quickfix list (exactly the same way that :make would do if you had a Makefile in place).
FWIW, I also have the following configuration in my vimrc:
let g:erlangHighlightErrors = 1
let g:erlangHighlightBif = 1
let g:erlangCompletionDisplayDoc = 1
let g:erlangWranglerPath = "/usr/local/share/wrangler"
let g:erlangRefactoring = 1

How to change vim cursor shape in different modes

I use vim under iTerm2 in Mac, or putty in windows. I want to see different vim cursor shape in different vim mode. I know gvim & macvim have this feature, and there's an article to figure out how to do that in gnome-terminal & KConsole. But I can't search out any solution in iTerm2 and Putty.
This plugin by the great Steve Losh takes care of iTerm2. I don't know about Putty.
The plugin by Steve Losh (https://github.com/sjl/vitality.vim) is a little buggy for some (adds weird characters in place of the cursor when you switch between tabs in iTerm2).
Adding this to your .vimrc works for me:
" Change cursor shape between insert and normal mode in iTerm2.app
if $TERM_PROGRAM =~ "iTerm"
let &t_SI = "\<Esc>]50;CursorShape=1\x7" " Vertical bar in insert mode
let &t_EI = "\<Esc>]50;CursorShape=0\x7" " Block in normal mode
endif
Credit goes to Erlend Hamberg:
http://hamberg.no/erlend/posts/2014-03-09-change-vim-cursor-in-iterm.html
I use this plugin since a long time, and it works perfectly with Konsole but it also supports a lot of terms.
The documentation says:
Currently supported terminals are iTerm2 for the Mac (version 1.0.0.20130602
beta or better is required), VTE3 based terminals (including gnome-terminal),
and KDE's Konsole. The xterm console is partially supported as well. Older
xterm's didn't support the line cursor, so this plugin currently sets the
cursor to underline instead.
Older versions of VTE3 based terminals (before v0.39) do not support changing
the cursor via escape sequences and are not supported. On unsupported
terminals, Vim's default behavior is left unaltered.
The plugin also supports tmux, and will change your cursor inside a tmux
session too.
I think you should not install any plugins,you can change the [putty reconfiguration]->Window->Appearance -> [Cursor appearance] ->[Vertical line],
it is more convenient.
And I think it is unnessary to change the vim cursor,because it will hardly find the cursor if you change the shape!

Ctrl + S is not working as a horizontal split in VIM when using CommandT

I've been tweaking my ~/.vim quite a lot lately, and it seems that I broke something.
I'm using the CommandT plugin, which when the search is open allows for the following:
enter - open the file in current buffer
Ctrl + S - open the file in horizontal split window
Ctrl + V - open the file in vertical split window
The problem is, that Ctrl + S suddenly stopped working in terminal VIM, only Ctrl + V is working. At first I thought I broke something in my general config, but then I tried it in MacVim, and Ctrl + S works just fine.
I'm not aware of having set anything specific only to the terminal VIM.
Is there any way that I can tell, if a key is being rebound to something else and where is that happening?
btw, my ~/.vim is a fork of Janus with most stuff removed and changed, I'm keeping it mostly for the Rakefile for installing plugins. here's the repo
I've found solution for this problem here: https://apple.stackexchange.com/a/34503
Just put in your .bashrc / .zshrc this line of code:
stty -ixon -ixoff
You can list all current mappings by typing :map inside the command window.
You could also clear all maps using :mapclear and then map <C-S> again.
It could be that you vimrc contains alternate maps depending on the environment. Look for things like has("gui_running").
Finally when running MacVim gvimrc is sourced vs. vimrc when running vim in the terminal.
Depending on the differences in both of those, mappings and other features will change.
I noticed, that you are talking about Cmd key in the title, but about Ctrl in your description. The Cmd key does not work in terminal vim.
If you are talking about the Cmd key, consult my answer to a similar question.

How do I run a terminal inside of Vim?

I am used to Emacs, but I am trying out Vim to see which one I like better.
One thing that I like about Emacs is the ability to run a terminal inside Emacs. Is this possible inside of Vim? I know that you can execute commands from Vim, but I would like to be able to run a terminal inside of a tab.
Outdated from August 2011
Check out Conque Shell (also on GitHub). Lets you run any interactive program inside vim, not just a shell.
I'm not sure exactly what you're trying to achieve (I've never used Emacs), but you can run commands in Vim by typing:
:! somecommand [ENTER]
And if you want to type in several commands, or play around in a shell for a while, you can always use:
:! bash (or your favourite shell) [ENTER]
Once the command or shell terminates, you'll be given the option to press Enter to return to your editor window
Vim is intentionally lightweight and lacking in the ability to do non-editorish type things, just as running a full-blown shell inside a Vim pane/tab, but as mentioned above there are third-party addons such as vim-shell that allow you to do that sort of thing.
Typically if I want to switch between Vim and my shell (Bash), I just hit CTRL+Z to pause the Vim process, play around in my shell, then type 'fg' when I want to go back to Vim - keeping my editor and my shell nice and separate.
Updated answer (11 years later...):
I would recommend using tmux instead of screen as suggested in the original answer below, if you choose to use that solution.
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.
I would definitely recommend screen for something like this. Vim is a text editor, not a shell.
I would use Ctrl+AS to split the current window horizontally, or in Ubuntu's screen and other patched versions, you can use Ctrl+A|(pipe) to split vertically. Then use Ctrl+ATab (or equivalently on some systems, Ctrl+ACtrl+I which may be easier to type) to switch between the windows. There are other commands to change the size and arrangement of the windows.
Or a less advanced use of screen is just to open multiple full-screen windows and toggle between them. This is what I normally do, I only use the split screen feature occasionally.
The GNU Screen Survival Guide question has a number of good tips if you're unfamiliar with its use.
The way that I get around this is:
pause Vim with Ctrl + Z,
play in the terminal,
then return to exactly where you left with Vim by just typing the command fg.
If enabled in your version of Vim, a terminal can be started with the :term command.
Terminal window support was added to Vim 8. It is an optional feature that can be enabled when compiling Vim with the +terminal feature. If your version of Vim has terminal support, :echo has('terminal') will output "1".
Entering :term will place you in Terminal-Job mode, where you can use the terminal as expected.
Within Terminal-Job mode, pressing Ctrl-W N or Ctrl-\ Ctrl-N switches the mode to Terminal-Normal, which allows the cursor to be moved and commands to be ran similarly to Vim's Normal mode. To switch back to Terminal-Job mode, press i.
Other answers mention similar functionality in Neovim.
:sh then Ctrl+D to get back in (bash)
Update:
You could map Ctrl+D in vim to run :sh, which allows you to toggle between bash and vim quickly.
noremap <C-d> :sh<cr>
The main new feature of Vim 8.1 is support for running a terminal in a Vim window.
:term will open the terminal in another window inside Vim.
:term
Added in Vim 8.1.
Keep in mind that whenever a terminal window is active, most keystrokes will simply be passed to the terminal instead of having their usual functions. Ctrl-W and its subcommands are the main exception. To send a literal ^W input to the terminal, press Ctrl-W .. You can also open the Vim : command line by pressing Ctrl-W :. The other Ctrl-W commands work as normal, so managing windows works the same no matter what type of window is currently selected.
Eventually a native :terminal command was added to vim in 2017.
Here is an excerpt from the :terminal readme:
This feature is for running a terminal emulator in a Vim window. A
job can be started connected to the terminal emulator. For example, to
run a shell:
:term bash
Or to run build command:
:term make myprogram
The job runs asynchronously from Vim, the window will be updated to
show output from the job, also while editing in another window.
This question is rather old, but for those finding it, there's a new possible solution: Neovim contains a full-fledged, first-class terminal emulator, which does exactly what ConqueTerm tried to. Simply run :term <your command here>.
<C-\><C-n> will exit term mode back to normal-mode. If you're like me and prefer that escape still exit term mode, you can add this to your nvimrc:
tnoremap <ESC><ESC> <C-\><C-N>
And then hitting ESC twice will exit terminal mode back to normal-mode, so you can manipulate the buffer that the still-running command is writing to.
Though keep in mind, as nvim is under heavy development at the time I'm posting this answer, another way to exit terminal mode may be added. As Ctrl+\Ctrl+n switches to normal mode from almost any mode, I don't expect that this answer will become wrong, but be aware that if it doesn't work, this answer might be out of date.
https://github.com/neovim/neovim
I know that I'm not directly answering the question, but I think it's a
good approach. Nobody has mentioned tmux (or at least not as a
standalone answer). Tmux is a terminal multiplexor like screen. Most
stuff can be made in both multiplexors, but afaik tmux it's more easily
to configure. Also tmux right now is being more actively developed than
screen and there's quite a big ecosystem around it, like tools that help
the configuration, ecc.
Also for vim, there's another plugin: ViMUX, that helps a lot in
the interaction between both tools. You can call commands with:
:call VimuxRunCommand("ls")
That command creates a small horizontal split below the current pane vim
is in.
It can also let you run from a prompt in case you don't want to run the
whole command:
<Leader>vp :VimuxPromptCommand<CR>
As it weren't enought, there are at least 6 'platform specific plugins':
vim-vroom: runner for rspec, cucumber and test/unit; vimux support via g:vroom_use_vimux
vimux-ruby-test: a set of commands to easily run ruby tests
vimux-cucumber: run Cucumber Features through Vimux
vim-turbux: Turbo Ruby testing with tmux
vimux-pyutils: A set of functions for vimux that allow to run code blocks in ipython
vimux-nose-test: Run nose tests in vimux
Here is a nice "use case": Tests on demand using Vimux and Turbux with Spork and Guard
Someone already suggested https://github.com/Shougo/vimshell.vim, but they didn't mention why. Consequently, when I came away from this question I wasted a lot of other time trying the other (much higher ranked) options.
Shougo/vimshell is the answer. Here's why:
In addition to being a terminal emulator, VimShell allows you to navigate through terminal output in normal and visual mode. Thus, if a command you run results in output that you'd like to copy and paste using the keyboard only...VimShell covers this.
None of the other options mentioned, including the :terminal command in NeoVim do this. Neovim's :terminal comes close, but falls short in at least the following ways as of 2/18/2017:
Moves the cursor to the end of the buffer, instead of at the last keeping it in the same spot like VimShell does. Huge waste of time.
Doesn't support modifiable = 1 see a discussion on this at Github, so helpful plugins like vim-easymotion can't be used.
Doesn't support the display of line numbers like Vimshell does.
Don't waste time on the other options, including Neovim's :terminal. Go with VimShell.
It's possible to open a new tab with a terminal in vim since 2017 as #fjardon said:
Just type: :terminal. It will open a tab by default above your current tab.
If you want it to open in another place you can try the following options:
:below terminal : open the terminal below current tab.
:below vertical terminal : open the terminal always vertically to the right.
You can play with these until you find what you like. After this you can set a map in your .vimrc configuration file, for me, I use:
nmap <leader>tt :below vertical terminal<CR>
This way I can type <space>tt (space my leader key) to open it quickly.
As a side note:
You can switch between your tabs (terminal and other buffers) with Ctrl+W Ctrl+W.
You can enter an editable mode in your terminal if you want to copy your commands with Ctrl+W N and go to normal terminal mode with i or a.
Cheers!
You might want to take a look at the :sh command (see :help sh in Vim).
Various commands
No, you cannot:
http://vimdoc.sourceforge.net/htmldoc/tips.html#shell-window
By far, I have tried a lot of solutions mentioned here, what I really wanted is to keep the terminal open while coding a similar experience in VsCode. Then I came across this solution which is working perfectly for me.
Before Installing:
I am using Nvim 0.5 but I think it can work for any version and checked also on vim
I am using macOS Catalina Version 10.15.7
Setup your integrated terminal
Step -1-
Create a script with the name myQuickTerminal.vim or whatever name you want.
Put the following script
"==============================================================================
"
" ▒█▀▀█ █░░█ ░▀░ █▀▀ █░█   ▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ░▀░ █▀▀▄ █▀▀█ █░░
" ▒█░▒█ █░░█ ▀█▀ █░░ █▀▄   ░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▀█▀ █░░█ █▄▄█ █░░
" ░▀▀█▄ ░▀▀▀ ▀▀▀ ▀▀▀ ▀░▀   ░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▀▀▀ ▀░░▀ ▀░░▀ ▀▀▀
"
"==============================================================================
" " This is a script that will trigger a terminal quickly than the FloatTerminal
" open new split panes to right and below
"link: https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
"==============================================================================
set splitright
set splitbelow
" turn terminal to normal mode with escape
tnoremap <Esc> <C-\><C-n>
" start terminal in insert mode
au BufEnter * if &buftype == 'terminal' | :startinsert | endif
" open terminal on ctrl+n
function! OpenTerminal()
split term://zsh
resize 10
endfunction
nnoremap <leader> n :call OpenTerminal()<CR>
NOTE: if you want to run bash instead of zsh for a particular reason then replace zsh with bash.
Step -2-
Lets source it, put this in init.vim for neovim or `.vimrc' for vim
source $HOME/.config/nvim/modules/mySpecialScripts/myQuickTerminal.vim
This will be preloaded ahead as you save and resource it, you can use source $MYVIMRC for quick reloading the init.vim file.
Step -3-
I mapped as you can see in the script n to open a terminal in a new pane, my is the (Space bar) and once I click (space + n) a terminal will be triggered and I will enjoy writing my code while the terminal is opened.
To quit insert mode in the terminal, press Esc.
Now, to switch to the code editor pane, use CTRL+w w. This shortcut can get annoying once you have more than two panels open, so I added the following shortcuts too.
I mapped these too for quick jumping among opened panes, use these
" Better window navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
Optional
If you want your terminal to exit the current buffer with key. you can use
tnoremap <Esc> <C-\><C-n>:q!<CR>
But since I want to switch in between several buffers I use without close the terminal I use instead:
tnoremap <Leader><Esc> <C-\><C-n>:q!<CR>
Final results
Reference:
https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
Only way I know of is by using vim-shell, a third-party patch.
I use this now, you may can try. VimShell
Split the screen and run command term ++curwin to run the terminal inside the Vim buffer. Following command does both and worked for me:
:bo 10sp | term ++curwin
If you are interested in quick answer, here is it: :vert term. It will split your screen vertically and open up terminal.
Try vterm, which is a pretty much full feature shell inside vim. It is slightly buggy with its history and clear functions, and still in development, but it still is pretty good
Assuming your version of vim supports +term command first, set shell for vim to use in one command (e.g. set=/usr/bin/zsh), and then run the command +term (i.e. bo 15vs +term). you may have to do some additional maneuvering of your windows (i.e. deleting one and rotating), but you'll have your terminal.
With vim 8.1.3741, just type :terminal to start a terminal inside of vim.
Try map :nnoremap ]t :terminal<CR> to do that quicker!
I acknowledge that I am not strictly answering your question, but what has worked better for me when using Vim and Terminals in the same window is Tmux (which is kind of a "run in the background software" like, similar to screen, although this one works better with splits and tabs).
This post will help you to understand how they work together: 'Tmux and Vim — even better together'.
This way we can convert Vim into a powerful IDE

Resources