LinkedIn Sourceforge Twitter

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

KSH bookmark utility

Posted on 2021-01-05 21:38:00 from Vincent in OpenBSD

Following my last post I had an interesting conversation about Qterminal: "a lightweight Qt terminal emulator based on QTermWidget".

The discussion was about the "lightweight" of a terminal and the comparison against xterm. But this is not the most interesting part of the discussion. To my eyes, the most interesting feature of Qterminal is the bookmarks.

You just click and the terminal is immediately in the corresponding folder. For a lazy person like me this is fantastic.

Thus, this blog is about Bookmark in KSH.


Introduction

What a nice feature Qterminal has implemented: bookmarks for a shell. It surely should exist somewhere else, but personally, I've never thought about it.

My solution, until last week, was to use alias:

alias cddev=cd ~/projects/nas/ksh-bookmarks/src

Such solution is perfect if you have not so many bookmarks. Once the list of folders increase, an other solution is more than welcome.

ksh completion

I have build a solution which will use the completion concept developed in ksh.

In short we must build an array with the list of possible entries
If my command's name is "kshbookmark", the completion array will look like this:

set -A complete_kshbookmark bookmark1 bookmark2 bookmark3 ...

So basically the idea will be to list the possible bookmarks by a short name. And to change the current directory to the one associated with the bookmark's name.

ksh-bookmark.sh

I've build a small script called "ksh_bookmark.sh".

My preference is to load it in my .profile ou .kshrc. If you don't like to option, you can adapt the script to become a standalone executable.

Download:

You can download it here

Installation

You append it to your .profile or .kshrc file by doing:

cat ksh_bookmarks.sh >> .kshrc

Doing this will add in your ksh a new shell's command called

kshbookmark

Please note that I've performed an alias of the kshbookmark command to "b".

alias b=kshbookmark

The completion build by this script is based on this short name "b". Please adapt it accordingly if you do not want to "b" command.

Configuration

There is no configuration file.

The only parameter if the name of the file where your bookmarks will be stored= BM_FILE.

By default this file is:

~/.ksh_bookmarks

But you can change it by doing:

export BM_FILE=/your/own/path/name

If you want, you can thus have multiple bookmark file and switch from them by changing this variable.

Usage:

Display the content of your bookmarks:

b<enter>

Switch to a bookmarked folder:

b <bookmark name> <enter>
b <letter><tab> <enter>

Since they are defined in the completion array typing the first letter, then tab will avoid you to type the full name.

Add a new bookmark:

b _add
b _add <bookmark name>

This command bookmark the current folder ($PWD). the name of the bookmark will be the folder path. You can overwrite it by provide a "bookmark name".

As you have guessed, there is 2 bookmark's name to avoid: _add and _edit. Those name are reserved for the "add" and "edit" command.

Modify or delete a bookmark:

b _edit.

This command will trigger you default EDITOR or vi to edit the BM_FILE.

There a few rules to respect in this file:

- comments are allowed, but at the beginning of a line with the symbol "#"
- between bookmark's name and folder, we must have 1 TAB
- space in bookmark's name are not allowed. No problem for the folder's name.

Demo

Here after a small demo of ksh-bookmark.sh in action:



Conclusion

As a lazy person, I'm amazed to not have such feature before. Few days, I'm using it, but I do not understand why I had not that before.

Thanks to the standard folder's completion of KSH, at first instance, it's easy to switch from one folder to another one. Indeed, in most of the case the 1st letter an TAB is just enough.

But with lot of folders, I'm always typing <letter>, <tab>, <letter> <tab>, ...

Since I'm using ksh-bookmark, I just type "b" <letter> <tab>. And boom. I'm in the righ folder.

At the end of the day, this is much more comfortable to have such bookmarking tool.



9, 7
displayed: 1756
Comments:

1. From russell on Fri Jan 8 14:42:58 2021

Not sure if it meets your requirements but ksh has a CDPATH variable that may do what you want. http://man.openbsd.org/ksh#CDPATH

2. From Vincent on Fri Jan 8 17:18:25 2021

Hello Russel. Thanks for your message. Indeed, with CDPATH you can switch more easily from few folders. This surely helps is some cases. A bit like "cd old new" could help too in some cases. But the script here offers much more




What is the first letter of the word Python?