Adaptive Ruby version when opening a terminal - ruby-on-rails

This question is the opposite of this one (also asked here, here and here)
I have two versions of ruby installed
ubuntu:~/environment $ rvm list
ruby-2.6.6 [ x86_64 ]
=* ruby-3.0.2 [ x86_64 ]
and every time I open a terminal window, ruby-3.0.2 (the default) is set. The problem is for a couple of my older projects I have to use ruby-2.6.6, so every time I have to switch with
rvm use 2.6.6
Is there a way to automatically select ruby 2.6.6 when I open the terminal window of the specific projects? I have tried to override the default rvm version with the .ruby-version file (as suggested here) but it does not do the trick.
EDIT File /home/ubuntu/.rvm/scripts/cd contains the following
#!/usr/bin/env bash
# Source a .rvmrc file in a directory after changing to it, if it exists. To
# disable this feature, set rvm_project_rvmrc=0 in /etc/rvmrc or $HOME/.rvmrc
case "${rvm_project_rvmrc:-1}" in
1|cd)
# cloned from git#github.com:mpapis/bash_zsh_support.git
source "$rvm_scripts_path/extras/bash_zsh_support/chpwd/function.sh"
# not using default loading to support older Zsh
[[ -n "${ZSH_VERSION:-}" ]] &&
__rvm_version_compare "$ZSH_VERSION" -gt 4.3.4 ||
{
function cd() { __zsh_like_cd cd "$#" ; }
function popd() { __zsh_like_cd popd "$#" ; }
function pushd() { __zsh_like_cd pushd "$#" ; }
}
__rvm_after_cd()
{
\typeset rvm_hook
rvm_hook="after_cd"
if [[ -n "${rvm_scripts_path:-}" || -n "${rvm_path:-}" ]]
then source "${rvm_scripts_path:-$rvm_path/scripts}/hook"
fi
}
__rvm_cd_functions_set()
{
__rvm_do_with_env_before
if [[ -n "${rvm_current_rvmrc:-""}" && "$OLDPWD" == "$PWD" ]]
then rvm_current_rvmrc=""
fi
__rvm_project_rvmrc >&2 || true
__rvm_after_cd || true
__rvm_do_with_env_after
return 0
}
[[ " ${chpwd_functions[*]} " == *" __rvm_cd_functions_set "* ]] ||
chpwd_functions=( "${chpwd_functions[#]}" __rvm_cd_functions_set )
# This functionality is opt-in by setting rvm_cd_complete_flag=1 in ~/.rvmrc
# Generic bash cd completion seems to work great for most, so this is only
# for those that have some issues with that.
if (( ${rvm_cd_complete_flag:-0} == 1 ))
then
# If $CDPATH is set, bash should tab-complete based on directories in those paths,
# but with the cd function above, the built-in tab-complete ignores $CDPATH. This
# function returns that functionality.
_rvm_cd_complete ()
{
\typeset directory current matches item index sep
sep="${IFS}"
export IFS
IFS=$'\n'
COMPREPLY=()
current="${COMP_WORDS[COMP_CWORD]}"
if [[ -n "$CDPATH" && ${current:0:1} != "/" ]] ; then
index=0
# The change to IFS above means that the \command \tr below should replace ':'
# with a newline rather than a space. A space would be ignored, breaking
# TAB completion based on CDPATH again
for directory in $(printf "%b" "$CDPATH" | \command \tr -s ':' '\n') ; do
for item in $( compgen -d "$directory/$current" ) ; do
COMPREPLY[index++]=${item#$directory/}
done
done
else
COMPREPLY=( $(compgen -d ${current}) )
fi
IFS="${sep}";
}
complete -o bashdefault -o default -o filenames -o dirnames -o nospace -F _rvm_cd_complete cd
fi
;;
2|prompt)
if
[[ -n "${ZSH_VERSION:-}" ]]
then
precmd_functions+=(__rvm_do_with_env_before __rvm_project_rvmrc __rvm_do_with_env_after)
else
PROMPT_COMMAND="${PROMPT_COMMAND%% }"
PROMPT_COMMAND="${PROMPT_COMMAND%%;}"
PROMPT_COMMAND="${PROMPT_COMMAND:-}${PROMPT_COMMAND:+; }__rvm_do_with_env_before; __rvm_project_rvmrc; __rvm_do_with_env_after"
fi
;;
esac

