### Configure shell keybindings for dotbare Source: https://github.com/kazhala/dotbare/wiki/Tips-and-Tricks Set up terminal shortcuts to trigger dotbare commands. Includes examples for both Zsh and Bash environments. ```zsh bindkey -s '^g' "dotbare fedit"^j ``` ```bash bind -x '"\C-g":"dotbare fedit"' ``` -------------------------------- ### Install dotbare using Zsh plugin managers Source: https://github.com/kazhala/dotbare/blob/master/README.md Demonstrates how to install dotbare using popular Zsh plugin managers like zinit, oh-my-zsh, and antigen, or via manual cloning. ```zinit zinit light kazhala/dotbare ``` ```oh-my-zsh git clone https://github.com/kazhala/dotbare.git $HOME/.oh-my-zsh/custom/plugins/dotbare # Add 'dotbare' to the plugins array in ~/.zshrc ``` ```antigen antigen bundle kazhala/dotbare ``` ```manual git clone https://github.com/kazhala/dotbare.git ~/.dotbare # Add the following to ~/.zshrc: source ~/.dotbare/dotbare.plugin.zsh ``` -------------------------------- ### Install Dotbare Bash Completion Source: https://github.com/kazhala/dotbare/blob/master/README.md Clones the Dotbare repository and sources the bash completion script to enable command-line completion for git and dotbare in bash. This requires git to be installed. ```bash git clone https://github.com/kazhala/dotbare.git ~/.dotbare source ~/.dotbare/dotbare.plugin.bash ``` -------------------------------- ### Define custom Vim command for dotfiles Source: https://github.com/kazhala/dotbare/wiki/Tips-and-Tricks Create a custom Vim command using fzf.vim to list and select dotfiles for editing. This requires fzf.vim to be installed. ```vim command! Dots call fzf#run(fzf#wrap({ \ 'source': 'dotbare ls-files --full-name --directory "${DOTBARE_TREE}" | awk -v home="${DOTBARE_TREE}/" "{print home $0}"', \ 'sink': 'e', \ 'options': [ '--multi', '--preview', 'cat {}' ] \ })) ``` -------------------------------- ### Bash Script Setup for Dotbare Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This snippet shows the essential lines to include at the top of any bash script intended for use with dotbare. It sets up the script's directory path and sources a helper script to establish necessary environment variables like DOTBARE_DIR and DOTBARE_TREE. ```bash #!/usr/bin/env bash mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/../helper/set_variable.sh ``` -------------------------------- ### Stage files safely in dotbare Source: https://github.com/kazhala/dotbare/wiki/Tips-and-Tricks Recommended methods for staging files in dotbare, avoiding the dangerous 'add --all' command which can stage unwanted files. ```sh dotbare fadd dotbare add -u dotbare commit -am "message" ``` -------------------------------- ### Preview Files with Syntax Highlighting using Shell Script Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This script provides a preview of files with syntax highlighting if compatible packages like 'bat' or 'highlight' are installed. It's designed to be invoked within the fzf preview argument and takes a filename and optional line number as input. ```shell mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" find . -maxdepth 1 -type f | sed "s|\./||g" | fzf --multi --preview "${mydir}/preview.sh {}" ``` -------------------------------- ### Integrate Dotbare with Existing Symlink Setup Source: https://github.com/kazhala/dotbare/blob/master/README.md Sets environment variables DOTBARE_DIR and DOTBARE_TREE to integrate Dotbare with an existing symlink or GNU stow setup. This allows Dotbare to recognize and manage dotfiles managed by other tools without a full migration. ```bash # e.g. I have all my dotfiles stored in folder $HOME/.myworld and symlinks all of them to appropriate location. # export DOTBARE_DIR="$HOME/.myworld/.git" # export DOTBARE_TREE="$HOME/.myworld" export DOTBARE_DIR= export DOTBARE_TREE= ``` -------------------------------- ### Install Tree Dependency on macOS Source: https://github.com/kazhala/dotbare/blob/master/README.md Installs the 'tree' package using Homebrew on macOS. This is an optional dependency for Dotbare that provides a directory tree view. ```bash # if you are on macos brew install tree ``` -------------------------------- ### Get Modified Files using fzf with Customization Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts The `get_modified_file` function lists modified files in fzf, showing file changes in the preview. It supports filtering by modification status (all, staged, unstaged) and output format (name or raw with status). Multi-select is enabled by default. The example shows how to capture multiple selected file names into a bash array. ```bash selected_files=() while IFS= read -r line; do selected_files+=("${line}") done < <(get_modified_file "select files to stage" "unstaged" "name") ``` -------------------------------- ### Get Selected Git File Path using fzf Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This function, `get_git_file`, presents all tracked dotfiles in fzf with file previews. It returns the selected file path. Arguments control the display format (full path or raw name) and whether multi-select is enabled. By default, it returns the full path and enables multi-select. ```bash selected_files=$(get_git_file "select files to backup" "raw") ``` -------------------------------- ### Alias dotbare command Source: https://github.com/kazhala/dotbare/wiki/Tips-and-Tricks Create a shorthand alias for the dotbare command to improve typing efficiency in the terminal. ```sh alias dots=dotbare ``` -------------------------------- ### Get Selected Commit Hash using fzf Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This function, `get_commit`, lists all git commits through fzf, displaying commit information in the preview window. It returns the 7-character hash of the selected commit. An optional argument can be provided to customize the header message displayed in fzf. ```bash selected_commit=$(get_commit "select a commit to foo boo") ``` -------------------------------- ### Exclude dotbare directory from git Source: https://github.com/kazhala/dotbare/wiki/Tips-and-Tricks Add the dotbare directory to the global .gitignore to prevent recursion issues. The path must be relative to the DOTBARE_TREE. ```sh echo ".cfg" >> $HOME/.gitignore echo ".config/.cfg" >> $HOME/.gitignore ``` -------------------------------- ### Get Selected Branch Name using fzf Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts The `get_branch` function allows users to select a git branch via fzf, with git log information displayed in the preview. It returns the name of the chosen branch. An optional argument can be used to set the fzf header message. ```bash selected_branch=$(get_branch 'select a branch to checkout') ``` -------------------------------- ### Get Selected Stash Identifier using fzf Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This function, `get_stash`, lists all stashes and displays their diff against HEAD in the fzf preview window. It returns the identifier of the selected stash (e.g., `stash@{0}`). An optional argument can be provided to customize the fzf header message. ```bash selected_stash=$(get_stash "select stash to delete") ``` -------------------------------- ### Delete Git Branch via fzf Selection using Bash Script Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This Bash script facilitates the deletion of Git branches, including remote branches, by first selecting the branch using fzf. It relies on helper scripts for setting variables and querying Git information. It requires Git to be installed and configured. ```shell #!/usr/bin/env bash mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/../helper/set_variable.sh source "${mydir}"/../helper/git_query.sh selected_branch=$(get_branch 'select a branch to delete') [[ -z "${selected_branch}" ]] && exit 1 remote_branch_regex="^remotes\/.*$" if [[ "${selected_branch}" =~ $remote_branch_regex ]]; then read -r remote_name origin_name branch_name <<< $( echo "${selected_branch}" \ | awk -F "/" '{ remote=$1 origin=$2 sub($1 FS $2 FS, "") print remote " " origin " " $0 }' ) git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" push -d "${origin_name}" "${branch_name}" else git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" branch -D "${selected_branch}" fi ``` -------------------------------- ### Initialize Dotbare Bare Repository Source: https://github.com/kazhala/dotbare/blob/master/README.md Initializes a new bare repository for managing dotfiles using the 'dotbare finit' command. By default, it sets up the repository in $HOME/.cfg. ```bash dotbare finit ``` -------------------------------- ### Complete Migration to Dotbare Bare Repository Source: https://github.com/kazhala/dotbare/blob/master/README.md Performs a complete migration to a Dotbare bare repository by initializing it with a remote URL. This command replaces existing symlinks with the original files and creates a bare repository. Use with caution and test thoroughly. ```bash # dotbare will replace all symlinks with the original file and a bare repository will be created at $DOTBARE_DIR dotbare finit -u [URL] ``` -------------------------------- ### Sourcing Git Query Helper Functions Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This code demonstrates how to source the `git_query.sh` helper file within a bash script. This makes all the git information querying functions available for use in the script, which are utilized by dotbare for fzf integration. ```bash source "${mydir}"/../helper/git_query.sh ``` -------------------------------- ### Search Files and Directories using Shell Script Source: https://github.com/kazhala/dotbare/wiki/Custom-Scripts This script searches for files or directories within the current directory up to one level deep. It requires sourcing a helper script and takes a single argument ('f' for file, 'd' for directory). It outputs the selected file path. ```shell source "${mydir}"/../helper/search_file.sh selected_files= () while IFS= read -r line; do selected_files+=("${line}") done < <(search_file 'f') ``` -------------------------------- ### Configure Dotbare for Existing Git Bare Repository Source: https://github.com/kazhala/dotbare/blob/master/README.md Sets environment variables DOTBARE_DIR and DOTBARE_TREE to inform Dotbare about the location of an existing git bare repository and the directory containing dotfiles. This is used for migrating from a normal git bare repository. ```bash export DOTBARE_DIR="$HOME/.cfg" export DOTBARE_TREE="$HOME" ``` -------------------------------- ### Configure Gitignore for Scripts Folder (Shell) Source: https://github.com/kazhala/dotbare/blob/master/CONTRIBUTING.md Shows how to configure the `.gitignore` file to ignore all files within the `scripts/` directory except for specific essential scripts like `fadd`, `fbackup`, and user-defined new scripts. This ensures that only intended scripts are tracked by Git. ```shell # ignore everything in scripts folder except the !scripts/fadd etc scripts/* !scripts/fadd !scripts/fbackup .... !scripts/your new script ``` -------------------------------- ### Improve Help Message Output Efficiency (Shell) Source: https://github.com/kazhala/dotbare/blob/master/CONTRIBUTING.md Demonstrates an optimization for displaying help messages in dotbare. Previously, messages were printed using multiple `echo -e` commands. This snippet shows the improved version where all messages are combined into a single `echo -e` command, reducing unnecessary invocations. ```shell # Before echo -e "usage: ...\n" echo -e "foo boo\n" echo -e "Optional arguments: ...\n" # After echo -e "usage: ... foo boo Optional arguments: ..." ``` -------------------------------- ### Enable Dotbare Command Completion Source: https://github.com/kazhala/dotbare/wiki/Completion Activates completion for dotbare subcommands. In Zsh, this requires compinit to be loaded first, while in Bash, it accepts an optional argument to support aliased command names. ```zsh autoload -U compinit && compinit _dotbare_completion_cmd ``` ```bash _dotbare_completion_cmd # For aliased commands _dotbare_completion_cmd config ``` -------------------------------- ### Enable Git Command Completion Source: https://github.com/kazhala/dotbare/wiki/Completion Activates git-specific completion for dotbare. Note that this functionality is limited as the underlying git completion script is unaware of custom DOTBARE_DIR or DOTBARE_TREE variables. ```zsh _dotbare_completion_git ``` ```bash _dotbare_completion_git # For aliased commands _dotbare_completion_git config ``` -------------------------------- ### Add Dotfiles with Dotbare Source: https://github.com/kazhala/dotbare/blob/master/README.md Adds dotfiles or directories to the Dotbare repository for tracking. It supports adding individual files or entire directories using flags like '-f' for files and '-d' for directories. ```bash dotbare fadd -f # or dotbare add [FILENAME] # add entire repository like .config directory dotbare fadd -d # or dotbare add [DIRECTORY] ``` -------------------------------- ### Add Dotbare to PATH and Create Alias Source: https://github.com/kazhala/dotbare/blob/master/README.md Adds the dotbare executable directory to the system's PATH for easy access and optionally creates an alias for the 'dotbare' command. This is for POSIX-compliant shells. ```bash # This is only an example command for posix shell # If you are on fish, use the fish way to add dotbare to your path export PATH=$PATH:$HOME/.dotbare alias dotbare="$HOME/.dotbare/dotbare" ``` -------------------------------- ### Mocking fzf invocation in Bats tests Source: https://github.com/kazhala/dotbare/blob/master/tests/README.md This snippet demonstrates how to mock the 'fzf' command within a bats test. By exporting the test directory to the PATH, the test can execute a local mock 'fzf' instead of the system-wide one. This allows for asserting that 'fzf' is called with the correct arguments, such as '--multi --preview' and 'tree -L 1 -C --dirsfirst {}'. ```shell #!/usr/bin/env bats stage_selected_dir() { export PATH="${BATS_TEST_DIRNAME}:$PATH" bash "${BATS_TEST_DIRNAME}"'/../dotbare' fadd --dir } ``` ```shell if [[ "$*" =~ '--multi --preview ' ]] && [[ "$*" =~ "tree -L 1 -C --dirsfirst {}" ]]; then # dotbare fadd --dir -- "./fadd.bats" @test "fadd stage selected dir" echo "fadd_stage_dir" fi ``` ```shell #!/usr/bin/env bats @test "fadd stage selected dir" { run stage_selected_dir # we could assert if fzf is actually being invoked correctly # in this case, it's supposed to search directory and using tree to provide preview [[ "${output}" =~ "add fadd_stage_dir" ]] } ``` -------------------------------- ### Bats unit tests for 'flog help' and 'flog reset' Source: https://github.com/kazhala/dotbare/blob/master/tests/README.md This code provides bats unit tests for the 'flog help' and 'flog reset' commands of the dotbare project. The 'flog reset' test utilizes the PATH override method to mock 'fzf' interactions, asserting that the output contains 'reset --flog_reset'. The 'flog help' test simply checks for a successful exit status and verifies the first line of the output. ```shell #!/usr/bin/env bats help() { bash "${BATS_TEST_DIRNAME}"'/../dotbare' flog -h } reset() { export PATH="${BATS_TEST_DIRNAME}:$PATH" bash "${BATS_TEST_DIRNAME}"'/../dotbare' flog --reset -y } @test "flog help" { run help [ "${status}" -eq 0 ] [ "${lines[0]}" = "Usage: dotbare flog [-h] [-r] [-R] [-e] [-c] [-y] ..." ] } @test "flog reset" { run reset [[ "${output}" =~ "reset --flog_reset" ]] } ``` -------------------------------- ### Mocking fzf for 'flog reset' in Bats tests Source: https://github.com/kazhala/dotbare/blob/master/tests/README.md This snippet shows how to mock the 'fzf' command for the 'flog --reset' functionality in bats tests. Similar to other mocks, it involves exporting the test directory to the PATH. The mock 'fzf' executable checks for specific arguments like '--header=select a commit' and 'show --color' to simulate the correct invocation and returns a specific string '--flog_reset'. ```shell if [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then # dotbare flog --reset -y -- "./flog.bats" @test "flog reset" echo "--flog_reset" fi ``` -------------------------------- ### Commit and Push Dotfiles with Dotbare Source: https://github.com/kazhala/dotbare/blob/master/README.md Commits the staged dotfile changes and pushes them to a remote repository. This involves setting up a remote origin and performing the push operation. ```bash dotbare commit -m "First commit" dotbare remote add origin [URL] dotbare push -u origin master ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.