Changing the location where tmux saves sessions? - terminal

I often connect to a terminal on my school's network. Anything higher than my home directory I cannot change. Is there a command for the .tmux.conf that would allow me to save my sessions in directory inside of my home directory.

By default tmux does not save its session. You need a plugin like tmuxinator or tmux-resurrect.
If you use tmux-resurrect plugin to save your session. It saves by default into ~/.tmux/resurrect directory. You can customize the location by adding this to your .tmux.conf
set -g #resurrect-dir '/some/path'
tmux-resurrect can be combined with tmux-continuum to add auto-save and restore features.
Features:
continuous saving of tmux environment
automatic tmux start when computer/server is turned on
automatic restore when tmux is started

Related

Opening a project directory in PhpStorm from the Terminal

I want to open a project directory in PhpStorm using pstorm .
I am aware that there are many threads that have discussed this issue, but I have not been able to completely solve my problem with the solutions from those threads.
Using the IntelliJ Toolbox, I click the gear icon (top right) and I enable the Generate Shell Scripts option. I set the Shell script location to /usr/local/bin (which is in my PATH). The Shell Script Name is specified as pstorm.
The first issue is that the script isn't generated in /usr/local/bin. Presumably since the Toolbox doesn't have write privileges at that location. I then set it somewhere where it has write privileges - something like /home/username/.local/share/JetBrains/Toolbox/bin. The script is successfully generated there under pstorm:
# Generated by JetBrains Toolbox 1.20.7940 at 2021-03-24T16:55:42.325644
"/home/username/.local/share/JetBrains/Toolbox/apps/PhpStorm/ch-0/203.7148.74/bin/phpstorm.sh" "$#"
I then copy that file to /usr/local/bin: sudo cp pstorm /usr/local/bin
I then open a terminal and navigate to my desired directory, opening said directory in PhpStorm with pstorm .
PhpStorm then opens the directory. However, PhpStorm is then 'locked' as a child to the (parent) terminal instance and closing the terminal also closes PhpStorm. A more complete explanation relating to this behaviour is given here
I would really prefer that PhpStorm not be dependend on a terminal instance, much like when I run code . for VSCode.
Any ideas why it is doing this and how to 'solve' this behaviour?

Is there any way to reset Mac terminal environment to factory settings?

My Mac has gotten very messy over the years while doing various things via the Terminal. Is there any way to reset it to factory settings without resetting my Mac entirely? (e.g. uninstall all packages, reset PATH variables, etc.)
Assuming that you haven't changed anything outside your home folder, and that you're using the default shell, you can reset the shell to its defaults by running this command:
rm -i ~/.profile ~/.bash*
and you can reset the Terminal application by deleting this file:
~/Library/Preferences/com.apple.Terminal.plist
Quit Terminal and relaunch.

Mute Macbook pro when computer is shutting down