You are probably using RVM as a shell script, and not as a shell function.
You can check like this in a typical shell (bash, zsh, ...) : execute: type rvm
If it displays rvm is /home/ying/.rvm/bin/rvm : you are using as a script (found in $PATH)
If it displays rvm is a function : you are using as a function (much better).
Check out: https://rvm.io/rvm/basics#post-install-configuration
If you are using as a script, and want to use as a function: you need to "source" the rvm function, it is located in <rvm main folder>/scripts/rvm, for instance if installed in $HOME:
source $HOME/.rvm/scripts/rvm
source /usr/local/rvm/scripts/rvm
Typically, at RVM installation time, it add the following line in the equivalent of .profile (depending on shell and if its global or user):
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
RVM changes automatically the version as described here:
https://rvm.io/workflow/projects
For detection of .ruby-version, the following is required:
RVM must be a recent version that supports the feature
RVM must be loaded in the shell (typically by .profile, or equivalent) so that is is executed as a function
the shell must be compatible with this callback feature (bash and zsh are)
Here is what happens:
When you load rvm as a function, it registers callback in the shell
when you cd into the project, RVM callbacks (in shell) detect the file .ruby-version (or others) and automatically do the equivalent of rvm use.
For instance, I use zsh (on osx) which has preexec and precmd callbacks
and it detect the ruby version file and applies when I cd into it or a sub folder.
It works with bash too.
If you are curious or want to see why it does not work for you look at the file <rvm main dir>/scripts/cd
typically the shell variable chpwd_functions is set to __rvm_cd_functions_set
which is the function called after a cd by rvm

Related

Oh my zsh / Iterm2 auto suggestion color is broken?

see screenshot below.
Added my .zshrc file also below.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="eastwood"
plugins=(git zsh-syntax-highlighting zsh-autosuggestions sudo web-search copypath copyfile copybuffer dirhistory history jsontools macos ruby rails)
source $ZSH/oh-my-zsh.sh
eval "$(/opt/homebrew/bin/brew shellenv)"
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="/Applications/Sublime Text.app/Contents/SharedSupport/bin:$PATH"
export PATH=/opt/homebrew/bin:$PATH
Fixed by changing the color of LIGHT CYAN in iTerm2.
And using theme "Dark Tango"
See screenshots.
It is a feature of irb and it is enabled as of Ruby 3.1
The new IRB console has an autocomplete function where a list with suggestions appears, and in addition, the documentation (if exists) of the selected option will appear.
You can disable it by adding code below to the ~/.irbrc file
IRB.conf[:USE_AUTOCOMPLETE] = false
Or if you just want to turn off colors:
IRB.conf[:USE_COLORIZE] = false
Or if you want to see or/and to change other settings of your IRB enviroment type IRB.conf in your irb console.

How to get asdf and rvm to co-exist

Is it possible to have asdf and rvm coexist? If so, how do you set it up? I made a test project to try out asdf but it seems that's affecting another existing project that's managed by rvm. When I run rails I'm getting:
asdf: No version set for command ruby
you might want to add one of the following in your .tool-versions file:
ruby 2.6.1
I've came across the same issue while installing asdf in macOS. I was able resolve it by creating .tool-versions file and adding the ruby version entry. You can do the same by running following command in the terminal.
$ echo 'ruby 2.6.1' >> .tool-versions
more information can be found here in this blog post
This is the hack I currently use. Running use-rvm or use-asdf uncomments the respective line in my ~/.bash_profile, and comments the unwanted line.
# RVM
# source $HOME/.rvm/scripts/rvm
# ASDF
. $HOME/.asdf/asdf.sh
. $HOME/.asdf/completions/asdf.bash
# Add Visual Studio Code (code)
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
use-rvm () {
sed -i "" 's|^# source $HOME/.rvm/scripts/rvm|source $HOME/.rvm/scripts/rvm|' ~/.bash_profile
sed -i "" 's|^. $HOME/.asdf/asdf.sh|# . $HOME/.asdf/asdf.sh|' ~/.bash_profile
bash --login
}
use-asdf () {
sed -i "" 's|^source $HOME/.rvm/scripts/rvm|# source $HOME/.rvm/scripts/rvm|' ~/.bash_profile
sed -i "" 's|^# . $HOME/.asdf/asdf.sh|. $HOME/.asdf/asdf.sh|' ~/.bash_profile
bash --login
}
And here's the gist

How can I avoid certain lines from .bashrc and .inputrc from being loaded in tmux?

