Automatically closing processes when quitting tmux - vim

I'm using tmux with tmuxinator to configure it. I always have different panes open, with two or more editor sessions open and other stuff. Every time I have to close the whole tmux session, because I'm turning off the computer, it takes time, because there are processes active in various tmux panes. So I have to manually quit every Vim instance, because if I just kill the pane containing it, then I will end up having vim swap files.
So, is there a way to quit the whole tmux session (not just detaching it) and automatically closing all the processes running in it?

Executing tmux kill-server kills all the sessions, windows, panes and also all processes running in them. This is the way to completely quit everything related to currently running tmux.
The above will unfortunately not solve the issue with vim swap files - you will still get them.
I solve the swap-file issue by having the following line in .vimrc:
set noswapfile
As you might guess, it disables swap files in vim. If you save files in vim regularly and you have a reliable computer, setting the above option might be perfectly acceptable.

Tmux Resurrect uses obsession.vim and it gives you the ability to save & restore Vim sessions on system reboot.

Related

tmux + vim, clipboard failure after re-attach when ssh from Windows to Linux machine

Have been enjoying tmux + vim these days, except one problem I cannot resolve every time.
It's a clipboard failure easy to reproduce. Vim's "+y "+p, copy to or paste from clipboard, work well if I create a new tmux session and keep using it either on Linux machine or via ssh (I use MobaXterm which supports X11 forwarding) from Windows. But copy and paste will surely stop working after I switch from one side to the other.
I tried to search but cannot find an answer but maybe I used wrong keywords. Any tip would be appreciated.
This is most likely related to your DISPLAY environment variable.
When you run tmux locally on your Linux machine, it's going to set DISPLAY to the default, most likely :0. This tells X11 programs to use your local X server. The Vim clipboard integration uses that X server for clipboard storage with the + register (in your config, based on your question; it could also use the * register, based on what Vim's clipboard option is set to).
When you SSH in with X11 forwarding, DISPLAY will be set to a virtual server, representing the forwarded connection--most likely :10.0 or localhost:10.0.
When you start tmux, it will use whatever DISPLAY is set to when it's initially started. If you detach and later reattach from the other machine, tmux won't automatically change DISPLAY. So if you initially start your tmux session from Linux, and then later SSH in from Windows and reattach the session, tmux (and the Vim running inside it) will still be using the Linux display.
One option is to quit Vim, do export DISPLAY=:0 (or DISPLAY=:10.0 as appropriate), and restart Vim. Note that you'll have to do this in each shell you have open, and if you open a new window/pane, it will still inherit the parent tmux session's DISPLAY setting.
Another option is to change the environment variable inside Vim, so that you don't have to restart Vim. You can do this via :let $DISPLAY="whatever".
There may be a way to change the tmux session's DISPLAY value at runtime, but I don't know how to do that. If you could figure a way out, you could probably automate it via a wrapper around tmux attach. This answer to a tmux question might help, but I just do one of the above.
I'm still looking for a better solution to this as well, but I found a bit of a workaround.
Save your vim session with :mksession
Exit with :qall
Reload the vim session with vim -S
When vim starts back up the clipboard integration works again.
Just like bydsky says. After your reattach Tmux session with an old running vim. Just run :xrestore Ex command in vim to restore X11 connection.

Tmux vs. iTerm2 split panes

Why should I use tmux when iterm2 has split panes?
I have never used tmux, and want to know if there are advantages to using that in my workflow instead of the split pane features iterm2 has.
I really like the dimming of inactive windows that iTerm2 split panes offers. Does tmux do something similar?
What are the advantages/disadvantages of each?
There is another advantage of tmux: what happens if you accidentally close iterm2? If you do it really by accident, you want to reopen everything again. With tmux it is normally as simple as reattaching session without losing anything. Most terminal emulators send SIGHUP to all children which terminates them by default and thus you lose unsaved data (at least, shell and vim command history and other data stored in viminfo) and running processes and thus reopening means rerunning everything.
iTerm2 can use tmux for it's split panes. Personally, I'm used to tmux by itself at this point, so I've not leveraged this ability extensively - but if you are used to iTerm2 split panes, you can get the benefits of tmux (mostly screen-like session saving) with the iTerm aesthetics.
https://gitlab.com/gnachman/iterm2/wikis/TmuxIntegration
My approach (not based on any particular insight) is to use iTerm tabs and panes to separate servers, and screen / tmux on the server to persist sessions.
I don't often have anything of importance running locally, but often do remotely.
Never used iterm2, however I have played with tmux a little and there are several articles about using tmux and vim together. These articles show how you can control a tmux session via the tslime plugin, and others, from Vim. What's nice about it is that you can run a command in Vim to compile your files, run unit tests, etc. without every leaving Vim, but you see the command run in the other tmux pane.
Apologies for not being a complete answer, but hopefully it helps point you in the right direction.
LINK: https://joshuadavey.com/2012/01/10/faster-tdd-feedback-with-tmux-tslime-vim-and-turbux/

