Skip to content

Instantly share code, notes, and snippets.

@doitian
Created February 8, 2020 08:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doitian/0c8775e88ceed7bac44c4fb4287822d5 to your computer and use it in GitHub Desktop.
Save doitian/0c8775e88ceed7bac44c4fb4287822d5 to your computer and use it in GitHub Desktop.
[Vim iTerm Launcher] Launch a new iTerm window and run Vim in it #macOS #automation
#!/usr/bin/env bash
# Run a command in a new iterm window, examples:
#
# iterm vim ~/.vimrc
set -e
set -u
[ -n "${DEBUG:-}" ] && set -x || true
export LANG=C.UTF-8
COMMAND=
if [ "$#" != 0 ]; then
COMMAND="$(printf '%q ' "$@")"
fi
osascript -l JavaScript -e 'function run(arg) {
const iterm = Application("iTerm");
const window = iterm.createWindowWithDefaultProfile();
iterm.write(window.currentSession, {text: arg});
}' "$COMMAND"
#!/usr/bin/env bash
# Vim launcher. Quits the parent shell when Vim quits.
#
# Examples:
#
# iterm-vim-wrapper file
#
# # edit clipboard in temp file clipboard.js
# iterm-vim-wrapper --edit-clipboard js
#
# # edit clipboard in temp file Car.java
# iterm-vim-wrapper --edit-clipboard Car.java
#
# # edit an empty scratch file scratch.js in a temp directory.
# iterm-vim-wrapper --edit-scratch js
#
# # edit an empty scratch file Car.java in a temp directory.
# iterm-vim-wrapper --edit-scratch Car.java
#
# It can be used together with iterm, such as
#
# iterm iterm-vim-wrapper --edit-clipboard
set -u
[ -n "${DEBUG:-}" ] && set -x || true
function vim_clipboard() {
set -e
local basename="$1"
shift
local arg="${1:-}"
local dir="$(mktemp -d "$TMPDIR/edit-in-editor.XXXXX")"
local file="$basename"
case "x${arg}" in
x.*) file="$file$arg";;
*.*) file="$arg";;
x) : ;;
*) file="$file.$arg";;
esac
trap "rm -rf $dir" EXIT
cd "$dir"
if [ "$basename" = clipboard ]; then
pbpaste > "$file"
vim -n -b "$file" && pbcopy < "$file"
else
vim -n -b "$file"
fi
}
if [ "${1:-}" = "--edit-clipboard" ]; then
# Edit clipboard in vim. Replace the clipboard with the edited content.
shift
vim_clipboard clipboard "$@"
elif [ "${1:-}" = "--edit-scratch" ]; then
# Edit an empty scratch file in a tempory directory
shift
vim_clipboard scratch "$@"
else
vim "$@"
fi
# Kill the parent process (the shell) when Vim quits
kill -SIGUSR1 $PPID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment