3

Is there a way to have a text file open in Vim, and have the shell open next to it in the same window?

I find myself opening a script for editing, making changes, closing Vim, running the script to test it, then opening Vim again, over and over. I suppose I could have two separate windows with a shell in one and Vim in the other but that somehow feels clunky (I guess I'm too used to IDEs). The real problem, however, is that I am using SSH to connect to a remote machine, and having two separate windows with two separate SSH sessions seems not just clunky but actually Wrong. Is there a better way or am I stuck with two windows/SSH sessions?

dkv
  • 6,602
  • 10
  • 34
  • 54
  • 3
    You can use `tmux` (or the older `screen`) to split the screen in half or quickly switch between two sessions – that other guy May 09 '17 at 00:03
  • 1
    As an addendum to the previous comment, see https://stackoverflow.com/questions/1236563/how-do-i-run-a-terminal-inside-of-vim – thiagowfx May 09 '17 at 00:04
  • You could also run `:shell`, however it won't work with split windows natively inside of vim. – thiagowfx May 09 '17 at 00:06
  • tmux is a great answer, as already suggested, but to answer your point about multiple ssh sessions, there is nothing wrong with that. I've been a sysadmin and developer since the late 90s, and I do exactly that pretty much every day to many hosts. – Dan Lowe May 12 '17 at 03:26

2 Answers2

4

I'd say the tool you're looking for is tmux. tmux will allow you to have 2 (or many more) sessions running at once, either in splits on your screen or in sessions that you can easily switch between. So yes, you can have vim on the left and the shell on the right and easily switch between them.

This is all extremely handy, but the best feature when working remotely over ssh is that the sessions continue if you lose your connection - you just reconnect, then tmux a to reopen your existing session, containing vim, long running install or build scripts, whatever.

A quick and easy guide to using tmux

nickspoon
  • 1,347
  • 12
  • 18
  • The instructions on the website say to compile it from the tarball -- is there a (perhaps "unofficial") easy way to install it on a remote machine e.g. with `yum`? I'm working with an AWS HPC cluster that so I'm effectively starting with a brand new machine every day and would like to avoid `scp`ing and compiling every day. (Unless I'm misunderstanding its intended usage and it should actually be installed on my local machine?) – dkv May 09 '17 at 16:22
  • Have you tried `yum`? The website is pretty awful. I have always just installed it using `apt-get install tmux` on my Debian based distros. – nickspoon May 09 '17 at 20:56
2

The quickest way is to call shell commands from inside vim (without leaving it) like this:

:! ls -l

The command will execute, then you will get the message:

Press ENTER or type command to continue
AhmadWabbi
  • 2,253
  • 1
  • 20
  • 35