Open vim tab in new (GUI) window?

I'd like to move the current tab into a new (visual/real) window in MacVim.
It's probably difficult, as:
there is nothing in the vim help and only very few - not helpful - hits on google
MacVim does not support it (link, 2009)
So I am wondering if someone has found a way to achieve this?
The closest I think you can come is using mksession
This will have several drawbacks (like, initially the secondary session will open a few more buffers than ultimately desired).
However, it will preserve your mappings, settings, history, window layout (if you had multiple windows inside the current tab, they will all get cloned).
If this idea tickles your fancy, you could look at creating a script that will filter parts out of the session file (which is Yust Another Vim Text Script)
:mksession! $HOME/detach.vim
:tabclose
:silent! !gvim remote --servername Detach -nR +'silent! source H:\detach.vim' +tabonly
save all current windows, mappings, settings (:he mksession)
close the tab we are about to detach
clone the session (detach.vim) into a remote vim
:silent! (try not to talk too much)
!gvim remote --servername Detach; launch a new remote Vim server if it doesn't yet exist, or talk to the existing server named Detach
-nR TODO Fix This is here to avoid the use of swapfiles (because I found no way to suppress the dreaded ATTENTION messages[1]). However, this may be unsafe advice depending on your situation, which is why I also include -R for read-only mode
+'silent! source H:\detach.vim' +tabonly -- In the remote vim, source the session to clone, and keep only the active tab (that was already closed in step 1.)
A little rough around the edges, for sure, but quite close to what you intended, I feel.
If you are comfortable, you can drop the -nR flags and just click through a few annoying swapfile attention messages (keyboard: Q).
[1] :he ATTENTION
Post-scripts:
on windows you might want to use :silent! !start /b gvim .... in case you have terminal windows sticking around
also on windows, you might get annoying behaviour if the resulting gvim window is too small. The :simalt ~x sequence is one hacky way to maximize the window (assuming English UI, because x is the accelerator for Maximize)
as icing on the cake, vim 7.3 has 'persistent undo'. See e.g. :se undofile
I don't think this is possible because, when you open a new instance of (g)vim you don't have access to the undo-history of the previous vim instance. All you can do is (in command mode):
:!gvim %
It will open the current file in a new instance of gvim. At least this is all I could think of. I may be wrong

What's the best (Linux-compatible) buffer/session manager for Vim?

