Skip to content

Instantly share code, notes, and snippets.

@kyokley
Last active September 24, 2022 15:22
Show Gist options
  • Save kyokley/92703c1367abfbd86b1b575be004a688 to your computer and use it in GitHub Desktop.
Save kyokley/92703c1367abfbd86b1b575be004a688 to your computer and use it in GitHub Desktop.
Bash Docker VIM: Quick bash functions to support using a dockerized Vim
# Append these functions to bashrc or zshenv to get quick acccess to a dockerized neovim
# For example, using curl:
# curl https://gist.githubusercontent.com/kyokley/92703c1367abfbd86b1b575be004a688/raw/65a910d066c332fdc3e428204e6138253bec2b17/dvim.sh >> ~/.bashrc
function git-absolute-path () {
fullpath=$([[ $1 = /* ]] && echo "$1" || echo "$(pwd -P)/${1#./}")
if [ $(git rev-parse --show-superproject-working-tree) ]
then
gitroot="$(git rev-parse --show-superproject-working-tree)"
else
gitroot="$(git rev-parse --show-toplevel)" || return 1
fi
[[ "$fullpath" =~ "$gitroot" ]] && echo "${fullpath/$gitroot\//}"
}
function dvim() {
ROOT_HOME="/root"
GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ $? -eq 0 ]
then
TITLE_ROOT="$GIT_ROOT"
if [ $(git rev-parse --show-superproject-working-tree 2>/dev/null) ]
then
GIT_ROOT="$(git rev-parse --show-superproject-working-tree)"
fi
docker run \
--rm -it \
-e DISPLAY=unix$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v "$GIT_ROOT":/files \
-v "$HOME"/.vimbackup:"$ROOT_HOME"/.vimbackup \
-v "$HOME"/.vimswap:"$ROOT_HOME"/.vimswap \
-v "$HOME"/.vimundodir:"$ROOT_HOME"/.vimundodir \
-v "$HOME"/.vimviews:"$ROOT_HOME"/.vimviews \
-v "$HOME"/.local/share/nvim/shada:"$ROOT_HOME"/.shada \
kyokley/neovim-custom \
-i "$ROOT_HOME"/.shada/dvim_main.shada \
--cmd "let g:git_root = \"${TITLE_ROOT}\"" \
$(
for file in $@
do
echo $(git-absolute-path "$file")
done
)
else
docker run \
--rm -it \
-e DISPLAY=unix$DISPLAY \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v $(pwd):/files \
kyokley/neovim-custom \
"$@"
fi
}
@kyokley
Copy link
Author

kyokley commented Jun 24, 2022

I created a dockerized version of my neovim which gave me an easy way to bring my config with me to any computer I used. However, the docker commands necessary to mount an entire project directory into the container or have clipboard access were very messy so I'm placing them here in a form that can easily be curl'd straight into a bashrc or zshenv.

Some things I was hoping to achieve with these functions:

  • Mount the current directory or entire git project inside the container
  • Persistent undo and backups
  • Shared clipboard with host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment