Skip to main content

When you click to a file or folder in graphical OS they will be opened in associated app. The xontrib-onepath brings the same logic to the xonsh shell. Type the filename or path without preceding command and an associated action will be executed. The actions are customizable.

If you like the idea click ⭐ on the repo and tweet.

Install

xpip install -U xontrib-onepath
echo 'xontrib load onepath' >> ~/.xonshrc
# Reload xonsh

Examples

# Run:                 # Action that will be executed:                       
/                      # cd /
executable_script      # ./executable_script
.xonshrc               # vim ~/.xonshrc
~/Downloads/logo.png   # xdg-open ~/Downloads/logo.png
git                    # git
./git                  # cd ./git

Default actions

If typed command is a registered name (i.e. git) and which command returns the path (i.e. /usr/bin/git) it will be executed. In other case the typed path will be used to make action. Default actions:

Priority Type Action
1 <DIR> cd
2 <XFILE> <RUN>
3 text/ vim
4 image/ xdg-open

Help to add more types and best default actions. PRs are welcome!

You can add platform and tool depend actions:

import shutil as _shutil
from xonsh.platform import ON_LINUX #, ON_DARWIN, ON_WINDOWS, ON_WSL, IN_APPIMAGE, ON_CYGWIN, ON_MSYS, ON_POSIX, ON_FREEBSD, ON_DRAGONFLY, ON_NETBSD, ON_OPENBSD

xontrib load onepath

if ON_LINUX and _shutil.which('nvim'):
    $XONTRIB_ONEPATH_ACTIONS['text/'] = 'nvim'

File types

# Type Example
1 Full path to file. ~/.xonshrc
2 File name. file.txt
3 File extension. *.txt
4 MIME type/subtype and extension. text/plain.txt
5 MIME type/subtype. text/plain
6 MIME type. text/
7 Any file. <FILE> (constant)
8 Any executable file. <XFILE> (constant)
9 Any directory. <DIR> (constant)
10 Any file or directory. * (constant)

To get MIME type for the file run file --mime-type --brief <file>.

Examples of actions

Use XONTRIB_ONEPATH_ACTIONS environment variable to add new actions. If you need more complex actions use callable xonsh aliases.

Simple actions

$XONTRIB_ONEPATH_ACTIONS['.xonshrc'] = 'vim'         # vim for `.xonshrc` file
$XONTRIB_ONEPATH_ACTIONS['*.log'] = 'tail'           # tail for text type *.log files
$XONTRIB_ONEPATH_ACTIONS['text/plain.txt'] = 'less'  # less for plain text *.txt files 
$XONTRIB_ONEPATH_ACTIONS['<DIR>'] = 'ls'             # list the files in the directory
$XONTRIB_ONEPATH_ACTIONS['application/zip'] = 'als'  # list files in zip file using atool

Run xsh regardless the execution permissions

$XONTRIB_ONEPATH_ACTIONS = {'*.xsh':'xonsh', **$XONTRIB_ONEPATH_ACTIONS}  # Insert on top
xontrib load onepath
script.xsh                            # the same as:  chmod +x script.xsh && ./script.xsh

View CSV tables using pandas

import pandas as pd
def _view_csv_with_pandas(args):
    print(pd.read_csv(args[0]))

aliases['view_csv_with_pandas'] = _view_csv_with_pandas
del _view_csv_with_pandas

$XONTRIB_ONEPATH_ACTIONS['application/csv'] = 'view_csv_with_pandas'

cd & ls

def _cdls(args):
     cd @(args[0])
     if int($(ls | wc -l).strip()) < 100:
         ls --group-directories-first -a --color
aliases['cdls'] = _cdls
del _cdls

$XONTRIB_ONEPATH_ACTIONS['<DIR>'] = 'cdls'

Known issues

ImportError: failed to find libmagic

Install libmagic: brew install libmagic.

NTFS in Linux: all files have execute permission

If you mount NTFS partition with default permissions then all files will have execute permission and onepath will execute them instead of action. The right way is to change default /etc/fstab settings. Example:

sudo umount /d
sudo mount -o uid=1000,gid=1000,dmask=027,fmask=137 /d 
ls -la /d
  • This package is the part of ergopack - the pack of ergonomic xontribs.
  • This package was created with xontrib template.

Metadata

Release files for xontrib-onepath 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xontrib-onepath 0.4.0
File Size Uploaded Provenance Actions
xontrib-onepath-0.4.0.tar.gz 5.4 kB Without provenance Details for xontrib-onepath-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xontrib-onepath 0.4.0
File Interpreter ABI Platform Actions
xontrib_onepath-0.4.0-py3-none-any.whl Python 3 none any Details for xontrib_onepath-0.4.0-py3-none-any.whl

Total release size: 11.0 kB

Release files / xontrib-onepath-0.4.0.tar.gz

Download URL xontrib-onepath-0.4.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
5022ea6283e72de1243113f0977e8e481e425752a4aef00ec946727f956500c1
BLAKE2b-256 checksum
How to use checksums
7be4e2cef2b67db8124397a951c4113c2033e7f54148b2e72fb366e5c5513839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / xontrib_onepath-0.4.0-py3-none-any.whl

Download URL xontrib_onepath-0.4.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ae854b8641da06b7f0e0f2f08b1e4bb1a116c9d955d2fa8e5d4c6aa1293619ce
BLAKE2b-256 checksum
How to use checksums
225a732556bd5f71a9dfa5a22a34c406be523bb396a558931f3bd0caf9c687ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Security for xontrib-onepath 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page