Is there a good project / session manager for vim? A session (or project) is a named lists of files, e.g. "bitonic_sort" could identify files "~/A/bitonic_sort.sk", "~/B/bitonic_sort.smt2", etc.
(rationale) I have a project where I need to edit files from many different locations, and it is too cumbersome to open them manually each time I resume work. (so, it looks like things like nerdtree brought up at this sister question, Favorite (G)Vim plugins/scripts?, won't work). I also need separate sessions (i.e. lists of files) for different projects, not just a recent document list.
After all documents have been loaded as buffers, any enhancements to switching between them is a plus (e.g. start typing a name, and matching documents are displayed). Thanks in advance.
Vim has built-in session manager. To save your current session use:
:mks session1.vim
This basically create a Vim script named session1.vim, which will restore your opened file if you source it or start Vim like this:
vim -S session1.vim
To overwrite your saved sessions, use :mks! your_saved_session.vim. Combine with a custom key map and this will be the solution. For more about Vim session read :help :mks. Vim also has views manager which is quite similar. Read more from: :help :mkview
For switching between buffers, you can use FuzzyFinder; but I prefer this key map:
nmap <C-tab> :bn<CR>
imap <C-tab> <ESC>:bn<CR>i
Add it to .vimrc and I can use Ctrl + Tab to switch between buffers just like Firefox tabs. Hope this help.
I'm not sure exactly what you're asking for... but if you want to turn on tab-completion when opening files in vim, add this to your ~/.vimrc:
" Auto-complete file names after <TAB> like bash does.
set wildmode=longest,list
set wildignore=.svn,CVS,*.swp
Also, take a look at screen. From the man page:
When screen is called, it creates a single window with a shell in it (or the specified command) and
then gets out of your way so that you can use the program as you normally would. Then, at any time,
you can create new (full-screen) windows with other programs in them (including more shells), kill
existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between
windows, view the scrollback history, switch between windows in whatever manner you wish, etc. All
windows run their programs completely independent of each other. Programs continue to run when their
window is currently not visible and even when the whole screen session is detached from the user's
terminal. When a program terminates, screen (per default) kills the window that contained it. If
this window was in the foreground, the display switches to the previous window; if none are left,
screen exits.
It's pretty much like having several xterms open, except unlike graphical xterms you can access your screen session if you access your machine remotely (e.g. by sshing to it). You could leave up several different instances of vim in separate screens with all the files you want open, and just never exit them.
The very basic setup I use is one vim window, one compile window, and one testing/debugging window.
And since we're talking about vim, check out this post: Post your Vim config. Lots of cool tweaks and spiffy stuff in there.
You can try the vim-workspace plugin, its session management features are automated and relatively simple (compared to vim-session): https://github.com/thaerkh/vim-workspace

How to temporarily exit Vim and go back

How could I exit Vim, not :q, and then go back to continue editing?
Assuming terminal Vim on a flavor of *nix:
To suspend your running Vim
Ctrl + Z
will suspend the process and get back to your shell
fg
will resume (bring to foreground) your suspended Vim.
To start a new shell
Start a subshell using:
:sh
(as configured by)
:set shell?
or
:!bash
followed by:
Ctrl+D (or exit, but why type so much?)
to kill the shell and return to Vim.
You can use :sh to exit to your default shell then typing $ exit at the shell prompt will return you to Vim.
You can switch to shell mode temporarily by:
:! <command>
such as
:! ls
You can also do that by :sus to fall into shell and back by fg.
If you frequently need to go back and forth between shell and vim, probably what you really want is have only one vim instance in the shell, and use it to open any file in the workspace. If so, check this question. Once you set it up correctly, you can :sus or C-z to return to the shell, then just v or v <newfile> to get back to vim.
And my answer is almost my daily routine.
If you are on a Unix system, Ctrl + Z will suspend Vim and give you a shell.
Type fg to go back. Note that Vim creates a swap file while editing, and suspending Vim wouldn't delete that file (you aren't exiting Vim after all). On dumb terminals, this method was pretty standard for edit-compile-edit cycles using vi. I just found out that for me, gVim minimizes on typing Z.
If you're using Neovim, you can do the following:
:terminal command to bring up a terminal window.
Do your terminal stuff
Type exit to kill the terminal process
Press any key to return to Neovim
Just put in fg and go back to your most recently suspended program.
There are several ways to exit Vim and have everything the same when you return. There is very good documentation within Vim itself explaining the various ways this can be done. You can use the following command within vim to access the relevant help page: :help usr_21
To give you a brief summary, here are the different methods of quitting and returning with your session intact:
Suspend and resume - You don't actually quit Vim with this; you simply hide your session in the background until you need it. If you reset your computer or issue a kill command to Vim, you will lose your session. This is good for when you want to switch to another task temporarily, but if this is the case, then you might want to look into using the GNU Screen utility instead.
Sessions - This is the true way of saving your session between instances of Vim. Even if you truly quit Vim, your session will be there for you when you return. This is probably what you are looking for.
To extend user Zen's answer, you could add the following line in your ~/.vimrc file to allow quick toggling between Bash and Vim:
noremap <C-d> :sh<cr>
If you don't mind using your mouse a little bit:
Start your terminal,
select a file,
select Open Tab.
This creates a new tab on the terminal which you can run Vim on. Now use your mouse to shift to/from the terminal. I prefer this instead of always having to type (:shell and exit).

Resources