Change Terminator profile from command line - bash

I have a Terminator profile (i.e. different bkgd colors, etc) that I use for production related scripts/consoles, but have to change the profile manually. Is there a way to change the profile at a bash level from within an open terminal window when launching production related scripts?
e.g
set_production_profile
heroku run rails console -a myprodapp
set_default_profile

Related

Add Auto-Run Scripts to Developer Command Prompt

Is there any way to add paths or run a script whenever the Developer Command Prompt launches? That is the only source of admin that my company gives me, and I'd like to run cmder instead, but cmder isn't in the paths when the DCP loads. It's a nuisance to constantly have to fully qualify it in the one directory I'm allowed to change.

Terminal window with running command

Is it possible to start a new window of terminal as in Windows: start example.bat? It opens a new window and runs an command.
You can do this using the open command, or ossascript. Here are some related questions using those commands in their answers:
Programmatically launch Terminal.app with a specified command (and custom colors)
Running a command in a new Mac OS X Terminal window
What arguments does the Mac's Terminal.app accept?
The open command is more straightforward, allowing you to specify the name of a script or command to be run, as noted in one answer. That describes this case (referring to open(1)):
open -a Terminal.app file
where file is the application to be opened. You may have to give its full pathname. The manual describes also a --args option which it states can be used to pass parameters to the application.

iTerm 2 profiles

