#nushell #aliases #bash #nu #convert #tree-sitter #file

app nu-alias-converter

Converts Bash aliases to Nushell

4 releases

0.1.3 Oct 17, 2024
0.1.2 Oct 17, 2024
0.1.1 Oct 17, 2024
0.1.0 Oct 17, 2024

#539 in Filesystem

Download history 260/week @ 2024-10-14 3/week @ 2024-10-21 4/week @ 2024-11-04

267 downloads per month

MIT/Apache and maybe CC-PDDC

50KB
588 lines

Nu alias converter

[!NOTE]
This project is for educational purposes. I aim to learn more about tree-sitter, parsing, and Rust.

Installation

cargo install nu-alias-converter

Usage

The main purpose of this tool is to convert Bash aliases to Nushell aliases. This can be done with this simple command:

nu-alias-converter .bash_aliases # will generate a bash-aliases.nu file in the same directory

but the best use case is to use it in the Nushell environment. This way, the file will be regenerated at the start of each shell session, so you will always be on sync with your Bash aliases.

Add this to the end of your env.nu file (find it by running $nu.env-path in Nushell):

# This command will be shorter in the future, I promise
nu-alias-converter ~/.bash_aliases -o $"($nu.default-config-dir)/bash-alises.nu"  | ignore

Now add this to your config.nu to source the generated aliases file (find the path with nu.config-path):

source bash_aliases.nu

This will make the bash aliases available in the Nushell environment.

Ignoring Aliases

Sometimes there are some aliases that you don't want to convert to Nushell, maybe because they are not valid or you don't want to use them in Nushell.

You can ignore aliases by adding them to a .aliasignore file in the root of your home directory or in the nushell config directory.

The file should contain all the aliases that should be ignored, one per line:

# ~/.aliasignore
ls
la
gst

This will not convert any of those aliases to nushell.

Another option is to ignore all the aliases that use a command, the syntax would be the same but with a bang (!) in front of the command name:

# ~/.aliasignore
la
gst
!ls
!htop

This will also ignore all the aliases that use ls and htop.

How?

The CLI app will be written in Rust, needs to be to use the nushell crates used for parsing. It will use treesitter to parse the bash script and get all the aliases.

The aliases will then be converted to the nushell format using the nu-parser crate. After converting, the aliases will be validated, if an alias is not valid it will be generated as a comment with the information of the parsing error. So the user can check it and fix it manually. It would be nice to auto generate the rust code from the content of the alias is the parsing fails but this is not a priority for now.

The converted aliases are written to a file. You can either generate them manually or use the Nushell environment. If using the environment method, the file will regenerate at the start of each shell session.

Unnecessary diagram:
Not that complex but I always wanted to try this out.

---
config:
  theme: dark
  look: classic
  layout: dagre
---
graph TD
  A[CLI App in Rust] --> K[Read .aliasignore file]
  A --> B[Parse Bash with Tree-sitter]
  K --> L[Should ignore alias?]
  B --> L
  L -->|No| C[Convert to Nushell format]
  L -->|Yes| M[Skip alias]
  C --> D[Validate Alias Syntax]
  D --> E[Write Valid Aliases to Nushell File]
  E --> F[Manual Method]
  E --> G[Nushell Environment Method]
  G --> H[Regenerate File at Each Shell Session]
  D --> I[Comment Out Invalid Aliases]
  I --> J[Include Parsing Error Information]

TODO:

Parsing Bash Aliases

  • Use tree-sitter to parse Bash and get all aliases
  • Handle expand_aliases and shopt -s expand_aliases
    • Implement recursive handling

Conversion to Nushell Format

  • Convert aliases to Nushell format
  • Validate alias content to ensure it is valid Nushell (with nu-parser)

File Handling

  • Write the converted aliases to a file
    • Write once
    • Use Nushell env.nu
  • Handle multiple files in a directory
  • Handle non-Bash script files (zsh, fish)
  • Add .alias_ignore file to skip certain aliases during conversion
    • Ignore aliases by name
    • Ignore aliases by command
    • Check for .alias_ignore in the current directory
    • Check for .alias_ignore in nu config directory
  • Handle cases when no aliases are found in the file
  • Handle empty files

Flags and Modes

  • Add --help flag
  • Add --no-comments flag
  • Handle *.nu files error
  • Add --output flag to specify the output file path and name
  • Handle missing files
  • Add debug mode

Performance

  • Use rayon to parallelize the conversion process
    • Improved from 25 seconds to 5 seconds with a 28888 line aliases file ✅

Dependencies

~62–96MB
~2M SLoC