From 7f5c8c112a299afb3b7a40c1dad423ef58a701c8 Mon Sep 17 00:00:00 2001 From: Maciej Lebiest Date: Sun, 12 Mar 2023 13:19:38 +0100 Subject: [PATCH] add bashrc script --- README.md | 46 ++++++++++++++++++++++-------- bashrc_nvim.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 bashrc_nvim.sh diff --git a/README.md b/README.md index 15d072c..f3e16e4 100644 --- a/README.md +++ b/README.md @@ -59,28 +59,36 @@ podman run --privileged -it --rm \ -e XDG_RUNTIME_DIR=/runtime_dir \ -e WAYLAND_DISPLAY=$WAYLAND_DISPLAY \ -v $XDG_RUNTIME_DIR/$WAYLAND_DISPLAY:/runtime_dir/$WAYLAND_DISPLAY \ - - v ~/.local/share/nvim/sessions:/root/.local/share/nvim/sessions \ + -v ~/.local/share/nvim/sessions:/root/.local/share/nvim/sessions \ --workdir /data \ - -v "./:/data:rw" \ - neovim:latest + -v "$(pwd):/data:rw" \ + neovim:latest ``` function for opening current dir or some files/folders in temporary container: ```bash function nvim() { + # Mount current folder OR folders/files given as parameters, then + # open neovim. Container will be removed on neovim exit. + # Mount wayland for clipboard sync. + # Also pass all parameters to neovim as its arguments. + for arg in "$@"; do - if [ "$arg" ] && [ -f "$arg" -o -d "$arg" ] ; then - local MOUNT_FILE="$MOUNT_FILE -v "$arg:$arg:rw""; + if [ -f "$arg" ] || [ -d "$arg" ] ; then + local MOUNT_FILE="$MOUNT_FILE -v "$arg:$arg:rw"" echo "Mounting $arg" fi done if [ -z "$MOUNT_FILE" ]; then # mount current workdir if no arguments with path # mount on base_path to make sessions saving work - local base_path="$(pwd)" + local base_path + base_path="$(pwd)" local MOUNT_FOLDER="--workdir /data$base_path -v "$base_path:/data$base_path:rw"" fi + # make sure there is a folder for sessions on default path + mkdir -p ~/.local/share/nvim/sessions podman run --privileged -it --rm \ -e XDG_RUNTIME_DIR=/runtime_dir \ @@ -100,22 +108,36 @@ then use function below. ```bash function nvim_project() { + # Mount current folder to a container that will not be removed on exit. + # Requires first argument to be a name for the container so it can be + # easily reentered later. + # If you specify some paths as latter parameters, then these paths will + # be mounted instead of current folder. + # Also mounts wayland for clipboard sync. + if [ -z "$1" ]; then - echo "give project/container name as first parameter"; - return 1; + echo "give project/container name as first parameter" + return 1 fi + local container_name + container_name="$1" + shift # skip first parameter as it can be name of a folder/file in + # current dir so it could try mounting it later for arg in "$@"; do - if [ "$arg" ] && [ -f "$arg" -o -d "$arg" ] ; then - local MOUNT_FILE="$MOUNT_FILE -v "$arg:$arg:rw""; + if [ -f "$arg" ] || [ -d "$arg" ] ; then + local MOUNT_FILE="$MOUNT_FILE -v "$arg:$arg:rw"" echo "Mounting $arg" fi done if [ -z "$MOUNT_FILE" ]; then # mount current workdir if no arguments with path # mount on base_path to make sessions saving work - local base_path="$(pwd)" + local base_path + base_path="$(pwd)" local MOUNT_FOLDER="--workdir /data$base_path -v "$base_path:/data$base_path:rw"" fi + # make sure there is a folder for sessions on default path + mkdir -p ~/.local/share/nvim/sessions podman run --privileged -it \ -e XDG_RUNTIME_DIR=/runtime_dir \ @@ -125,7 +147,7 @@ function nvim_project() { $MOUNT_FILE \ $MOUNT_FOLDER \ --entrypoint bash \ - --name "$1" \ + --name $container_name \ neovim:latest } diff --git a/bashrc_nvim.sh b/bashrc_nvim.sh new file mode 100644 index 0000000..d76abc1 --- /dev/null +++ b/bashrc_nvim.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +function nvim() { + # Mount current folder OR folders/files given as parameters, then + # open neovim. Container will be removed on neovim exit. + # Mount wayland for clipboard sync. + # Also pass all parameters to neovim as its arguments. + + for arg in "$@"; do + if [ -f "$arg" ] || [ -d "$arg" ] ; then + local MOUNT_FILE="$MOUNT_FILE -v "$arg:$arg:rw"" + echo "Mounting $arg" + fi + done + if [ -z "$MOUNT_FILE" ]; then + # mount current workdir if no arguments with path + # mount on base_path to make sessions saving work + local base_path + base_path="$(pwd)" + local MOUNT_FOLDER="--workdir /data$base_path -v "$base_path:/data$base_path:rw"" + fi + # make sure there is a folder for sessions on default path + mkdir -p ~/.local/share/nvim/sessions + + podman run --privileged -it --rm \ + -e XDG_RUNTIME_DIR=/runtime_dir \ + -e WAYLAND_DISPLAY="$WAYLAND_DISPLAY" \ + -v "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY:/runtime_dir/$WAYLAND_DISPLAY:rw" \ + -v ~/.local/share/nvim/sessions:/root/.local/share/nvim/sessions:rw \ + $MOUNT_FILE \ + $MOUNT_FOLDER \ + neovim:latest "$@" +} + +function nvim_project() { + # Mount current folder to a container that will not be removed on exit. + # Requires first argument to be a name for the container so it can be + # easily reentered later. + # If you specify some paths as latter parameters, then these paths will + # be mounted instead of current folder. + # Also mounts wayland for clipboard sync. + + if [ -z "$1" ]; then + echo "give project/container name as first parameter" + return 1 + fi + local container_name + container_name="$1" + shift # skip first parameter as it can be name of a folder/file in + # current dir so it could try mounting it later + for arg in "$@"; do + if [ -f "$arg" ] || [ -d "$arg" ] ; then + local MOUNT_FILE="$MOUNT_FILE -v "$arg:$arg:rw"" + echo "Mounting $arg" + fi + done + if [ -z "$MOUNT_FILE" ]; then + # mount current workdir if no arguments with path + # mount on base_path to make sessions saving work + local base_path + base_path="$(pwd)" + local MOUNT_FOLDER="--workdir /data$base_path -v "$base_path:/data$base_path:rw"" + fi + # make sure there is a folder for sessions on default path + mkdir -p ~/.local/share/nvim/sessions + + podman run --privileged -it \ + -e XDG_RUNTIME_DIR=/runtime_dir \ + -e WAYLAND_DISPLAY="$WAYLAND_DISPLAY" \ + -v "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY:/runtime_dir/$WAYLAND_DISPLAY:rw" \ + -v ~/.local/share/nvim/sessions:/root/.local/share/nvim/sessions:rw \ + $MOUNT_FILE \ + $MOUNT_FOLDER \ + --entrypoint bash \ + --name $container_name \ + neovim:latest +}