I have recently switched over to iTerm2 and love it. I am wondering though if there is a way to use profiles to correspond to what environment/specific machine you are on.
Say if I am doing tasks in one window on my mac the profile is displayed as default, but if I ssh into a machine (lets say dev0), the profile on iTerm will update to profile dev0. Once I've finished with dev0 and call exit, the profile will switch to default again.
I realize one work around is to open up a specific profile whenever I want to ssh into another machine and have a way to distinguish, but if the connection is closed it requires you to notice based off text rather than say the background of the window.
Is this possible? If not how can this feature be added, and is there a way I can contribute?
iTerm2 supports a custom escape code that changes the profile on the fly. Put it in your .bashrc or .bash_profile.
<esc>]50;SetProfile=X^G
where X is the profile. For instance, to change the profile to one called "Foo", us this shell script:
#!/bin/bash
echo -e "\033]50;SetProfile=Foo\a"
To change it back when you log out, put code to change the profile back to default in ~/.bash_logout.
for zsh users
lets say you have 2 profiles, one named mac (for your primary machine) and one for linux (your remote machine)
when entering the session, we need to tell zsh to load our profile
connect to remote linux
in ~/.zshrc add echo -e "\033]50;SetProfile=linux\a"
source your files for immediate effect: source ~/.zshrc
your new theme should be visible within the iterm session.
when exiting the session, we need to tell zsh to switch back to our original profile
connect to remote linux
in linux ~/.zlogout add the following
if [ "$SHLVL" = 1 ]; then
echo -e "\033]50;SetProfile=mac\a"
clear
fi
now you can swap profiles with ease <3.
if you are using bash, i believe the steps are similar but you would instead modify ~/.bashrc and ~/.bash_logout
demo
The latest iTerm2 nightly (Build 2.9.20150329-nightly at the time of writing) allows you to do that easily. You can download it here.
Once you've installed and opened it:
Log in to your remote machine via ssh and click iTerm2 (the app menu) > Install Shell Integration. It will download a script with curl and install it. Do the same on your local machine.
Go to Preferences > Profiles.
Create a new profile for your local machine. Customize it to fit your needs (change background color, name, etc)
Go to the Advanced tab and scroll to the bottom.
In Automatic Profile Switching, click '+' and add the hostname of your local machine. The hostname is the one you get when running echo $HOST on the target machine. It is not always the one you see in your prompt.
Create another profile, this time for your remote machine, and customize it.
Add the hostname of the remote machine in Automatic Profile Switching.
Now, if you ssh into the remote machine, your profile will change, and if you exit out of the ssh session, you will be back to your local profile.
You can combine this solution with #esod's answer seamlessly.
Note: it didn't work for me until I created a profile specifically for the desktop instead of using the default profile.
See the documentation for more info.
step 1:
custom your iterm profile, e.g. dark, light
step 2:
add code before to your shell profile, e.g .bashrc or .zshrc
# Change iterm2 profile. Usage it2prof ProfileName (case sensitive)
it2prof() { echo -e "\033]50;SetProfile=$1\a" }
step 3:
make sense your profile
exec $SHELL -l
step 4:
toggle your iterm theme profile
it2prof dark
it2prof light
I had this same wish and found this can be accomplished in iTerm 2 (Build 1.0.0.20130319) in the application's preferences.
You can assign a profile (say a remote profile) with a different preset than your default preset by going to:
Profiles -> Open Profiles
select the profile and click Edit Profiles...
Go to the Colors Tab and choose a preset for this profile from the list in Load Presets...
Further, I've set up Keys shortcut for different profiles so I can have one iTerm window look different than another window. I did this by:
Creating a new a Profile in Preferences
Creating a new Profile Shortcut Key in Preferences-> Keys whose action is New Window with Profile
My Default profile has a black background but sometimes it helps me to have a white background. I duplicate my Default profile and name the new profile DefaultLight. On my Default profile I go to the Keys tab where I create a new Profile Shortcut Key whose Keyboard Shortcut is ^+cmd+n, whose action is New Window with Profile, and whose Profile is DefaultLight.
After saving the prefrences, cmd+n opens a new window with a black background and ^+cmd+n opens a new window with a white background.
There's also a New Tab with Profile action in the Keyboard Shortcut Keys Preference if you're interested in taking this even further.
In my case, I need to combine Yohaï Berreby's answer with my hosts' setting to implement this feature.
In Automatic Profile Switching, click '+' and add the hostname of your local machine. The hostname is the one you get when running echo $HOST on the target machine. It is not always the one you see in your prompt.
My staging server doesn't set $HOST and host name is :
[devel#alveo-staging ~]$ hostname
alveo-staging
But I can't set the rule with hostname as alveo-staging. Because in fact alveo-staging is just an alias of the real hostname (alveo-staging.xxx), which can be set in /etc/hosts.
So the quick solution to this is to use * wildcards, to set the rule as *staging*.
Then it works.
For Fishell user:
1. Create a fish function:
functions it2prof > ~/.config/fish/functions/it2prof.fish
Add this content to ~/.config/fish/functions/it2prof.fish file and save:
function it2prof
echo -e "\033]50;SetProfile=$argv\a"
end
enjoy your command:
it2prof whatever_profile_you_define

Delete gnome terminal configuration

I made a mistake in my gnome terminal configuration. I entered a command to start with in the preferences, but that command fails, and now all I get is a window that opens and closes right away, and I basically can't use gnome terminal anymore :-( Is there any way I can remove the configuration file and restart fresh??
Thanks!
Open the XTerm (Standard terminal for linux) and enter this command
gnome-terminal -e bash
It opens the gnome-terminal. Open profile preferences and configure your terminal to "Hold the terminal open".
Editing preferences
$HOME/.gconfd/saved_state
the above file might be of interest depending on exactly what configuration you changed. Of course, it holds configuration from other programs as well.
If you are on the newer gnome terminal that uses dconf, it's a little trickier, but still doable:
Profiles are stored with a UUID, you need to find the UUID of the profile to remove:
dconf dump /org/gnome/terminal/ | less
Search for a visible-name='...' entry matching the profile you want to remove. Look above that for the section header like [legacy/profiles:/:...]. The full name of the item you want to delete is thus /org/gnome/terminal/legacy/profiles:/:.... Delete it thus:
dconf reset -f /org/gnome/terminal/legacy/profiles:/:...
Side note: This Q&A probably should be moved to unix.stackexchange.com.

Mac OS X / Open terminal with specified windows

Is it possible to open a terminal window with 3 tabs. Each tab should have different path.
Example:
Tab1: /etc
Tab2: /bin
Tab3: /www/ tail -f file.txt
This is absolutely possible, but it will take some work on your part. The first thing you need is to set up each window/tab you want in your Settings:
I have 4 tabs that I open automagically every time I open Terminal. DB Shell, Editor, Server, and Shell. These are all within the Sasquatch (don't ask) project, thus the naming. Each of these should then have a unique command associated with them:
In this case, I'm executing vim. If you happen to have a specific directory you'd like to start off in, you can use something like vim ~/projects/main/. Really whatever you want to go in there is the command the shell will execute when it opens. Now you need to open all your windows/tabs:
Close everything.
Open a new window for each of your profiles.
Go to the Shell menu => New Tab/New Window => Select the profile you created above.
Repeat for each window or tab you want.
Once you have all of your windows and/or tabs open, save them as a Window Group.
Go to the Window menu => Save Window As Group....
Give your Window Group a name (this is helpful later).
If you want this group to open every time you open Terminal, check the box at the bottom of this screen, and then hit save.
Close out of all of your Windows/Tabs (time to test it!).
Open your new Window Group.
Go to the Window menu => Open Window Group => Select the group you just made.
This should pop up all the windows you just had, in the same position. Each of the commands you set up in Settings should be launched in their respective tabs.
As of Mac OS X Lion 10.7, Terminal makes this much easier to do, without creating new profiles for each command.
By default, Terminal will remember and restore the current working directory for each terminal in a Window Group. (If the working directory has been communicated to Terminal using an escape sequence. The default shell, bash, will do this at every command prompt. For other shells, you'll need to adapt the code in /etc/bashrc.)
If you create a terminal with Shell > New Command, Terminal will automatically run that command when a Window Group is opened. Terminal will automatically run a limited set of "safe" commands†, and when saving a Window Group there's an option to run all commands in the group.
Terminal also automatically does these for all windows when restarting Terminal with Resume enabled. So, you may not even have to create a Window Group, depending on your circumstances.
For your example case:
Use Shell > New Command to run "tail -f /www/file.txt".
Create a new tab and "cd /etc".
Create a new tab and "cd /bin".
Save them with Window > Save Windows as Group. Be sure to check the "Restore all commands" checkbox.
Each time you open that Window Group, it will recreate those windows and run the commands. If you need to run a command and specify the starting directory, in the New Command dialog check the "Run command inside a shell" checkbox and make the command "cd ; ".
Also note that you can tell Terminal to open your Window Group at startup with Terminal > Preferences > Startup > On startup, open > Window group. There's even a checkbox to set this when saving a new Window Group.
† The "safe" commands include anything listed in /etc/shells, plus: screen, tmux, emacs, vi/vim, nano, pico, and top. You can customize the list with "defaults write com.apple.Terminal RestorableCommands". Set it to an array of strings containing command names or full paths. Some commands have parameters that are "unsafe" to run automatically without user intervention, so by default these commands are only considered "safe" if they do not have any arguments. To make a command safe to run with arguments, add an asterisk, e.g., "top *" is in the default value for this preference.
You can do what you wish from within Terminal.
If in Terminal preferences (Settings tab) you create a new profile (or copy one with Duplicate Settings), you can then set each profile to run a command on startup (the "Shell" subgroup within the profile).
Then setup your tabs by using the Shell > New Tab menu to create the new tabs from each of the specific profiles that you created for the three different executables.
Then do the Save Window Group to save the group of tabs (and it will save their profiles as well).
I suggest the use if iTerm instead of Terminal. If only because it is more configurable. You can script it, but more important to you is that you can create a bookmark folder (one for each tab) and then "open in tabs" which will give you the behavior you seek.
I suspect you can control commands to be executed too. One of the programs I use creates a single-tab terminal window and arranges to execute a profile-setting script before continuing to the command prompt - the same should be feasible for a multi-tab terminal. The file is a MacOS X properties XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CommandString</key>
<string>. /Applications/IBM/informix/demo/server/profile_settings</string>
<key>FontAntialias</key>
<false/>
<key>RunCommandAsShell</key>
<false/>
<key>ShowShellCommandInTitle</key>
<true/>
<key>TerminalType</key>
<string>xterm</string>
<key>WindowTitle</key>
<string>IDS Command Window</string>
<key>name</key>
<string>IDS Command Window</string>
<key>type</key>
<string>Window Settings</string>
</dict>
</plist>
You can click on it and the terminal window is launched, the profile settings are set, and then you have a command prompt to type at. Presumably, changing the 'dot' command into the 'tail' command of the question would work; it might be that the 'RunCommandAsShell' key set to '<true\>' would replace the normal shell with the command - which is perhaps more appropriate for the question.
Another way of doing this is by using the Elscripto ruby gem: https://github.com/Achillefs/elscripto. It allows yuo to easily specify terminal tabs using a YAML file
Open the tabs you want and set each one up as you wish, i.e. in tab 1, cd /etc, tab 2 cd /bin and so on. Now go to Window > Save Windows as Group. Click the checkbox 'Use window group when Terminal starts' and hey presto!
http://www.iterm2.com/#/section/features/split_panes
"Divide a tab up into multiple panes, each one of which shows a different session. You can slice vertically and horizontally and create any number of panes in any imaginable arrangement."
How to Create Custom iTerm2 Window Arrangements
Create a custom keyboard shortcut to automatically spawn a set of windows and splits with processes running.
With a shortcut cmd+shift+w transform split your window arrangement into 3 panels
https://www.youtube.com/watch?v=Rg8AT-nds1Q

Resources