Skip to content

Instantly share code, notes, and snippets.

@MarcinKonowalczyk
Last active July 6, 2021 12:51
Show Gist options
  • Save MarcinKonowalczyk/709e93f08e9d72f8092acd5b8d34c81f to your computer and use it in GitHub Desktop.
Save MarcinKonowalczyk/709e93f08e9d72f8092acd5b8d34c81f to your computer and use it in GitHub Desktop.
vscode script to run .vscode/run.sh script on the current file
#! /bin/bash
# Expanded bash script
# This file. In the vscode keybinding this comes from ${file}
FILE="$0";
# The directory of this file (expanded.sh).
# In keyboding this is the dirname of the $FILE
PWD=$(pwd);
# script file to search for
SCRIPT=".vscode/run.sh";
# Echo to see what's up
echo "FILE = $FILE";
echo "PWD = $PWD";
echo "SCRIPT = $SCTIPT";
while :; do
RUN="$PWD/$SCRIPT";
if [[ -e $RUN ]]; then
echo "Run script found at $RUN";
"$RUN" "$FILE";
break;
else
echo "No $RUN found";
fi;
if [[ $PWD != "/" && $PWD != "~" ]]; then :; else
echo "No $SCRIPT found";
break;
fi;
PWD=$(dirname "$PWD");
done
[
{ // Run the ./.vscode/run.sh in terminal, passing it the name of the current file if it exists
// Also, search through parent directories to see whether there is a .vscode/run.sh there
"key": "shift+enter",
"command": "workbench.action.terminal.sendSequence",
"when": "editorTextFocus",
"args": {
// "text": "\u0003bash -c 'FILE=\"${file}\"; PWD=$(dirname \"$FILE\"); SCRIPT=\".vscode/run.sh\"; echo \"FILE = $FILE\"; echo \"PWD = $PWD\"; echo \"SCRIPT = $SCRIPT\"; while :; do RUN=\"$PWD/$SCRIPT\"; if [[ -e $RUN ]]; then echo \"Run script found at $RUN\"; \"$RUN\" \"$FILE\"; break; else echo \"No $RUN found\"; fi; [[ $PWD != \"/\" && $PWD != \"~\" ]] || { echo \"No $SCRIPT found\" && break; }; PWD=$(dirname \"$PWD\"); done'\u000D",
"text": "\u0003bash -c 'FILE=\"${file}\"; PWD=$(dirname \"$FILE\"); SCRIPT=\".vscode/run.sh\"; while :; do RUN=\"$PWD/$SCRIPT\"; if [[ -e $RUN ]]; then \"$RUN\" \"$FILE\"; break; fi; [[ $PWD != \"/\" && $PWD != \"~\" ]] || { echo \"No $SCRIPT found\" && break; }; PWD=$(dirname \"$PWD\"); done'\u000D"
}
}
]
echo:
@echo "Hello from the makefile!"
# https://gist.github.com/MarcinKonowalczyk/709e93f08e9d72f8092acd5b8d34c81f
# Example .run.sh
echo "Hello from run script! ^_^"
# The direcotry of the main project from which this script is running
# https://stackoverflow.com/a/246128/2531987
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
ROOT="${ROOT%/*}" # Strip .vscode folder
NAME="${ROOT##*/}" # Project name
PWD=$(pwd);
# Extension, filename and directory parts of the file which triggered this
# http://mywiki.wooledge.org/BashSheet#Parameter_Operations
FILE="$1"
FILENAME="${FILE##*/}" # Filename with extension
FILEPATH="${FILE%/*}" # Path of the current file
FILEFOLDER="${FILEPATH##*/}" # Folder in which the current file is located (could be e.g. a nested subdirectory)
EXTENSION="${FILENAME##*.}" # Just the extension
ROOTFOLDER="${1##*$ROOT/}" && ROOTFOLDER="${ROOTFOLDER%%/*}" # folder in the root directory (not necesarilly the same as FILEFOLDER)
[ $ROOTFOLDER != $FILENAME ] || ROOTFOLDER=""
# Echo of path variables
VERBOSE=true
if $VERBOSE; then
# https://stackoverflow.com/a/5947802/2531987
GREEN='\033[0;32m'; YELLOW='\033[0;33m'; RED='\033[0;31m'; NC='\033[0m'
echo -e "ROOT : $GREEN${ROOT}$NC # root directory of the project"
echo -e "NAME : $GREEN${NAME}$NC # project name"
echo -e "PWD : $GREEN${PWD}$NC # pwd"
echo -e "FILE : $GREEN${FILE}$NC # full file information"
echo -e "FILENAME : $GREEN${FILENAME}$NC # current filename"
echo -e "FILEPATH : $GREEN${FILEPATH}$NC # path of the current file"
echo -e "FILEFOLDER : $GREEN${FILEFOLDER}$NC # folder in which the current file is located"
echo -e "EXTENSION : $GREEN${EXTENSION}$NC # just the extension of the current file"
if [ $ROOTFOLDER ]; then
if [ $ROOTFOLDER != $FILEFOLDER ]; then
echo -e "ROOTFOLDER : $GREEN${ROOTFOLDER}$NC # folder in the root directory"
else
echo -e "ROOTFOLDER : ${YELLOW}<same as FILEFOLDER>${NC}"
fi
else
echo -e "ROOTFOLDER : ${YELLOW}<file in ROOT>${NC}"
fi
fi
##################################################
# Do some other stuff here. In this case run the makefile if
# you're currently editing the makefile
if [ "$FILENAME" = "makefile" ]; then
echo "About to run the makefile!";
make;
fi

run.sh

Vscode keybinding script to run .vscode/run.sh in bash, passing the current file as an input.

instructions

Add the keybinding to keybindings.json, then in your project make the .vscode folder (if there isnt one there already and add run.sh file to it). It will be run each time you press the keybinding shift+enter with an editor focus.

[ -d ".vscode" ] || mkdir ".vscode"
SOURCE="https://gist.githubusercontent.com/MarcinKonowalczyk/709e93f08e9d72f8092acd5b8d34c81f/raw/2a1de7e14c654f48c1cdfd8ab50fe00d067c0024/run.sh"
curl $SOURCE > .vscode/run.sh
chmod u+x ./.vscode/run.sh

one-liner

[ -d ".vscode" ] || mkdir ".vscode"; curl https://gist.githubusercontent.com/MarcinKonowalczyk/709e93f08e9d72f8092acd5b8d34c81f/raw/2a1de7e14c654f48c1cdfd8ab50fe00d067c0024/run.sh > .vscode/run.sh && chmod u+x ./.vscode/run.sh

test

cd ~
mkdir test
cd test
[ -d ".vscode" ] || mkdir ".vscode"; curl https://gist.githubusercontent.com/MarcinKonowalczyk/709e93f08e9d72f8092acd5b8d34c81f/raw/2a1de7e14c654f48c1cdfd8ab50fe00d067c0024/run.sh > .vscode/run.sh && chmod u+x ./.vscode/run.sh
touch hi.txt
code hi.txt

And press shift+enter. You should see something like:

Hello from run script! ^_^
ROOT       : /Users/marcinkonowalczyk/test  # root directory of the project
NAME       : test  # project name
PWD        : /Users/marcinkonowalczyk/test  # pwd
FILE       : /Users/marcinkonowalczyk/test/.vscode/run.sh # full file information
FILENAME   : run.sh  # current filename
FILEPATH   : /Users/marcinkonowalczyk/test/.vscode  # path of the current file
FILEFOLDER : .vscode  # folder in which the current file is located
EXTENSION  : sh  # just the extension of the current file
ROOTFOLDER : <same as FILEFOLDER>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment