How to set xterm-256colors on iTerm2 via script / command line - command-line

In iTerm2 we can set the xterm-256color option by going to Preferences -> Profiles -> Terminal -> Terminal Emulation -> Report Terminal Type and chosing xterm-256color
I want to add to my dotfile's installation script so I don't have to change it manually.
Is there a way of setting this option via command line? Maybe via iTerm itself or maybe via apple's defaults write.....
Thank you!

default works only if the setting is a top level key, for example
[admin#mb-125:~] : defaults read com.googlecode.iterm2 | grep -i promptonquit
PromptOnQuit = 1;
[admin#mb-125:~] : defaults write com.googlecode.iterm2 PromptOnQuit -bool FALSE
[admin#mb-125:~] : defaults read com.googlecode.iterm2 | grep -i promptonquit
PromptOnQuit = 0;
xterm-256color is part of a dictionary, and we need to use plistbuddy to change it, here is the command to see the current settings
[admin#mb-125:~] : /usr/libexec/PlistBuddy -c "Print :\"New Bookmarks\":0:\"Terminal Type\"" Library/Preferences/com.googlecode.iterm2.plist
xterm-256color
And this is how you change it from command line
[admin#mb-125:~] : /usr/libexec/PlistBuddy -c "Set :\"New Bookmarks\":0:\"Terminal Type\" xterm" Library/Preferences/com.googlecode.iterm2.plist
[admin#mb-125:~] : /usr/libexec/PlistBuddy -c "Print :\"New Bookmarks\":0:\"Terminal Type\"" Library/Preferences/com.googlecode.iterm2.plist
xterm

In the ~/Library/Preferences folder there's a file called com.googlecode.iterm2.plist. This file contains all your iTerm preferences. What I like to do is keep a copy of that preferences file inside my dotfile repo. When I'm installing on a new system I copy it into ~/Library/Preferences via my installation script .
Hope that helps!

Related

Show current branch on terminal

Is there any way to show in Terminal of VS Code to show in brackets current branch? I saw it somewhere but not sure how it can be done. By some extension or whatever..
C:/myUser/project> git status
I would like to see it something like:
C:/myUser/project>(master) git status
Open zshrc file
open ~/.zshrc
Add this text in the end of zshrc file
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats 'on branch %b'
setopt PROMPT_SUBST
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_} > '
Source zshrc file
source ~/.zshrc
For Linux Terminal
You can modify the PS1 variable. PS1 is a Bash Environment Variable that represents the primary prompt string which is displayed when the shell is ready.
You can achieve your result by modifying this variable with a script.
First, get the output of your current value of the variable by running
$ echo $PS1
Sample output:[\u#\h \W]$
Now you save the following code in a bash file(Remember to replace the initial string of export PS1 with the output of the above command).
#!/bin/bash
source ~/.bashrc
get_cur_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="[\u#\h \W]\$(get_cur_branch)\$ "
Let's say path of the file is "/home/samar/Documents/my_vs_script.sh"
Now change your VS code settings by adding the following lines in 'settings.json'
"terminal.integrated.shellArgs.linux": [
"--init-file",
"/home/samar/Documents/my_vs_script.sh"
]
Now each time you open a new terminal in VS code, script file "my_vs_script.sh" will execute and you get the desired output.
For Windows-Powershell
The solution above works well for the Linux terminal. But if you want to do it for another command-line shell-like Powershell, you can change the 'setting.json' to
{
"terminal.integrated.shellArgs.windows": [
"-NoExit",
"-Command", "c:/scripts/myscript.ps1"
]
}
where 'myscript.ps1' must have a function 'prompt' definition to add git branch to your prompt.
You can refer this question for your 'myscript.ps1' code.
You don't need to change 'Microsoft.PowerShell_profile.ps1'. Defining it in another file works too.
I hope it helps.

How can I make a function run every time cd successfully changes to another directory within sh on FreeBSD?

I'm using sh as my shell on FreeBSD but I want to be able to have a pretty prompt like the one bash gives me on Ubuntu. There are two things that the FreeBSD implementation of sh seems to lack as far as PS1 escape characters go:
The \w works but does not expand $HOME to ~, so this is something I have already hacked up myself
I can use PS1 to update the prompt on the terminal, but as far as I can tell it is not possible to use the PS1 variable to update the title bar as well. ESC and BEL fail to set the title as one would expect if they were using bash or ksh
Here is my .shrc file
update_prompt() {
case "$PWD" in
"$HOME"*)
pretty_pwd="~${PWD#*"${HOME}"}"
;;
"/usr$HOME"*)
pretty_pwd="~${PWD#*"/usr${HOME}"}"
;;
*)
pretty_pwd="$PWD"
;;
esac
case "$TERM" in
xterm*|rxvt*)
PS1="[$USER#\\h $pretty_pwd]\\$ "
;;
*)
;;
esac
printf "\\033]0;[%s#$(hostname -s): %s]\\007" "$USER" "$pretty_pwd"
}
update_prompt
So when I fire up a terminal or log in via ssh, it gives the pretty prompt that I like. But now I need this function to run every time that cd is executed and returns an exit status of 0.
I was going to use an alias that was something like:
alias cd="cd $1 && update_prompt"
but that was before I realized that aliases do not except arguments. How might I go about doing something like this?
You can use a function instead of an alias:
cd() {
command cd "$#" && update_prompt
}
Just put it into ~/.shrc. You have to use command here to let sh know that you are referring to the actual cd builtin command instead of the function you've just defined.
Refer to the sh(1) manual page for the details on how to make sh(1) source the ~/.shrc file when it starts:
Therefore, a user should place commands that are to be executed only at login
time in the .profile file, and commands that are executed for every shell
inside the ENV file. The user can set the ENV variable to some file by placing
the following line in the file .profile in the home directory, substituting for
.shrc the filename desired:
ENV=$HOME/.shrc; export ENV
I use this trick in my cd alias manager. Here's a link to the source code of the function: https://github.com/0mp/goat/blob/v2.5.0/libgoat.sh#L31-L57
You can do it with alias+arguments if you swap the commands:
$ alias cd="echo change; cd"
$ pwd
/nas
$ cd /
change
$ pwd
/
$ cd /etc
change
$ pwd
/etc
$

launch sublime text 3 in terminal with zsh

I recently purchased a new MacBook and I am trying to re-configure my system.
The app is inside the Applications folder as 'Sublime Text.app'
I have edited the sublime.plugin.zsh file via other advice I found online to 'Sublime Text 3.app' as well as 'Sublime Text.app' with no luck on either:
elif [[ $('uname') == 'Darwin' ]]; then
local _sublime_darwin_paths > /dev/null 2>&1
_sublime_darwin_paths=(
"/usr/local/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
)
for _sublime_path in $_sublime_darwin_paths; do
if [[ -a $_sublime_path ]]; then
alias subl="'$_sublime_path'"
alias st=subl
break
fi
done
fi
alias stt='st .'
I still get
zsh: command not found: st
I am simply at a loss on where to go next
I had the same problem with zsh and this did the job:
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl
Then you launch a open a file my_file.txt with Sublime:
subl ./my_file.txt
Don't specify any file if you just want to open Sublime. I hope this helps ;)
First, try to first launch the sublime binary manually (interactively) via zsh.
To do that, you'll have to discover where this binary is. There are two practical options here, choose what you are most comfortable with:
Check manually those listed binaries, see which of them exist.
Slightly modify your script to echo something inside your if:
if [[ -a $_sublime_path ]]; then
echo "Sublime found: $_sublime_path"
alias subl="'$_sublime_path'"
alias st=subl
break
fi
After finding the correct one, create the st alias in your .zshrc file:
alias st="/correct/path/to/subl"
If you don't find anything in the first step, then your original script is really not supposed to work.
Just moved to App in mac
Check your current path
echo $PATH
Add a sym link from Sublime App to one of your path. Choose /usr/local/bin for example
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
Then back to terminal and run sublime. You should be open the sublime through terminal
To setup alias for mac users;
open ~/.zshrc using the below command
vi ~/.zshrc
Add the following alias
alias subl="'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl'"
run subl . command should work properly.
Official documentation: https://www.sublimetext.com/docs/command_line.html#mac
ZSH
If using Zsh, the default starting with macOS 10.15, the following command will add the bin folder to the PATH environment variable:
echo 'export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"' >> ~/.zprofile

Merging files in Sublime with Sublimerge via command line

I'm trying to find a better merge file option and wanted to try out Sublime as of my work is done using it. so I installed Sublimerge and now am stuck. I know I can compare two already open files or compare via the Sidebar but what I want to do is fire it off via the command line so I can kick it off from our source control program like I can with every other merge tool I've seen. Does anyone know the command line format to do this?
N.B. - I've long since given up trying to use sublime to handle merges and instead switched to other tools to handle this. Therefore I've never felt I can accept any answer as I'm not checking them to see if they work in the way I'd want, or indeed whether they work at all.
subl -n --wait "<LEFT>" "<RIGHT>" --command "sublimerge_diff_views {\"left_read_only\": true, \"right_read_only\": true}"
See "VCS Integration" for details.
As Nickolay already suggested, this is the whole directive you have to put in your ~/.gitconfig:
[merge]
tool = sublimerge
[mergetool "sublimerge"]
cmd = subl -n --wait \"$REMOTE\" \"$BASE\" \"$LOCAL\" \"$MERGED\" --command \"sublimerge_diff_views\"
trustExitCode = false
[diff]
tool = sublimerge
[difftool "sublimerge"]
cmd = subl -n --wait \"$REMOTE\" \"$LOCAL\" --command \"sublimerge_diff_views {\\\"left_read_only\\\": true, \\\"right_read_only\\\": true}\"
Taking jnns' answer, but making appropriate changes for windows.
%USERPROFILE%\.gitconfig:
[merge]
tool = sublimerge
[mergetool "sublimerge"]
cmd = sublime_text -n --wait \"$REMOTE\" \"$BASE\" \"$LOCAL\" \"$MERGED\" --command \"sublimerge_diff_views\"
trustExitCode = false
[diff]
tool = sublimerge
[difftool "sublimerge"]
cmd = sublime_text -n --wait \"$REMOTE\" \"$LOCAL\" --command \"sublimerge_diff_views {\\\"left_read_only\\\": true, \\\"right_read_only\\\": true}\"
note location of .gitconfig
in windows, the executable is sublime_text.exe, NOT subl
don't forget to add sublime text executable to the path
I'm not sure exactly how to do it, but I'm getting closer.
First, you need a handy path to the Sublime binary:
mkdir ~/bin
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl
Then, in your git GUI or git command line, configure ~/bin/subl as your merge tool.
I use Source Tree, and I haven't figured exactly how to best use Sublimerge, but I managed to open both versions, merge them, and then I have to do a bit of manual work.
Still not smooth, but better than nothing!

iPhone .bashrc not working properly

SOLVED: I had the wrong line endings selected in my text editor
I tried to get a .bashrc going on my iPhone, just for fun. After adding source /var/root/.bashrc to my /etc/profile file I get this every time I log into a terminal emulator, local or over SSH.
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
>
My cursor overwrites the '>' everytime I type
The contents of /var/root/.bashrc
alias install='apt-get install'
alias remove='apt-get remove'
alias aptsearch='apt-cache search'
alias respring='killall SpringBoard'
alias safemode='touch /var/mobile/Library/Preferences/com.saurik.mobilesubstrate.dat && killall SpringBoard'
alias shutdown='halt'
alias poweroff='halt'
alias ls='ls -group-directories-first -Ah'
alias lsl='ls -Ah1 --group-directories-first'
alias killall='killall -v'
alias reload='source /var/root/.bashrc'
export PS1='\w> '
clear
The contents of /etc/profile
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games'
export PS1='\h:\w \u\$ '
umask 022
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
source /var/root/.bashrc
Any and all help is appreciated.
SOLVED: I had the wrong line endings selected in my text editor
Try manually executing the script and taking a look at the output. You should get a line number to help you along.