Terminal Bookmarks, what about bashmarks? [SOLVED] with a simple script!

Moderator: Forum moderators

Post Reply
geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Terminal Bookmarks, what about bashmarks? [SOLVED] with a simple script!

Post by geo_c »

I was looking into the best way to bookmark directories in the terminal, and I was considering bashmarks https://github.com/huyng/bashmarks

Any suggestions as to something better, or advice for using this script?

Last edited by geo_c on Sat May 20, 2023 2:25 pm, edited 1 time in total.

geo_c
Old School Hipster, and Such

Burunduk
Posts: 244
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 122 times

Re: Terminal Bookmarks, what about bashmarks?

Post by Burunduk »

So many options to choose from. Even the PPM on Fossapup offers autojump and cdargs. There are also fzf based utilities (like fzf-marks).

I've adapted this solution to my puppy. Added to /root/.bashrc:

Code: Select all

# cd bookmarks
# function to add a bookmark; usage: bm [<bookmark_name>], run again to remove
bm () { local b="/root/puppy-reference/${1:-$(basename $(pwd))}"; ln -svn "$(pwd)" "$b" || rm -i "$b"; }
# set CDPATH, don't export it (it can be without a leading ":")
CDPATH=":/root/puppy-reference"
# cd alias that dereferences links
alias cdd='cd -P'
# set bash-completion for cdd and bm
complete -o nospace -F _cd cdd
complete -o nospace -F _cd bm

It's limited but better than nothing. To use it in the terminal, cd to a directory and make a bookmark: bm mydir. This command creates a symlink to the current directory in the /root/puppy-reference (that exists already in Fossapup, if not, it's to be created manually). To cd to the mydir afterwards, it's enough to run: cd mydir where the tab-completion can be used. cdd is here for convenience, for the directory to be named as it is (not as '/root/puppy-reference/something'). It also can be used to dereference the current working directory: cdd ..
If CDPATH starts with ":", local sub-directories have precedence - cd mydir is the same as cd ./mydir if ./mydir exists and it's not possible to cd to the bookmarked directory.

geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Re: Terminal Bookmarks, what about bashmarks?

Post by geo_c »

Burunduk wrote: Thu May 18, 2023 3:34 am

It's limited but better than nothing. To use it in the terminal, cd to a directory and make a bookmark: bm mydir.

This looks good. I will experiment with it later tonight. Thanks!

geo_c
Old School Hipster, and Such

geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Re: Terminal Bookmarks, what about bashmarks?

Post by geo_c »

Burunduk wrote: Thu May 18, 2023 3:34 am

it's enough to run: cd mydir where the tab-completion can be used. cdd is here for convenience, for the directory to be named as it is (not as '/root/puppy-reference/something'). It also can be used to dereference the current working directory: cdd ..
If CDPATH starts with ":", local sub-directories have precedence - cd mydir is the same as cd ./mydir if ./mydir exists and it's not possible to cd to the bookmarked directory.

Oh yeah, that's nice, I see how it works now, bm [input] saves whatever is the current working directory as a link in puppy reference under the name [input]

So now I have a link to a commonly used directory named mydir, cuz I didn't quite get it at first!

This will work well.

So that seems to suggest that an alternate method to create bookmarks is just drop a bunch of symlinks in /root/puppy-reference. And they can be renamed just by renaming the symlink.

Am I correct about that?

I never knew what that puppy-reference directory was about, now it's starting to make sense.

Will this work in non-puppy distros like Kennel-Linux, KLV-airedale, etc? Just by creating the folder /root/puppy-reference?

geo_c
Old School Hipster, and Such

geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Re: Terminal Bookmarks, what about bashmarks?

Post by geo_c »

Also, I'm still working out the dereference function in my head, what it means exactly.

Okay, I get it, dereference meaning backout to the parent directory using cdd ..

geo_c
Old School Hipster, and Such

Burunduk
Posts: 244
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 122 times

Re: Terminal Bookmarks, what about bashmarks?

Post by Burunduk »

Ok, I'll try to explain better.

geo_c wrote: Sat May 20, 2023 1:54 am

Will this work in non-puppy distros like Kennel-Linux, KLV-airedale, etc? Just by creating the folder /root/puppy-reference?

I use this method because it's simple and doesn't require to install anything. It uses the bash built-in functionality and should work with bash on any system. If 'kennels' use bash, it's OK. I haven't tested it with other shells but CDPATH is not bash specific, so you can try.

The bookmark folder name can be different. If you are not going to use the bm function to add bookmarks, you can create any number of folders in different places and then add them all to the CDPATH environment variable: CDPATH='/root/bookmarks:/mnt/home/bookmarks:/root/other-bookmarks. Add this variable assignment to your /root/.bashrc but don't export it as it's useless in scripts and may break some of them (if they contain something like cd here instead of cd ./here).

After this you can add symlinks to directories to any of the bookmark folders and the cd command will find them. For example, if you have added a symlink /root/bookmark/funny_images that points to /mnt/home/images/puppies, you can type cd fun and press <tab> to complete it. The bookmarks names should be unique as only the first one will be found, but you can have several differently named bookmarks (symlinks) for the same directory.

