Skip to content

Instantly share code, notes, and snippets.

@thingsiplay
Last active July 15, 2021 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thingsiplay/5669c7264a3f52e28e1ac7652c9ada97 to your computer and use it in GitHub Desktop.
Save thingsiplay/5669c7264a3f52e28e1ac7652c9ada97 to your computer and use it in GitHub Desktop.
Select textfile in dmenu and open it in Vim.
#!/usr/bin/env bash
# vimd by Tuncay D.
# Select textfile in dmenu and open it in Vim.
#
# Usage:
# vimd
# Or:
# vimd DIRECTORY
# If TERMINAL is empty, then vim will be run directly.
# If TERMINAL is set to any value, it will use it as the terminal command to
# open a new window.
#
# Examples:
# "alacritty -e", "xterm -e", "konsole -e", "gnome-terminal --",
# "xfce4-terminal -e"
TERMINAL=
# TERMINAL="alacritty -e"
# Note: In case TERMINAL is not empty
# A few specific terminal require different quoting on execution. So in
# case of such a terminal, edit the line at the end of the script from
# $TERMINAL vim "$FILE"
# to
# $TERMINAL "vim $FILE"
# The terminal "xfce-terminal" is known to require this.
# Default to current working directory, if no DIRECTORY was given.
if [ "$#" -eq "0" ]
then
# Default folder to open in.
DIRECTORY="."
else
DIRECTORY="$1"
fi
# find: List all files from DIRECTORY, no folders.
# grep: Check if file is binary and get text files only.
# print: Print to stdout, so dmenu can catch the files.
# dmenu: Show a horizontal list of all found files.
#
DMENU="dmenu -i -l 15"
# DMENU="rofi -dmenu -i -l 15"
FILE=$(find "$DIRECTORY" -maxdepth 1 -type f -exec grep -sIq . {} \; -print | $DMENU)
# Open file only, if a file was selected or a new name is typed in.
if [ -n "$FILE" ]
then
if [ ! -f "$FILE" ]
then
FILE=$DIRECTORY/$FILE
fi
FILE=$(readlink -f "$FILE")
echo "$FILE"
if [ -n "$TERMINAL" ]
then
$TERMINAL vim "$FILE"
# Uncomment the below line, if the terminal does not work with above
# code. Some terminal might handle the quoting on arguments
# differently. Use this line instead, if you use "xfce-terminal".
# $TERMINAL "vim $FILE"
else
vim "$FILE"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment