How can I configure ripgrep to always search from my home directory? - ripgrep

How can I configure my ~/.ripgreprc glob(s) to always search from the home directory regardless of where I run rg from?

I'm pretty sure you can't configure ripgrep to do that, and it would be a bad idea to do that if you could, but you can make a shell function to do what you want.
Put this in your ~/.bashrc:
function rgh()
{
rg "$#" ~/
}
Then you just rgh whatever and it will expand to rg whatever ~/.
(I chose rgh for "ripgrep home" but you can call it whatever you want.)

Related

How do I give my shell script a simple word to make it run. For example "mkdir"

so I have a bash script right now which automates the git process for me. I have made the shell script accessible from everywhere. I want to give the script a command like "ctdir" instead of typing in "intilize_directory.sh" every time. Is there a way to make this possible?
There are (at least) three ways to do this:
First, if it's on your path, you can simply rename it to ctdir.
Second, you can create an alias for it in your startup scripts (like $HOME/.bashrc):
alias ctdir='initialize_directory.sh'
Third, you can create a function to do the work (again, defining it in your startup scripts):
ctdir() {
initialize_directory.sh
}
Just remember to make sure you load up your modified startup scripts after making the changes. New shells should pick the changes up but you may need to re-source it manually from an existing shell (or just exit and restart).
Agreed with #paxdiablo, the best way is to create an alias.
Following steps will work in Linux:
Naming the alias.
Type the following at the command line:
alias ctdir='initialize_directory.sh'
Edit bashsrc file.
This file is usually present at your home directory.
Add at the alias mentioned in step 1 at the end of the bashsrc file to make them permanent and reusable in every session.
vi ~/.bashsrc

Create Own Module I Can Use On Terminal

I'm working on a mac terminal and I'm creating some simple commands that I can run anywhere. These are mostly to do things I repeatedly do in one simple command, so I don't have to type it out every time. Some examples:
alias changetocertaindestination="cd ~/repos/certaindestination"
opencertainfile() {
open ~/repos/mydestination/certainfile.txt
}
I would like to know how to make it so I can use the notation that other modules/packages(I'm not sure what they are called) use, i.e. git branch , instead of gitbranch
So instead of every prefacing every alias with changeto, and using changetodestination I can use changeto destination.
You just need to parameterize your functions.
changeto () {
cd -- ~/repos/"$1"
}
$ changeto certaindestination
$ pwd
/home/alex/repos/certaindestination

Autocomplete function argument in shell function

Have a couple quick bash functions that let me get to folders I use often:
function dp() {
cd ~/Development/Personal/$1
}
function dw() {
cd ~/Development/Work/$1
}
So I can type dp some-project to go directly to a personal project for example. It would be killer if I could get tab-completion working on the function argument so it automatically pulled in filenames from the directory I'm going to navigate to, but I can't seem to figure it out...
Ideally I could just type dp b{tab} and get dp blog for example, where blog is a folder in the ~/Development/Personal/ directory.
Anyone know how to make this work? I'm using ZSH if it matters!
It does matter very much which shell you're using.
First, let me mention that you can add often used directories to a "hotlist" that you can then use with cd. For instance, you could do this:
cdpath=(~/Development/Personal ~/Development/Work)
and then, at any point (and from any directory), you should be able to type cd blog. Tab completion will work as well.
If you still want to have your own functions for cd and add tab completion for them, here's a very informative article about how to write your own completion functions.
In a nutshell, you create a file in the zsh completion directory, called _dp, and add something like this to it:
#compdef dp
compadd $(command ls -1 $HOME/Development/Personal 2>/dev/null --color=none | sed -e 's/ /\\ /g')

how to correctly call unix command from other dirs

I have a relatively simple question that I cant figure out and I cant figure out the right search query to find the info I need on google so I thought I would ask the collective.
In short:
cd /var/www/config
./deploy.sh - works!
but
./var/www/config/deploy.sh
doesnt :(
deploy.sh calls another bash script and it seems that the called script cant find the libs it needs because it searches relative to where it was called from which in this case would be / instead of /var/www as it expects.
I'm trying to call this from a capistrano script therefore need to find a way to call it without having to cd first. Does anyone know a simple way to achieve this?
EDIT: Thanks for your quick suggestions, its still playing up. deploy.sh calls another bash file called sake. I have uploaded a copy here http://tinypaste.com/25fc8
Cheers guys!
Don't put a . (period) in front of your command. Just use:
$ /var/www/config/deploy.sh
You can also wrap it too so you can return to existing dir, sometimes proggies like to pick up the PWD in which to work so might be worth setting it explicitly:
( cd /var/www/config/ && ./deploy.sh )
If you want to remain where you are after the command is done:
(cd /var/www/config; ./deploy.sh)

Coloring directory name in ksh

In my current situation, it is not unusual for me to have several UNIX computers I connect to, as several different users depending on the situation, and to traverse through various directories on the machines doing things. I use ksh through the entire thing.
I was fiddling with my prompt recently, and I was able to get it to change some colors depending on my current username and current server. However, what I would also want is for it to change colors based on my current directory. For example, if I were in directory "foo", the prompt should be yellow, but if I were in directory "bar", the prompt would be magenta. In both cases, subdirectories should also count, so a simple substring check should be enough.
The problem I ran into, however, is that when I run my .profile script, it properly colors the directory--but it no longer dynamically updates whenever I switch to another directory--and I'm not sure how before I did all the branching, I was able to get it to print my current working directory correctly even after I switched directories.
I did some googling, and find information for bash, but ksh seems to be largely ignored. As I cannot figure out how to do this on my own, I must bring it to the Stack Overflow community, to add it to future knowledge. Thus, with my long-winded explanation, the "quick version" of my question is as follows:
In ksh, how can I set up my prompt to display the current working directory and color the text based on where the current working directory is? Is it even possible?
Why not using zsh? It is based on ksh, and it is much more powerful. In zsh you can write chpwd function that is implicitly called every time you change directory. In this function you can check your current directory and set PS1 to whatever you want.
Alternatively (even in ksh) you can create an alias for cd command:
change_my_ps() {
PS1=...
}
better_cd() {
builtin cd "$#"
change_my_ps
}
alias cd=better_cd
Something like this. I'm not sure it is proper, I don't remember ksh syntax.
I was able to dig up a semi-solution here:
http://books.google.com/books?id=QYu_v2R6fIQC&pg=PA71&lpg=PA71&dq=korn+dynamic+prompt&source=bl&ots=yMEZiWrGyU&sig=8KBbs12Mtk3eGNSZQiLVmFYZVFY&hl=en&ei=2HX4Sej0K6LWMP2NxakP&sa=X&oi=book_result&ct=result&resnum=2#PPA72,M1
Though enacting it is still difficult.
To display the current directory in ksh, put this in your .profile file:
export PS1="\$PWD "
That will dynamically update when you change directory without mucking around with functions.
Since nobody actually answered the part about the color, here's how I'd do it. I can't color the text, so I've also included a screenshot of my terminal.
See Wikipedia's page on ANSI escape codes for the full list of color codes and print --man and printf --man for the details of printing escape sequences in ksh.
(Note that ksh93v, currently in beta, will contain a prompt language based (I think) on bash's, which will make this sort of thing somewhat easier, though I think you'll probably still need to use something like this function to do complex conditional prompts like this one.)
> cat ~/scripts/prompt
function prompt
{
set -eu
typeset c=
case $PWD in
*/foo) c=3;; # yellow
*/bar) c=5;; # magenta
esac
print "\E[3${c}m$PWD\E[m > "
}
PS1='`prompt`'
> . ~/scripts/prompt
/Users/adavies > cd foo
/Users/adavies/foo > cd ../bar
/Users/adavies/bar >
I use this:
function chdir
{
cd "$#"
CWDH=${PWD%/*}
PS1="($_time)$hname:${CWDH##*/}/${PWD##*/} ->"
export PS1
}
alias cd=chdir
chdir .
Ignore the time and hname, but the rest should work for you. Changing colors is going to be terminal dependent. You need to know the escape codes for each color for the terminal you will be using. If you know you only ever use an xterm, it will be easier.

Resources