Skip to content

Instantly share code, notes, and snippets.

@elundmark
Last active July 28, 2019 14:14
Show Gist options
  • Save elundmark/bf1e3fc925aa8d8472ea449e4f6766f2 to your computer and use it in GitHub Desktop.
Save elundmark/bf1e3fc925aa8d8472ea449e4f6766f2 to your computer and use it in GitHub Desktop.
Modified vimv for subl (removed: git mv added: error checking - all files are protected from being overwritten , i3wm stuff)
#!/usr/bin/env bash
f() {
local -a args=("${@}") valid_args=()
local -i i=0 len=${#args[@]}
for (( ; i < len; i++ )); do
if [[ "${args[i]}" != '' ]] ; then
valid_args+=("${args[i]}")
else
echo "Removed empty string [$((i+1))]" 1>&2
fi
done
if (( ${#valid_args[@]} > 0 )) ; then
local IFS=$'\n'
echo "${valid_args[*]}"
fi
return $?
}
f "${@}"
exit $?
Copyright 2017 Thameera Senanayaka
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/usr/bin/env bash
# Lists the current directory's files in Vim, so you can edit it and save to rename them
# USAGE: vimv [file1 file2]
# https://github.com/thameera/vimv
# MODIFIED: 2019-07-28 13:52:21
# BY: elundmark@telia.com
# git clone https://github.com/thameera/vimv.git
# New usage: rename_with_subl.sh [file1 file2]
# This script uses subl
set -e
declare -a src=() dest=()
declare tmp_file wp_visit_from="$HOME/.ramdisk/.vimv_rename_opened_from_where"
tmp_file="$HOME/.ramdisk/$(basename "$(mktemp -u -t '.vimv_rename_XXXXXXXXXX')")"
declare -i count=0
if command -v i3-msg &>/dev/null; then
i3-msg -t get_workspaces | jq '.[] | select(.focused==true).name' > "$wp_visit_from"
fi
if [[ $# -ne 0 ]] ; then
src=("$@")
else
readarray -t src < <(find -L "$(pwd)" -mindepth 1 -maxdepth 1 -regextype posix-extended -not -path "$(pwd)" -not -regex '.*/\.Trash(-[0-9]+)?/.+' -not -regex '.*/\.git/.+' -not -regex '.*/[^/].*-back-ovfs(/.+)?' -not -regex '.*/node_modules/.+' -not -regex "$HOME/.wine/dosdevices/.*" -not -regex '.*/lost\+found/?.*' -iregex '^.+$' -printf '%f\n' | sort)
fi
touch "$tmp_file" || exit 1
args_to_lines "${src[@]}" >> "$tmp_file"
if command -v i3-msg &>/dev/null; then
i3-msg 'workspace "4 "' &>/dev/null
fi
while [[ "$count" -eq 0 && -f "$tmp_file" ]] ; do
subl -w "$tmp_file"
readarray -t dest < <(cat "$tmp_file")
for ((i=0;i<${#src[@]};++i)); do
if [[ "${src[i]}" != "${dest[i]}" ]] ; then
if [[ -e "${dest[i]}" ]] ; then
echo "Destination aĺready exists:"$'\n'"${dest[i]}" 1>&2
count=0
break
fi
mkdir -p "$(dirname "${dest[i]}")"
mv -n -v "${src[i]}" "${dest[i]}"
if [[ $? -eq 0 && -e "${dest[i]}" && ! -e "${src[i]}" ]] ; then
count=$((count+1))
else
if [[ -f "$wp_visit_from" ]] ; then
i3-msg -t command "workspace $(< "$wp_visit_from")" &>/dev/null
fi
echo "An error occured!" 1>&2
file "${dest[i]}" 1>&2
file "${src[i]}" 1>&2
# easaier to troubleshoot if we just exit here and not trigger another loop
exit 1
fi
else
echo "src and dest are the same:"$'\n'"${src[i]}"$'\n'"${dest[i]}" 1>&2
count=0
break
fi
done
done
echo "$count" files were renamed.
if [[ -f "$wp_visit_from" ]] ; then
i3-msg -t command "workspace $(< "$wp_visit_from")" &>/dev/null
fi
rm "$tmp_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment