1

Say I've been working on the same project for the past few weeks, every day I like to start with a fresh terminal. This means I always start with cd /path/to/project.

When working with iTerm2 on the Mac, I very much enjoy the option to show my most frequently (and recently) used directories. Described here under 'Recent Directories'.

A quick keystroke and a pop-up appears allowing me to complete a directory path after typing cd.

Is there anything in Ubuntu which can match this behaviour?

  • Are you familiar with Tab completion? – wjandrea Jan 4 '19 at 13:39
  • @wjandrea Am I right to think that this doesn't give a list of recently/commonly used folders? – LondonRob Jan 4 '19 at 13:41
  • Yes, you are right. cd <tab> gives you all the subdirectories in the current directory. It is a general feature suggesting also command and file name completions. – muclux Jan 4 '19 at 14:02
  • @London It doesn't, no, but it can complete the names of directories. I'm sort of wondering if you've tried using it but found it inconvenient, or if you've considered customizing it. – wjandrea Jan 4 '19 at 14:11
  • 1
    The fish shell (fish in the Ubuntu repositories) does this by default. – Nonny Moose Jan 5 '19 at 2:46
3

There are some options.

1. Fuzzy finder

A utility, fuzzy finder, will do exactly what you want. Fuzzy finder is in the Ubuntu repositories (at least as of 19.04) and can be installed with

sudo apt get install fzf

Alternatively, it can be installed from its github page.

Once installed, at the prompt, press Alt+c. You immediatelly can start typing the name of a folder underneath the folder where you are. When the name appears, select it and press Enter to cd into it.

WHen typing a command, Ctrl+T will allow you to search and put any name of a folder or file on the command line.

2. Your bash history

  • Type Ctrl+r: this starts reverse search in your history
  • type pro (i.e. the actual name of your project): likely, you recent command cd /path/to/project will already have popped up.
  • Press Enter to execute the command

This will reduce your effort to four, five keystrokes. If another matching (more recent) command pops up, press Ctrl+r again to continue the reverse search.

Also typing just cd after Ctrl+r already will list the last cd command used. Continue pressing Ctrl+r to cycle through previous cd commands. However, the chance that your folder name is more unique and thus is found quicker, is higher.

3. Rofi or dmenu

With rofi or dmenu, you can easily create small menu's that dynamically represent your recent folders. rofi (or dmenu) takes a text file as input for menu items. The output of the command history | grep cd can be displayed to you through rofi as following:

history | grep cd | rofi -dmenu

Place this in a small script, and bind a hotkey to it. The hotkey will now display all your recent cd commands. Type a few letters of the directory to narrow down the list.

4. Other options

  • Make aliases to the commands you know you will be repeating
  • Make small scripts to run the commands you know will be repeating
  • More options: shell function, partial command history search – wjandrea Jan 4 '19 at 14:51
  • Yes, this is not bad. I know about ctrl+R but it assumes you already know the name of the directory you want to go to. I enjoyed not having to think too hard, and just seeing my most recently/commonly visited directories. Maybe there is no way. – LondonRob Jan 4 '19 at 15:29
  • I would assume you know part of the folder name if you frequently visit the folder using the terminal. Anyway, typing "cd" and searching backwards will cycle through all your recent cd command. history | grep cd will list them all. Again, you can put this in an alias. One step further is to script that as input for the rofi menu, and bind that script to a hotkey. With one hotkey, you would then list all your recent cd commands and be able to select and run one with arrow keys and Enter. – vanadium Jan 4 '19 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.