I have these lines in my .inputrc:
"(": "\C-v()\ei"
"[": "\C-v[]\ei"
"{": "\C-v{}\ei"
"\"": "\C-v\"\C-v\"\ei"
"\'": "\C-v\'\C-v\'\ei"
This autocloses quotes and brackets in a terminal. But it causes an inconvenience in a tmux session: when I send text containing quotes to frome one pane (vim) to another pane (bash / python / R etc), every quote is turned into two, very annoying.
Is it possible to disable these lines in (and only in) tmux?
tmux sets the TMUX environment variable, so in .bashrc (or .profile or whatever):
if [ '' = "$TMUX" ] ; then
echo not in TMUX
else
echo in TMUX
fi
You can set INPUTRC to override the default .inputrc location, so you could have a tmux one and a non-tmux one, and export a suitable INPUTRC value in .bashrc depending on TMUX. You could even concoct a suitable .inputrc (e.g. in /tmp) for that session based on a "common" file and a "non-tmux session" file.
Unfortunately tmux exports TMUX, and so subshells started from a tmux session will have TMUX set regardless. Not found a way round that yet.
I ended up doing this in ~/.bashrc:
if [[ '' = "$TMUX" ]]
then
set -o vi
bind -m vi-insert '"(" "\C-v()\ei"'
bind -m vi-insert '"[" "\C-v[]\ei"'
bind -m vi-insert '"{" "\C-v{}\ei"'
bind -m vi-insert '"\"" "\C-v\"\C-v\"\ei"'
bind -m vi-insert '"\047" "\C-v\047\C-v\047\ei"'
else
echo Welcome to Tmux!
fi
UPDATE
Adopting user3392484 's suggestion, I found this much better:
if [[ '' = "$TMUX" ]]
then
export INPUTRC=~/.inputrc
else
export INPUTRC=~/.tmux.inputrc
echo Welcome to Tmux!
fi

Is there a good macvim git diff?

I have been using textmate for many years and I just made the switch to macvim and one thing that I used all the time with textmate was the command git df which in my .gitconfig was just an alias for
[alias]
df = !git diff | mate
and what that did was give me a screen like this
Is there a replacement in mvim that I can add somewhere for me to get similar behavior
I describe what I use here.
Basically, add the following lines to your "~/.gitconfig":
[diff]
tool = default-difftool
[difftool "default-difftool"]
cmd = default-difftool.sh $LOCAL $REMOTE
With the following wrapper script:
#! /bin/bash
if [[ -f /Applications/MacVim.app/Contents/MacOS/Vim ]]
then
# bypass mvim for speed
VIMPATH='/Applications/MacVim.app/Contents/MacOS/Vim -g -dO -f'
elif [[ -f /usr/local/bin/mvim ]]
then
# fall back to mvim
VIMPATH='mvim -d -f'
else
# fall back to original vim
VIMPATH='vimdiff'
fi
$VIMPATH $#
You can get the diff one file at a time by doing:
git difftool -t vimdiff
vimdiff can be replaced with gvimdiff for gvim, so I would assume you can also replace it with mvimdiff for macvim.
I am not sure of a way to pipe the entirety of git diff into vim though.

About Ruby Version Manager Installation

I was trying to install RVM:Ruby Version Manager from:
http://rvm.beginrescueend.com/rvm/install/
I opened up my terminal in my mac os version 10.5.8 and use the command line
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
then I got the message,
"You must now finish the install manually:
1) Place the folowing line at the end of your shell's loading files(.bashrc or .bash_profile for bash and .zshrc for zsh), after all path/variable settings:
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm
Please note that this must only occur once - so, you only need to add it the first time you install rvm.
2) Ensure that there is no 'return' from inside the .bashrc file. (otherwise rvm will be prevented from working properly).
This means that if you see '[ -z ] && return' then you must change this line to:
if [[ ! -z ]] ; then
... original content that was below the && return line ...
fi # <= be sure to close the if.
#EOF .bashrc
Be absolutely sure to REMOVE the '&& return'.
If you wish to DRY up your config you can 'source ~/.bashrc' at the bottom of your .bash_profile.
placing all non-interactive items in the .bashrc, including the 'source' line above
3) Then CLOSE THIS SHELL and open a new one in order to use rvm.
Installation of RVM to /Users/Home/.rvm/ is complete.
kapplej-4:~ Home$ #!/usr/bin/env bash
kapplej-4:~ Home$
kapplej-4:~ Home$ # Install git
kapplej-4:~ Home$ mkdir -p $HOME/.rvm/src && cd $HOME/.rvm/src && version=1.6.5.3
kapplej-4:src Home$ curl -O http://kernel.org/pub/software/scm/git/git-$version.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2645k 100 2645k 0 0 953k 0 0:00:02 0:00:02 --:--:-- 1060k
kapplej-4:src Home$ cd git-$version && ./configure --prefix=/usr/local && make && sudo make install
-bash: cd: git-1.6.5.3: No such file or directory"
I have no idea how to complete installation manually, I am a complete newbie so can someone please give me a step by step instruction on how to do this. For eg, where do I go to add those lines to the shell's loading files?
Thanks,
Not sure why you're installing git through rvm, but my guess is that you didn't extract the tar.gz file before trying to access the directory. Before cd'ing into the git directory you'll need to do
tar -zxf git-$version.tar.gz
To complete the install you need to add the line:
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm
to the end of your .bashrc file (assuming you're using bash which you can determine with echo $SHELL.)
You also need to verify that your bash script doesn't return (the -z return bit in the RVM message. If it does return do what RVM says during the install.)
Once the line is added exit your shell and open a new one. You should then be able to do rvm info to see rvm info and rvm to get more information on RVM.
If Git is already installed, the up-to-date command to install RVM is curl -L get.rvm.io | bash -s stable.

Resources