├── README.md └── cd.kak /README.md: -------------------------------------------------------------------------------- 1 | # kakoune-cd 2 | 3 | [kakoune](http://kakoune.org) commands to change or print the working directory. 4 | 5 | ## Install 6 | 7 | Add `cd.kak` to your autoload dir: `~/.config/kak/autoload/`. 8 | 9 | Or via [plug.kak](https://github.com/andreyorst/plug.kak): 10 | 11 | ``` 12 | plug 'delapouite/kakoune-cd' %{ 13 | # Suggested mapping 14 | map global user c ': enter-user-mode cd' -docstring 'cd' 15 | # Suggested aliases 16 | alias global cdb change-directory-current-buffer 17 | alias global cdr change-directory-project-root 18 | alias global ecd edit-current-buffer-directory 19 | alias global pwd print-working-directory 20 | } 21 | ``` 22 | 23 | ## Usage 24 | 25 | This file provides 4 commands: 26 | 27 | - `change-directory-current-buffer`: cd to current buffer dir 28 | - `change-directory-project-root`: cd to root dir (location of`.git` dir in parent dirs) 29 | - `edit-current-buffer-directory`: open an edit prompt in the current buffer directory 30 | - `print-working-directory`: echo `$PWD` 31 | 32 | A `cd` mode is also available with a few predefined locations. 33 | Feel free to map your own bookmarks on it. 34 | 35 | ## See also 36 | 37 | - [kakoune-buffers](https://github.com/Delapouite/kakoune-buffers) 38 | - [kakoune-goto-file](https://github.com/Delapouite/kakoune-goto-file) 39 | - [kakoune-edit-or-dir](https://github.com/TeddyDD/kakoune-edit-or-dir) 40 | - [vcs.kak](https://github.com/lenormf/kakoune-extra/blob/master/vcs.kak) 41 | 42 | ## Licence 43 | 44 | MIT 45 | -------------------------------------------------------------------------------- /cd.kak: -------------------------------------------------------------------------------- 1 | define-command change-directory-current-buffer -docstring 'cd to current buffer dir' %{ 2 | evaluate-commands %sh{ 3 | buffer_dirname=$(dirname "$kak_bufname") 4 | echo "cd \"${buffer_dirname}\"" 5 | echo print-working-directory 6 | } 7 | } 8 | 9 | # only works for git now, use `hg root` for mercurial 10 | define-command change-directory-project-root -docstring 'cd to project root dir' %{ 11 | change-directory-current-buffer 12 | evaluate-commands %sh{ 13 | project_root=$(git rev-parse --show-toplevel) 14 | echo "cd \"${project_root}\"" 15 | echo print-working-directory 16 | } 17 | } 18 | 19 | define-command print-working-directory -docstring 'print working directory' %{ 20 | evaluate-commands %sh{ 21 | echo "echo -markup {Information}$PWD" 22 | } 23 | } 24 | 25 | declare-option -hidden str oldpwd 26 | 27 | define-command edit-current-buffer-directory -docstring 'edit in current buffer dir' %{ 28 | evaluate-commands %sh{ echo "set global oldpwd '$PWD'" } 29 | change-directory-current-buffer 30 | execute-keys :edit 31 | hook -group oldpwd global BufCreate .* %{ 32 | change-directory "%opt{oldpwd}" 33 | remove-hooks global oldpwd 34 | } 35 | # on cancelled edit prompt 36 | hook -group oldpwd global RawKey %{ 37 | change-directory "%opt{oldpwd}" 38 | remove-hooks global oldpwd 39 | } 40 | } 41 | 42 | declare-user-mode cd 43 | map global cd b ': change-directory-current-buffer' -docstring 'current buffer dir' 44 | map global cd c ': cd %val{config}; print-working-directory' -docstring 'config dir' 45 | map global cd e ': edit-current-buffer-directory' -docstring 'edit in current buffer dir' 46 | map global cd h ': cd; print-working-directory' -docstring 'home dir' 47 | map global cd p ': cd ..; print-working-directory' -docstring 'parent dir' 48 | map global cd r ': change-directory-project-root' -docstring 'project root dir' 49 | 50 | # Suggested aliases 51 | 52 | #alias global cdb change-directory-current-buffer 53 | #alias global cdr change-directory-project-root 54 | #alias global ecd edit-current-buffer-directory 55 | #alias global pwd print-working-directory 56 | --------------------------------------------------------------------------------