I'm thinking of using the Mac's applescript to make a program that mutes the system when it is shutting down.
Though I'm new to applescript and I don't know how to use the IF-statement to determine if the system is shutting down. I've done some googling and I've found that the finder app is the app that is "controlling" the shutdown, but i don't know how to check if the state is "shut down". Can anybody assist me in this matter?
AppleScript has no direct mechanism for detecting a shutdown/logout.
It does have a mechanism for creating applications that can react to themselves being quit.
Thus, you can:
use AppleScript to create a stay-open application (.app bundle) with a standard on quit handler, in which you perform the desired action (
make sure that the application is launched on login - in the simpler case as a Login Item (via System Preferences, see below), or, with more flexibility but complexity, as a launch agent (see https://stackoverflow.com/a/22872222/45375).
Instructions:
Open Script Editor and open a new script window.
Paste the following code:
# This standard handler is called when the application quits.
on quit
# Mute the system volume.
# !! See caveat below.
set volume with output muted
continue quit # signal to the system that it's OK to quit
end quit
Save the script as a stay-open application:
with File Format Application
check Stay open after run handler
Open System Preferences > Users & Groups > Login Items, drag the newly saved *.app bundle into the list, and check the checkbox next to it, so as to make it launch hidden.
The final step is to hide the new application's Dock icon, as there's no reason for it to have one:
From Terminal, run the following:
defaults write /full/path/to/newApp.app/Contents/Info.plist LSUIElement 1
Note: You could use LSBackgroundOnly too, but the advantage of LSUIElement is that you can still display UI elements if you want to, such as for debugging.
Important: Substitute the full path of your new app for /full/path/to/newApp.app; the command will only work if you specify the full path to the Info.plist file.
To test, start the new app interactively, and make sure that no Dock icon appears. (You can quit the app via Activity Monitor).
CAVEAT: If the intent is to suppress the system startup sound, set volume with output muted has two drawbacks:
it will not work if headphones happened to be plugged at the time of shutdown
you will have to unmute the volume on startup (however, you could do that in an on on run handler in the same app).
Consider the alternative approach below, which requires admin privileges to set up and invokes nvram SystemAudioVolume=%80 with root privileges, which bypasses the above drawbacks.
You could run do shell script "nvram SystemAudioVolume=%80" user name "someAdminUsername" password "matchingAdminPassword" with administrator privileges from the above AppleScript app, but you'd have to hard-code the password, which is not advisable for security reasons.
Alternative approach, using a system-wide logout hook via com.apple.loginwindow.
There's a deprecated mechanism for running a script on logout that, however, still works as of OSX 10.10; given that there's no direct non-deprecated equivalent, it may continue to be supported.
Note that you do need admin privileges:
sudo defaults write com.apple.loginwindow LogoutHook <yourScript>
<yourScript> must be an executable, such as a shell script; note that the executable is run in the context of the root user.
In case you're thinking of muting the startup sound, invoke the following shell command from that script:
nvram SystemAudioVolume=%80 # to try this interactively, prepend `sudo `
This will mute sounds until after a reboot, effectively muting the startup sound, without keeping the sound muted.
Note that the nvram command requires root privileges, which are by definition in effect in a script run via the com.apple.loginwindow logout hook.; by contrast, to try the command interactively, use sudo nvram SystemAudioVolume=%80 - otherwise, you'll get the following, unhelpful error message: nvram: Error setting variable - 'SystemAudioVolume': (iokit/common) general error
Honestly, it is better to make a deterministic solution. What I mean is, is that you make a script that:
Mutes your computer.
Shuts it down.
Then you take your script and create an Automator service, that you can assign to some shortcut, to make it easier for you to use it. ctrl-opt-cmd-eject or something. :)
This is just how I would have solved it, if I have the need, it is short and sweet to make work, and should work reasonably well.
If you want to use the LogoutHook mentioned in #mklement0's answer.
You can use the normal Applescript command set volume with output muted.
You just need to add the osascript shebang to the top of the Applescript document
i.e
#!/usr/bin/osascript
set volume with output muted
And then save the file as applescript text file.
In the save dialogue use : file format: Text )
It will get the extension .applescript
Once it is saved, use Terminal.app to chmod the script as you would a normal shell script which in effect it is.
i.e
/bin/chmod +x foo.applescript
Then add it to the loginwindows LogoutHook.
sudo defaults write com.apple.loginwindow LogoutHook foo.applescript
I Know this is an old post but for anyone still looking how to do this(like I was) I have a simple method.
Before I started Scripting I created a new folder in my home folder called toolbar scripts.(this is optional)
With the desktop showing Finder click on Go >Utilities >Script Editor.
In the window that opens type in or copy and paste the code
set volume with output muted
tell application "finder"
shut down
end tell
Click on the last button above the script you added - it should be compile. If you cannot find that button then on the top click on Script >Compile
Click on File >Save in the save as I called mine shutdown and chose the script folder (this is optional)
Down the bottom of the dialog box at file format click on the arrow and change the format to application and click on save.
Open the folder you saved it in and drag the icon to the dock. Click on the icon you just put in the dock.
now if all is right this should mute the volume and shutdown the computer.
This will not shutdown the computer if you still have anything open.
Cheers
Peter
first, you should create a sound-off script (with terminal)
sudo nano /Library/Scripts/sound-off.sh
after filling it with these lines:
#!/bin/bash
osascript -e ‘set volume output muted 1’
and make a sound-on script like that
sudo nano /Library/Scripts/sound-on.sh
and fill it with:
#!/bin/bash
osascript -e ‘set volume 4’
then access them as executing files
sudo chmod u+x /Library/Scripts/sound-off.sh
sudo chmod u+x /Library/Scripts/sound-on.sh
and the last part is set them when the mac device is turn off and on:
sudo defaults write com.apple.loginwindow LogoutHook /Library/Scripts/sound-off.sh
sudo defaults write com.apple.loginwindow LoginHook /Library/Scripts/sound-on.sh

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.

Resources