The bm function and the cdd alias are just for convenience. The parameter to bm is optional. If you run bm without it, the symlink in the puppy-reference directory will be named after the current working directory basename. For example, if you have your terminal opened in /usr/local/bin, running just bm (without a bookmark name) will create a symlink /root/puppy-reference/bin (if it doesn't exist already). If you run bm once again, it will ask you whether you want to remove the symlink (enter y to do it).

Another example:

So now I have a link to a commonly used directory named mydir, cuz I didn't quite get it at first!

You can run bm mydir again (or bm my and then <tab> to complete) to remove the bookmark (or just delete the symlink manually if you prefer). While you need to cd to a directory to add a bookmark for it with bm, removing is possible from anywhere without cd.

Also, I'm still working out the dereference function in my head, what it means exactly.

When a bookmark (that is a symlink) is used to cd to a directory, the current working directory is named as the link is. So for the example above, when you run cd bin (instead of cdd bin) and then pwd, you get /root/puppy-reference/bin and this is confusing - you don't see the real directory name. But if you now run cdd ., you dereference the symlink, and pwd now returns /usr/local/bin - much better.

geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Re: Terminal Bookmarks, what about bashmarks?

Post by geo_c »

Burunduk wrote: Sat May 20, 2023 4:38 am

Ok, I'll try to explain better.

Thanks for the detailed explanation. This is valuable bash knowledge to me. The dereference is actually pretty important, as once working from a referenced directory, the actual directory address is where the symlink resides, /root/[bookmarks] as opposed to where the link points, as in any other symlinked directory, so navigation from that point on is relative to where the symlink resides as opposed to where it points,

that is until it is dereferenced with cdd

I'll be using the bm function. It's quite handy and exactly what I was looking for. Something simple, universal to all linux systems (that use bash) and easily adaptable to my personal needs.

Thanks again.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Re: Terminal Bookmarks, what about bashmarks?

Post by geo_c »

And so this works great in reference to my latest work running Frescobaldi lilypond code editor, which doesn't need to be installed because it's written in python. It can be launched with the command python3 frescobaldi and runs from the source code: viewtopic.php?p=89628#p89628

Only I store the source code in a directory that looks like this:
/mnt/home/dbox.sync.mir/app.sys/linxsys/source/frescobaldi

So I went to the directory in Xfe file manager, opened a terminal from Xfe, and ran the command bm fresco and created a bookmark in /root/puppy-reference, which allows me to now simply open a terminal anywhere and type

python3 frescobaldi to run it.

I didn't ask the question about about terminal bookmarks with frescobaldi in mind. It's just one of those synchronous benefits.

geo_c
Old School Hipster, and Such

Burunduk
Posts: 244
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 122 times

Re: Terminal Bookmarks, what about bashmarks?

Post by Burunduk »

geo_c wrote: Sat May 20, 2023 2:20 pm

So I went to the directory in Xfe file manager, opened a terminal from Xfe, and ran the command bm fresco and created a bookmark in /root/puppy-reference, which allows me to now simply open a terminal anywhere and type

python3 frescobaldi to run it.

CDPATH is similar to PATH but it's only used for the cd command. The bookmark/symlink helps to navigate faster as it's easier to type cd fresco than cd /mnt/home/dbox.sync.mir/app.sys/linxsys/source/frescobaldi. It doesn't help python to find the script. If you open the terminal somewhere outside the source directory and run python3 frescobaldi, you'll get the error message: "No such file or directory".

cd fresco; python3 frescobaldi would work, but this is not very efficient. The file frescobaldi is an executable python script and it has a shebang (#!/usr/bin/env python3), to run it from anywhere you can create a symlink to it in the /usr/bin or in other directory in $PATH. The setup.py script can do this for you, it also should add a desktop file to /usr/share/applications.

--

geo_c wrote: Sat May 20, 2023 2:00 pm

The dereference is actually pretty important, as once working from a referenced directory, the actual directory address is where the symlink resides, /root/[bookmarks] as opposed to where the link points, as in any other symlinked directory, so navigation from that point on is relative to where the symlink resides as opposed to where it points,

that is until it is dereferenced with cdd

To summarize:

shell: cd -P /symlink/to/mydir means cd /real/path/to/mydir (cdd is an alias to this command). This works in bash and busybox ash (in hush cd works as cd -P).

midnight commander: alt-l (a small 'L') hotkey - if the currently selected file is a symlink to a directory, resolve/dereference the symlink and load that directory on the other panel.

rox: right-click menu -> Window -> Follow Symbolic Links.

xfe and sunflower: probably don't have this function.

geo_c
Posts: 2501
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 1799 times
Been thanked: 705 times

Re: Terminal Bookmarks, what about bashmarks?

Post by geo_c »

Burunduk wrote: Sun May 21, 2023 6:49 am

cd fresco; python3 frescobaldi would work, but this is not very efficient.

Yes this is what I meant, I can cd fresco to the directory where the source files reside and python3 frescobaldi to run it. Pretty effiecient compared to typing the full path of where those files are stored currently.

--

Burunduk wrote: Sun May 21, 2023 6:49 am

To summarize:

shell: cd -P /symlink/to/mydir means cd /real/path/to/mydir (cdd is an alias to this command). This works in bash and busybox ash (in hush cd works as cd -P).

midnight commander: alt-l (a small 'L') hotkey - if the currently selected file is a symlink to a directory, resolve/dereference the symlink and load that directory on the other panel.

rox: right-click menu -> Window -> Follow Symbolic Links.

xfe and sunflower: probably don't have this function.

Very helpful again. I have used those functions in file managers to go to the actual directory.

geo_c
Old School Hipster, and Such

Post Reply

Return to “File Management”