Nushell + fzf — functions for git switch and git branch -d

I’ve recently re-discovered nushell and I’ve fallen hard. I’m committed. I’m stumbling my way through things, of course, but I’ve set my course and am not turning back.

Perhaps one day I’ll blog my reasons, but this post is about two of my most-used zsh functions:

  • gbs : a function to git switch using fzf
  • gbd : a function to git branch -d using fzf

I’ve ported them to nushell. Here’s the source, without comment or explanation:

def gbs [] {
  let branch = (
    git branch |
    split row "\n" |
    str trim |
    where ($it !~ '\*') |
    where ($it != '') |
    str join (char nl) |
    fzf --no-multi
  )
  if $branch != '' {
    git switch $branch
  }
}

def gbd [] {
  let branches = (
    git branch |
    split row "\n" |
    str trim |
    where ($it !~ '\*') |
    where ($it != '') |
    str join (char nl) |
    fzf --multi |
    split row "\n" |
    where ($it != '')
  )
  if ($branches | length) > 0 {
    $branches | each { |branch| git branch -d $branch }
    ""
  }
}

I put them in my config.nu. I welcome any feedback.

Edit: tested with nushell 0.78.1, built from source.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.