├── .editorconfig ├── .gitattributes ├── .github ├── FUNDING.yml ├── issue_template.md ├── pull_request_template.md ├── release-drafter.yml └── workflows │ ├── python-publish.yml │ └── test.yml ├── .gitignore ├── .pre-commit-config.yaml ├── LICENSE ├── README.md ├── pyproject.toml ├── requirements.txt ├── setup.py ├── tests ├── __init__.py └── test_xontrib.py └── xontrib └── jump_to_dir.py /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # this is the top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | end_of_line = lf 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | indent_style = space 12 | indent_size = 4 13 | 14 | [*.{yml,json}] 15 | indent_size = 2 16 | 17 | [*.{md,markdown}] 18 | # don't trim trailing spaces because markdown syntax allows that 19 | trim_trailing_whitespace = false 20 | 21 | [{makefile,Makefile,MAKEFILE}] 22 | indent_style = tab 23 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.xsh text linguist-language=Python 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | 2 | # These are supported funding model platforms 3 | 4 | #github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 5 | #patreon: xonssh # Replace with a single Patreon username 6 | #open_collective: # Replace with a single Open Collective username 7 | #ko_fi: # Replace with a single Ko-fi username 8 | #tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 9 | #community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 10 | #liberapay: # Replace with a single Liberapay username 11 | #issuehunt: # Replace with a single IssueHunt username 12 | #otechie: # Replace with a single Otechie username 13 | custom: ['https://github.com/anki-code', 'https://www.buymeacoffee.com/xxh', 'https://github.com/xonsh/xonsh#the-xonsh-shell-community'] 14 | 15 | -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | Hi! Thank you for the awesome xontrib! I gave it a star! 2 | 3 | I've found that ... 4 | 5 | ## For community 6 | ⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍 comment** 7 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | Hi! Thank you for the awesome xontrib! I gave it a star! 2 | 3 | Please take a look at my PR that ... 4 | 5 | ## For community 6 | ⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍 comment** 7 | -------------------------------------------------------------------------------- /.github/release-drafter.yml: -------------------------------------------------------------------------------- 1 | # Release drafter configuration https://github.com/release-drafter/release-drafter#configuration 2 | # Emojis were chosen to match the https://gitmoji.carloscuesta.me/ 3 | 4 | name-template: "v$NEXT_PATCH_VERSION" 5 | tag-template: "v$NEXT_PATCH_VERSION" 6 | 7 | categories: 8 | - title: ":rocket: Features" 9 | labels: [enhancement, feature] 10 | - title: ":wrench: Fixes & Refactoring" 11 | labels: [bug, refactoring, bugfix, fix] 12 | - title: ":package: Build System & CI/CD" 13 | labels: [build, ci, testing] 14 | - title: ":boom: Breaking Changes" 15 | labels: [breaking] 16 | - title: ":pencil: Documentation" 17 | labels: [documentation] 18 | - title: ":arrow_up: Dependencies updates" 19 | labels: [dependencies] 20 | 21 | template: | 22 | ## What’s Changed 23 | $CHANGES 24 | ## :busts_in_silhouette: List of contributors 25 | $CONTRIBUTORS 26 | -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will upload a Python Package using Twine when a release is created 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries 3 | 4 | # This workflow uses actions that are not certified by GitHub. 5 | # They are provided by a third-party and are governed by 6 | # separate terms of service, privacy policy, and support 7 | # documentation. 8 | 9 | name: Upload Python Package 10 | 11 | on: 12 | release: 13 | types: [published] 14 | 15 | permissions: 16 | contents: read 17 | 18 | jobs: 19 | deploy: 20 | 21 | runs-on: ubuntu-latest 22 | 23 | steps: 24 | - uses: actions/checkout@v3 25 | - name: Set up Python 26 | uses: actions/setup-python@v3 27 | with: 28 | python-version: '3.x' 29 | - name: Install dependencies 30 | run: | 31 | python -m pip install --upgrade pip 32 | pip install build 33 | - name: Build package 34 | run: python -m build 35 | - name: Publish package 36 | uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 37 | with: 38 | user: __token__ 39 | password: ${{ secrets.PYPI_API_TOKEN }} 40 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Testing 2 | 3 | on: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | testing: 9 | # reuse workflow definitions 10 | uses: xonsh/actions/.github/workflows/test-pip-xontrib.yml@main 11 | with: 12 | cache-dependency-path: pyproject.toml 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pycqa/isort 3 | rev: 5.12.0 4 | hooks: 5 | - id: isort 6 | args: [ 7 | "--filter-files", # skip files that are excluded in config file 8 | "--profile=black", 9 | "--skip=migrations" 10 | ] 11 | 12 | - repo: https://github.com/ambv/black 13 | rev: 22.1.0 14 | hooks: 15 | - id: black 16 | language_version: python3.9 17 | 18 | - repo: https://github.com/pre-commit/mirrors-mypy 19 | rev: "v0.941" # Use the sha / tag you want to point at 20 | hooks: 21 | - id: mypy 22 | args: [ "--allow-untyped-globals", "--ignore-missing-imports" ] 23 | additional_dependencies: [ types-all ] 24 | 25 | - repo: https://github.com/pre-commit/pre-commit-hooks 26 | rev: v4.1.0 27 | hooks: 28 | - id: trailing-whitespace 29 | - id: check-yaml 30 | - id: check-toml 31 | - id: check-merge-conflict 32 | - id: debug-statements 33 | - id: check-added-large-files 34 | 35 | - repo: https://github.com/pycqa/flake8 36 | rev: "4.0.1" # pick a git hash / tag to point to 37 | hooks: 38 | - id: flake8 39 | args: [ "--ignore=E501,W503,W504,E203,E251,E266,E401,E126,E124,C901" ] 40 | additional_dependencies: [ 41 | 'flake8-bugbear', 42 | ] 43 | 44 | - repo: https://github.com/asottile/pyupgrade 45 | rev: v2.31.1 46 | hooks: 47 | - id: pyupgrade 48 | args: 49 | - "--py37-plus" 50 | - repo: https://github.com/compilerla/conventional-pre-commit 51 | rev: v1.2.0 52 | hooks: 53 | - id: conventional-pre-commit 54 | stages: [commit-msg] 55 | args: [] # optional: list of Conventional Commits types to allow 56 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023, anki-code 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Jump to used before directory by part of the path in xonsh shell.
Lightweight zero-dependency implementation of autojump or zoxide projects functionality. 3 |

4 | 5 |

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

8 | 9 | ## Note 10 | 11 | This xontrib is using [xonsh sqlite history backend](https://xon.sh/tutorial_hist.html#sqlite-history-backend) to get statistics by directories you're used to run commands. 12 | 13 | ## Installation 14 | 15 | To install use pip: 16 | 17 | ```bash 18 | xpip install xontrib-jump-to-dir 19 | # OR: xpip install -U git+https://github.com/anki-code/xontrib-jump-to-dir 20 | ``` 21 | 22 | ## Usage 23 | 24 | Init: 25 | ```xsh 26 | # Check that you're using sqlite history in ~/.xonshrc 27 | $XONSH_HISTORY_BACKEND = 'sqlite' 28 | xontrib load jump_to_dir 29 | ``` 30 | Jump to directory by path: 31 | ```xsh 32 | mkdir -p /tmp/hello /tmp/world 33 | cd /tmp/hello 34 | echo 1 35 | echo 2 36 | echo 3 37 | cd /tmp/world 38 | echo 1 39 | cd / 40 | 41 | j # Jump to most frequent directory i.e. `/tmp/hello/` because 3 `echo` commands were executed. 42 | j wor # Jump to directory with `*wor*` in path i.e. `/tmp/world/`. 43 | j t he # Jump to directory with `*t*he*` in path i.e. `/tmp/hello/`. 44 | ``` 45 | Jump to directory by command: 46 | ```xsh 47 | cd /tmp 48 | echo 112233 49 | cd / 50 | jc 22 # Jump to the directory where `*22*` command executed i.e. `/tmp`. 51 | ``` 52 | 53 | Custom shortcut: 54 | ```xsh 55 | $XONTRIB_JUMP_TO_DIR_SHORTCUT = 'z' 56 | xontrib load jump_to_dir 57 | z tm # Jump to previous directory with `*tm*` in path e.g. `/tmp/` 58 | zc git commit #Jump to previous directory where `*git*commit*` command executed e.g. `/git/` 59 | ``` 60 | 61 | ## How it works 62 | 63 | The history database has the commands you run and the directory where you was. The xontrib sorts the directories from history database by count of executed commands and filter them by mask e.g. the `j tm` command will find the directories by mask `*tm*`. Then you jump into the existing directory with the highest number of executed commands or if you already there to the previous directory by statistics. So if you have no commands that were executed in `/example` directory please avoid expectation that you can jump to it by running `j ex`. 64 | 65 | If you want to add fallback functionality to jump to any directory by partial path in case of zero result in history database (e.g. `j o lo bi` will jump to `/opt/local/bin`) feel free to create PR. 66 | 67 | ## Environment variables 68 | 69 | * `XONTRIB_JUMP_TO_DIR_SHORTCUT` - shortcut string. Default `j`. 70 | * `XONTRIB_JUMP_TO_DIR_WARNING` - show warnings from xontrib. Default `True`. 71 | 72 | ## Credits 73 | 74 | This package was created with [xontrib template](https://github.com/xonsh/xontrib-template). 75 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | 2 | [project] 3 | name = "xontrib-jump-to-dir" 4 | version = "0.2.1" 5 | license = {file = "LICENSE"} 6 | description = "Jump to used before directory by part of the path. Lightweight zero-dependency implementation of autojump or zoxide projects functionality." 7 | classifiers = [ 8 | "Programming Language :: Python :: 3", 9 | "License :: OSI Approved :: MIT License", 10 | "Operating System :: OS Independent", 11 | "Topic :: System :: Shells", 12 | "Topic :: System :: System Shells", 13 | "Topic :: Terminals", 14 | ] 15 | requires-python = ">=3.8" 16 | dependencies = ["xonsh>=0.12.5"] 17 | authors = [ 18 | { name = "anki-code", email = "no@no.no" }, 19 | ] 20 | [project.readme] 21 | file = "README.md" 22 | content-type = "text/markdown" 23 | 24 | 25 | 26 | 27 | 28 | [project.urls] 29 | 30 | Homepage = "https://github.com/anki-code/xontrib-jump-to-dir" 31 | Documentation = "https://github.com/anki-code/xontrib-jump-to-dir/blob/master/README.md" 32 | Code = "https://github.com/anki-code/xontrib-jump-to-dir" 33 | "Issue tracker" = "https://github.com/anki-code/xontrib-jump-to-dir/issues" 34 | 35 | 36 | 37 | 38 | [project.optional-dependencies] 39 | dev = ["pytest>=7.0"] 40 | 41 | 42 | 43 | [build-system] 44 | requires = [ 45 | "setuptools>=62", 46 | "wheel", # for bdist package distribution 47 | ] 48 | build-backend = "setuptools.build_meta" 49 | [tool.setuptools] 50 | 51 | packages = ["xontrib"] 52 | package-dir = {xontrib = "xontrib"} 53 | 54 | platforms = ["any"] 55 | include-package-data = false 56 | [tool.setuptools.package-data] 57 | 58 | xontrib = ["*.xsh"] 59 | 60 | 61 | 62 | [tool.isort] 63 | profile = "black" 64 | 65 | [tool.black] 66 | include = '\.pyi?$' 67 | force-exclude = ''' 68 | /( 69 | \.git 70 | | \.hg 71 | | \.mypy_cache 72 | | \.pytest_cache 73 | | \.tox 74 | | \.vscode 75 | | \.venv 76 | | _build 77 | | buck-out 78 | | build 79 | | dist 80 | | disk-cache.sqlite3 81 | )/ 82 | ''' 83 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | xonsh 2 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | setup() 3 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anki-code/xontrib-jump-to-dir/5fa5c32ea71fafb27085ca0f54cc5d5acbe19e1d/tests/__init__.py -------------------------------------------------------------------------------- /tests/test_xontrib.py: -------------------------------------------------------------------------------- 1 | def test_it_loads(load_xontrib): 2 | load_xontrib("jump_to_dir") 3 | -------------------------------------------------------------------------------- /xontrib/jump_to_dir.py: -------------------------------------------------------------------------------- 1 | """Jump to used before directory by part of the path. Lightweight zero-dependency implementation of autojump or zoxide projects functionality. """ 2 | 3 | from xonsh.built_ins import XSH 4 | import functools 5 | 6 | _hist_backend = XSH.env.get('XONSH_HISTORY_BACKEND') 7 | 8 | if _hist_backend == 'sqlite': 9 | def _jump_to_dir(args, search_column='cwd', shortcut='j'): 10 | import sqlite3 as _sqlite3 11 | from pathlib import Path as _Path 12 | con = _sqlite3.connect(XSH.env.get('XONSH_HISTORY_FILE')) 13 | success = False 14 | try: 15 | cur = con.cursor() 16 | sql = f""" 17 | SELECT cwd FROM xonsh_history 18 | WHERE 19 | {search_column} LIKE ? 20 | AND cwd != ? 21 | AND inp NOT LIKE '{shortcut} %' 22 | AND inp NOT LIKE '{shortcut}c %' 23 | GROUP BY cwd ORDER BY count(*) DESC 24 | LIMIT 10""" 25 | for row in cur.execute(sql, (f"%{'%'.join(args) if args else ''}%", XSH.env.get('PWD'))): 26 | if _Path(row[0]).exists(): 27 | __xonsh__.subproc_captured_stdout(['cd', row[0]]) 28 | success = True 29 | break 30 | finally: 31 | con.close() 32 | 33 | return 0 if success else 1 34 | 35 | _sc = __xonsh__.env.get('XONTRIB_JUMP_TO_DIR_SHORTCUT', 'j') 36 | aliases[_sc] = lambda args: _jump_to_dir(args, search_column='cwd', shortcut=_sc) 37 | aliases[_sc+'c'] = lambda args: _jump_to_dir(args, search_column='inp', shortcut=_sc) 38 | 39 | elif __xonsh__.env.get('XONTRIB_JUMP_TO_DIR_WARNING', True): 40 | print(f"xontrib-jump-to-dir: You're using {_hist_backend} for history backend. It's not supported for jump.") 41 | print("xontrib-jump-to-dir: Feel free to contribute: https://github.com/anki-code/xontrib-jump-to-dir") 42 | --------------------------------------------------------------------------------