Finally, I want to show you how to write your own custom replacement for the 'cd' command.
Do you find yourself always typing the same thing upon changing into a directory? You probably at least list the files there every time, perhaps so much that your hands automatically type 'ls' after every 'cd'.
Well, by trying every way I could think of, it turns out there's only one way to properly accomplish the goal we're seeking. We have to create a shell function.
Shell functions are part of shell programming. Like in compiled programming languages, functions provide a sort of procedural modularizability. One can create a generic function to perform an often-used bit of logic or computation with different parameters. In this case, the parameter is the current working directory.
Here's a simple one:
function cs () {
cd $1
ls
}
As @geirha corretly notes, the above function will fail if you try to switch to a directory with a space in its name:
$ cs A\ B/
-bash: cd: A: No such file or directory
<current directory listing>
You should instead use the following function:
function cs () {
cd "$@" && ls
}
Once you add that code to your ~/.bashrc, you should be able to do this:
As an expansion to this function: cs() { cd "$1" && ls; }, you may want to pass all of the function's arguments to cd by using $@ instead of "$1" as such: cs() { cd $@ && ls; }.
I had problems redefining cd because rvm changes my cd definition too. See https://stackoverflow.com/a/19941991/1601989. I didn't really want to use builtin because that would skip whatever rvm is doing.
I added the following to my .bashrc:
# cdd allows you to cd to the directory of the given file or directory
function cdd()
{
if [[ $# -eq 0 ]]; then
cd
elif [[ -d "$*" ]]; then
cd "$*"
elif [[ -f "$*" ]]; then
echo "WARNING: file given, cd to file's dirname" 1>&2
local dir=$(dirname "$*")
cd "$dir"
else
cd "$*"
fi
}
function cs()
{
cdd $* && ls
}
And then after the rmv line in the .bashrc:
alias cd='cdd'
# Use bash built in completion for cd to allow for filenames to be used
complete -r cd
From Bash Tips and Tricks: 'cd' with style:
As @geirha corretly notes, the above function will fail if you try to switch to a directory with a space in its name:
You should instead use the following function:
Once you add that code to your
~/.bashrc
, you should be able to do this:You can use the
builtin
command in bash :Use a function instead of an alias:
Thanks Florian Diesch for the tip of using a function. I can't use
cs
as the name because there is acs
command in the csound package, so I usedlc
.I added this to
~/.bash_aliases
(nano ~/.bash_aliases
):The terminal needs to be
reset
for this to come into effect.As an expansion to this function:
cs() { cd "$1" && ls; }
, you may want to pass all of the function's arguments tocd
by using$@
instead of"$1"
as such:cs() { cd $@ && ls; }
.I had problems redefining
cd
becauservm
changes mycd
definition too. See https://stackoverflow.com/a/19941991/1601989. I didn't really want to usebuiltin
because that would skip whateverrvm
is doing.I added the following to my
.bashrc
:And then after the
rmv
line in the.bashrc
: