running tmux automatically when xterm is launched: what is the most elegant/correct solution? - bash

I am trying to setup my workstation in such a way that tmux is run for each terminal (xterm, gnome-terminal, ...) that is launched. I was thinking to add tmux to the .bashrc; problem is that if I launch bash twice for whatever reason, it start a second tmux inside the current tmux.
So:
is there a way to detect, maybe from the .bashrc, that the current bash is the 'first' one and not a second one launched in the same terminal?
any other good ideas / best practices / bash design patterns?

You can add the following to your .bash_profile:
SHELL=tmux
This is the first place xterm checks for the command to run if none is given on the command line.

Combine the two previous answers:
alias xterm='SHELL=tmux xterm'
You get the desired behavior when starting just xterm, but you can still use xterm for other operations, such as xterm top.

What about aliasing xterm to xterm tmux?
Just add the following line to your .bashrc:
alias xterm='xterm tmux'

Related

run a looping command in 10 terminals

an easy question from some of you.
I need to run a command which take into account 10 different files. For some reasons I need to run this command for each file in parallel possibly in 10 different terminals.
Is there a way to do this in a shell script? Ideally, I would open terminals in a loop i=10 then loopin into the files folder, however I do not know hot to open different terminals in a loop and assign a command to each one.
I hope it is clear, if not let me know
There are better ways to solve your problem, but if for some reason just want to open 10 terminals and in "parallel" run a command within them, you could give a try to tmux with the option synchronize-panes on, you could use this script for example:
#!/bin/sh
for i in {1..10}
do
tmux split-window
tmux select-layout tiled
done
clear
tmux select-layout tiled
tmux setw synchronize-panes on
After having tmux running just run it, it should look than something like the attached picture:

Open a small window while in Vim for executing commands

I need to write lot of code and compile very often. I hate switching back and forth various windows just to compile the code. Is it possible to open a small window at bottom and run invoke shell and close that window when needed?
With GVim or MacVim, you can run external commands in the command-line: Gvim/MacVim comes with a (very) limited shell that will happily show you whatever the compiler outputs. The general usage pattern is:
:!command
:!command %
With CLI Vim, the same method will pause Vim and return to the shell to execute your command.
In both cases, you'll get a message asking you to press ENTER to come back to your normal editing.
Using :make | cw would be a slightly more sophisticated alternative, with the added bonus of showing the errors in the quickfix window.
An even more sophisticated approach would be to use Tim Pope's Dispatch plugin in combination with tmux or screen.
Sounds like a problem for Screen
http://www.gnu.org/software/screen/
Quick reference of commands
http://aperiodic.net/screen/quick_reference
I use tmux to achieve something like that. I have the following in my ~/.tmux.conf file:
bind s splitw -v -p 25 -c '#{pane_current_path}' '/bin/bash'
bind q kill-pane
On pressing Ctrl-b + s (prefix + s), a new pane containing a bash shell opens up at the bottom. I can run shell commands from there: find, grep, make, etc. When I'm done, I press Ctrl-b + q to close the shell.
To enable tmux on every bash session, add the following to your ~/.bashrc:
[[ -z "$TMUX" ]] && exec tmux
Maybe map a key to shell out to the compiler and run the program if compilation is successful:
:map F8 :!cc % && ./a.out
Or maybe just
:sh
make run
Ctrl-D
Another option is to suspend vi, using Ctrl-Z and do your stuff in the shell, then type fg to bring vim back to the foreground. Note that this is actually a feature of your shell, rather than vim but it produces the effect you seek.
Note this idea originates from the book "Efficient Linux at the Command Line" by Daniel Barrett. I forget the page number.

Can I make emacsclient start alternate-editor in the background?

I have installed Emacs 24 on my Mac using homebrew, and similar to another question (listed below), I would like to use the same command when first starting Emacs as I do when opening additional files from the command line. To this end, I have set up an alias (modified a bit for readability):
EAPP=/usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS
alias emacs='emacsclient -a $EAPP/Emacs -n'
Here's the twist: I put the -n (same as --no-wait) at the end because I like not having to hit C-x # to finish buffers. A side effect that I like is that it returns immediately to the command line.
Could there be a way to alter my alias to make the first-time run go in the background as well? Or should I just teach my fingers to put a & after every invocation of my emacs alias?
Question about making emacs and emacsclient seem the same:
Emacs - connect to daemon (if it exists) without using emacsclient

Any execution of command in vim causes it gets suspended

This occurs when I set the vim's shell to be interactive:
set shellcmdflag=-ic
or
set shell=/bin/bash\ -i
I like these because they give syntax highlighting to the output (eg.: !ls)
But the cost is that I have to type fg # every time.
Is this a default behavior?
How can I get interactive shell in vim without having to make it run foreground?
You can't. This behavior is perfectly normal and expected and in line with Vim's author's philosophy. It's very unlikely to change in the future.
If you want a shell inside Vim, you'll have to install a plugin like Conque or Vimshell.
I usually just use tmux instead. You can split the terminal and have a normal interactive shell and an instance of vim running side by side - very handy.

Bash / open a terminal with a command to run passed as an argument

I created a script that starts all the apps I need for my day and assign them to the workspaces I want.
I'd like to know is there was a way to pass an argument to the terminal I open. The argument would be an alias that runs a massive source update of all the projects I'm working on.
I'd like to do something like this:
gnome-terminal start_rolls
Unfortunately, this only opens the terminal and the command is simply not run.
Any ideas ?
Thanks in advance !
Rolf
P.S.: the -e option does not work with aliases, it seems...
gnome-terminal --help-terminal-options says:
-e, --command Execute the argument to this option inside the terminal
some combination of "-e", "bash -c", "bash -i" and your alias might help
-e is going to be deprecated in future releases you should use something like this:
gnome-terminal -- bash -c "vim Main.java"
you can replace "vim Main.java" for whatever you want

Resources