Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autojump recommendations? #149

Closed
matschaffer opened this issue Oct 28, 2021 · 4 comments
Closed

Autojump recommendations? #149

matschaffer opened this issue Oct 28, 2021 · 4 comments

Comments

@matschaffer
Copy link
Contributor

I noticed some comments like #35 (comment) that seem to point to something like https://github.com/rupa/z or https://github.com/wting/autojump built into z4h but it's not quite clear what that is.

I'm accustomed to using j <first few chars> to navigate to common locations.

I noticed z4h bindkey z4h-cd-down Shift+Down in my config so tried shift+down but it seems to just complete based on an directory below the current cwd.

For now I just brew installed autojump and added z4h source "${HOMEBREW_PREFIX}/etc/profile.d/autojump.sh" to my ~/.zshrc.

Thanks in advance for the recommendations and really nice work on the setup!

@romkatv
Copy link
Owner

romkatv commented Oct 28, 2021

zsh4humans stores persistent directory history. It gets loaded into the builtin dirstack when you start zsh. Try opening a new terminal and typing cd -<TAB> -- you'll see the history. You can also hit Shift+Left to go back in dirstack. This is useful if you want to create a new terminal tab and cd into the last directory you've visited, or to go back after a cd. Shift+Left/Shift+Right work like Back/Forward buttons in a web browser.

Shift+Up goes to the parent directory and Shift+Down goes to a subdirectory. Since there are many subdirectories, Shift+Down asks you to choose.

There is also Alt+R for fzf over directory history. This is the closes thing to autojump and similar tools.

In my zshrc configure things a bit differently:

zstyle ':z4h:fzf-dir-history' fzf-bindings tab:repeat
zstyle ':z4h:fzf-complete'    fzf-bindings tab:repeat
zstyle ':z4h:cd-down'         fzf-bindings tab:repeat

z4h bindkey z4h-fzf-dir-history Shift+Down

This rebinds Shift+Down to z4h-fzf-dir-history -- the widget that you can invoke via Alt+R by default. You'll no longer have a binding for z4h-cd-down but that's OK because you can get the same behavior with Shift+Down Tab.

The three zstyle lines rebind Tab in three fzf-based widgets from the default up to repeat. The latter causes the selection to get accepted (like pressing Enter) and immediately opens fzf once again. When you invoke z4h-fzf-dir-history, the first entry is always the current directory, so repeat on that will repopulate fzf with subdirectories of the current directory. Just like z4h-cd-down. You can press Tab on other entries, too, if you need to go into their subdirectories. It's also useful when TAB-completing file arguments. Instead of waiting in fzf for all files and directories to be traversed, you can accept a directory with Tab to narrow down the search.

Note that you can undo and redo completions the same way as any other command line changes. You can find their bindings in your ~/.zshrc. Some people prefer to rebind them to something else. I often use Tab to expand and verify globs and undo the expansion before executing the command. E.g., I could type rm **/*.orig, press Tab to expand the glob, check that it looks good, press Ctrl+/ to undo the expansion and execute the command. I execute commands with glob arguments in order to have them this way in history. This allows me to re-execute them even when the set of **/*.orig files changes.

P.S.

Unrelated but useful. Add this to ~/.zshrc. This makes transient prompt work properly when closing ssh connections. It'll become the default in v6.

z4h bindkey z4h-eof Ctrl+D
setopt ignore_eof

Also install bfs on your machine (it's packaged for all major operating systems). zsh4humans will use it instead of find when traversing directories. This improves ranking of files in fzf and it's faster. You don't need to configure this, bfs will be picked up automatically.

@matschaffer
Copy link
Contributor Author

zsh4humans stores persistent directory history. It gets loaded into the builtin dirstack when you start zsh. Try opening a new terminal and typing cd -<TAB> -- you'll see the history. You can also hit Shift+Left to go back in dirstack. This is useful if you want to create a new terminal tab and cd into the last directory you've visited, or to go back after a cd. Shift+Left/Shift+Right work like Back/Forward buttons in a web browser.

Shift+Up goes to the parent directory and Shift+Down goes to a subdirectory. Since there are many subdirectories, Shift+Down asks you to choose.

Sweet! This was clear enough from seeing the bindings in my ~/.zshrc and playing with them a bit :)

There is also Alt+R for fzf over directory history. This is the closes thing to autojump and similar tools.

THAT'S what I was looking for. I'll try to work Alt+R into my muscle memory :) Nice choice of keys since it's similar in nature to Ctrl+R.

In my config I configure things a bit differently:

zstyle ':z4h:fzf-dir-history' fzf-bindings tab:repeat
zstyle ':z4h:fzf-complete'    fzf-bindings tab:repeat
zstyle ':z4h:cd-down'         fzf-bindings tab:repeat

z4h bindkey z4h-fzf-dir-history Shift+Down

This rebinds Shift+Down to z4h-fzf-dir-history -- the widget that you can invoke via Alt+R by default. You'll no longer have a binding for z4h-cd-down but that's OK because you can get the same behavior with Shift+Down Tab.

The three zstyle lines rebind Tab in three fzf-based widgets from the default up to repeat. The latter causes the selection to get accepted (like pressing Enter) and immediately opens fzf once again. When you invoke z4h-fzf-dir-history, the first entry is always the current directory, so repeat on that will repopulate fzf with subdirectories of the current directory. Just like z4h-cd-down. You can press Tab on other entries, too, if you need to go into their subdirectories. It's also useful when TAB-completing file arguments. Instead of waiting in fzf for all files and directories to be traversed, you can accept a directory with Tab to narrow down the search.

This is cool. How do you get back if you tab on the wrong item though? My instinct was to hit "shift tab" but it didn't work.

Note that you can undo and redo completions the same way as any other command line changes. You can find their bindings in your ~/.zshrc. Some people prefer to rebind them to something else. I often use Tab to expand and verify globs and undo the expansion before executing the command. E.g., I could type rm **/*.orig, press Tab to expand the glob, check that it looks good, press Ctrl+/ to undo the expansion and execute the command. I execute commands with glob arguments in order to have them this way in history. This allows me to re-execute them even when the set of **/*.orig files changes.

Nice point :)

Unrelated but useful. Add this to ~/.zshrc. This makes transient prompt work properly when closing ssh connections. It'll become the default in v6.

z4h bindkey z4h-eof Ctrl+D
setopt ignore_eof

Very cool. I don't use transient prompt though since I like to have the previous timestamps available in my backscroll. I guess iTerm2 could also do this for me, but seems easier to just have them there ready for copy/paste when needed.

Also install bfs on your machine (it's packaged for all major operating systems). zsh4humans will use it instead of find when traversing directories. This improves ranking of files in fzf and it's faster. You don't need to configure this, bfs will be picked up automatically.

Nice! I grabbed that from brew install tavianator/tap/bfs

@romkatv
Copy link
Owner

romkatv commented Oct 29, 2021

This is cool. How do you get back if you tab on the wrong item though? My instinct was to hit "shift tab" but it didn't work.

Logically, Shift+Left should do that, although Shift+Tab also makes sense. None of these currently work though. @maximbaz has been asking for this feature for a long time but I never gotten around to implementing it. Now you have to close fzf -- Esc or Ctrl+G (the latter is faster) -- and hit Shift+Left.

Very cool. I don't use transient prompt though since I like to have the previous timestamps available in my backscroll.

Try changing ~/.p10k.zsh like this:

@@ -37,6 +37,7 @@
     vcs                     # git status
     # =========================[ Line #2 ]=========================
     newline                 # \n
+    time                    # time when the command started executing (only shown in past prompts)
     prompt_char             # prompt symbol
   )
 
@@ -1541,7 +1542,7 @@
   # If set to true, time will update when you hit enter. This way prompts for the past
   # commands will contain the start times of their commands as opposed to the default
   # behavior where they contain the end times of their preceding commands.
-  typeset -g POWERLEVEL9K_TIME_UPDATE_ON_COMMAND=false
+  typeset -g POWERLEVEL9K_TIME_UPDATE_ON_COMMAND=true
   # Custom icon.
   # typeset -g POWERLEVEL9K_TIME_VISUAL_IDENTIFIER_EXPANSION='⭐'
   # Custom prefix.
@@ -1588,6 +1589,9 @@
   #               typed after changing current working directory.
   typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=off
 
+  function p10k-on-post-prompt() { p10k display '1|empty_line'=hide '2/left/time'=show }
+  function p10k-on-pre-prompt()  { p10k display '1|empty_line'=show '2/left/time'=hide }
+
   # Instant prompt mode.
   #
   #   - off:     Disable instant prompt. Choose this if you've tried instant prompt and found

If you want past commands separated by empty lines, remove |empty_line from p10k-on-post-prompt and p10k-on-pre-prompt. See p10k help display for syntax.

You can also have time in past prompts on the right instead of on the left. You can probably figure out how to achieve this. If not, ask.

@romkatv
Copy link
Owner

romkatv commented Nov 2, 2021

Closing because I believe I've answered all questions here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants