Linux(Debian Stretch)でTexpander(TextExpanderのようなスニペットツール)を使ってみた
2018年09月06日 木曜日 09時00分仕事の関係もあり、普段使っているマシンはMacBookPro15で、スニペットツールとしてTypinatorを利用しています。
macのスニペットツールでググると
TextExpander、Alfred、Dash、など、いろいろなアプリがありますよね。
macにはいろいろなスニペットツールがあるのですが、Linuxでは選択肢が少ないと思います。
勉強用に使っているDebianでも、スニペットツールを使いたいなと思い、色々と調べたのでメモとして書いておきます。
Linuxのスニペットツール
TextExpanderの代わりになるアプリをググると、alternativeToというサイトがよくでてきます。
おそらく、以下が定番なんでしょう。
色々と試してみた
-
AutoKeyは、Pythonで書かれているので、Pythonができる方はおすすめ。
-
xdotoolは、X11の操作をコマンドで操作できるコマンドのようです。
# As of version 2.20100623, you can do this simpler version of above:
xdotool search "Mozilla Firefox" windowactivate --sync key --clearmodifiers ctrl+l
このコマンドを実行するとFirefoxをアクティブにして、ctrl+lを入力する
状態にできます。
xdotoolは、Texpander内でも利用されています。
- Texpanderは、シェルスクリプトなので、カスタマイズが簡単にできるところが良いです!
というころで、今回は、Texpanderを使うことにしました!
Texpanderを使ってみる
作者の方のブログを読めば分かりますが、一応…
https://leehblue.com/ubuntu-text-expander/

必要なコマンドをインストールする
sudo apt install xsel
sudo apt install xdotool
sudo apt install zenity
clone
Downloadsにcloneしてみます。
~ $ cd Downloads/
Downloads $ git clone [email protected]:leehblue/texpander.git
Downloads $ cd texpander/
texpander $ ls -la
total 64
drwxr-xr-x 3 teruhirokomaki teruhirokomaki 4096 Sep 7 21:37 .
drwxr-xr-x 5 teruhirokomaki teruhirokomaki 4096 Sep 7 21:36 ..
drwxr-xr-x 8 teruhirokomaki teruhirokomaki 4096 Sep 7 21:37 .git
-rw-r--r-- 1 teruhirokomaki teruhirokomaki 6 Sep 7 21:37 .gitignore
-rw-r--r-- 1 teruhirokomaki teruhirokomaki 35142 Sep 7 21:37 LICENSE.md
-rw-r--r-- 1 teruhirokomaki teruhirokomaki 5169 Sep 7 21:37 README.md
-rwxr-xr-x 1 teruhirokomaki teruhirokomaki 2080 Sep 7 21:37 texpander.sh
texpander.shをすきな場所におく
home/user/bin/
にtexpander.sh
をおく。
~/bin
がなければ作成してください。
Downloads $ cp texpander/texpander.sh ~/bin/
.texpanderを作成しテストファイルをおく
Downloads $ cd
~ $ mkdir .texpander
~ $ echo 'test' > .texpander/test.txt
ショートカットを設定する
作者の方と同じようにショートカットを設定します。


一部修正した
$HOME
の部分を修正した。
# If ~/.texpander directory does not exist, create it
if [ ! -d ${$HOME}/.texpander ]; then
mkdir ${$HOME}/.texpander
fi
修正後
# If ~/.texpander directory does not exist, create it
if [ ! -d ${HOME}/.texpander ]; then
mkdir ${HOME}/.texpander
fi
シェルスクリプトを見てみる
一部を抜粋してます…
以下のスクリプトを見てわかるとおり、かなりシンプルなため…
name=$(zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations $abbrvs)
path="${base_dir}/${name}"
if [ -f "${base_dir}/${name}" ]
then
if [ -e "$path" ]
then
# Preserve the current value of the clipboard
clipboard=$(xsel -b -o)
# Put text in primary buffer for Shift+Insert pasting
echo -n "$(cat "$path")" | xsel -p -i
# Put text in clipboard selection for apps like Firefox that
# insist on using the clipboard for all pasting
echo -n "$(cat "$path")" | xsel -b -i
# Paste text into current active window
sleep 0.3
xdotool key shift+Insert
ファイルが.sh
と.go
の場合の条件を追加してみた
name=$(zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations $abbrvs)
path="${base_dir}/${name}"
if [ -f "${base_dir}/${name}" ]
then
if [ -e "$path" ]
then
# Preserve the current value of the clipboard
clipboard=$(xsel -b -o)
# get ext from path
ext=$(echo $path | sed 's/^.*\.\([^\.]*\)$/\1/')
# check ext
if [ $ext = "sh" ]; then
bash $(echo $path) | xsel -p -i
bash $(echo $path) | xsel -b -i
elif [ $ext = "go" ]; then
go run $(echo $path) | xsel -p -i
go run $(echo $path) | xsel -b -i
else
# Put text in primary buffer for Shift+Insert pasting
echo -n "$(cat "$path")" | xsel -p -i
# Put text in clipboard selection for apps like Firefox that
# insist on using the clipboard for all pasting
echo -n "$(cat "$path")" | xsel -b -i
fi
# Paste text into current active window
sleep 0.3
xdotool key shift+Insert
使ってみる
こんな感じで使えます。

zenityコマンドを見てみる
Texpnaderで使われているzenity
というコマンドは、どんなものか見てみました。
manをみる
man zenity
ZENITY(1) General Commands Manual ZENITY(1)
NAME
zenity - display GTK+ dialogs
SYNOPSIS
zenity [options]
DESCRIPTION
zenity is a program that will display GTK+ dialogs, and return (either in the return code, or on standard output) the users input. This allows you to present information, and ask for information
from the user, from all manner of shell scripts.
For example, zenity --question will return either 0, 1 or 5, depending on whether the user pressed OK, Cancel or timeout has been reached. zenity --entry will output on standard output what the user
typed into the text entry field.
Comprehensive documentation is available in the GNOME Help Browser.
OPTIONS
This program follows the usual GNU command line syntax, with long options starting with two dashes (`-').
Dialog options
--calendar
Display calendar dialog
--entry
Display text entry dialog
--error
Display error dialog
--file-selection
Display file selection dialog
--info Display info dialog
--list Display list dialog
--notification
Display notification
--progress
Display progress indication dialog
--question
Display question dialog
--text-info
Display text information dialog
--warning
Display warning dialog
--scale
Display scale dialog
--color-selection
Display color selection dialog
--password
Display password dialog
--forms
Display forms dialog
General options
--title=TITLE
Set the dialog title
--window-icon=ICONPATH
Set the window icon with the path to an image. Alternatively, one of the four stock icons can be used: 'error', 'info', 'question' or 'warning'
--width=WIDTH
Set the dialog width
--height=HEIGHT
Set the dialog height
--timeout=TIMEOUT
Set the dialog timeout in seconds
Calendar options
--text=STRING
Set the dialog text
--day=INT
Set the calendar day
--month=INT
Set the calendar month
--year=INT
Set the calendar year
--date-format=PATTERN
Set the format for the returned date. The default depends on the user locale or be set with the strftime style. For example %A %d/%m/%y
Text entry options
--text=STRING
Set the dialog text
--entry-text=STRING
Set the entry text
--hide-text
Hide the entry text
Error options
--text=STRING
Set the dialog text
--no-wrap
Do not enable text wrapping
--no-markup
Do not enable pango markup
File selection options
--filename=FILENAME
Set the file or directory to be selected by default
--multiple
Allow selection of multiple filenames in file selection dialog
--directory
Activate directory-only selection
--save Activate save mode
--separator=SEPARATOR
Specify separator character when returning multiple filenames
--confirm-overwrite
Confirm file selection if filename already exists
--file-filter=NAME | PATTERN1 PATTERN2
Sets a filename filter
Info options
--text=STRING
Set the dialog text
--no-wrap
Do not enable text wrapping
--no-markup
Do not enable pango markup
List options
--text=STRING
Set the dialog text
--column=STRING
Set the column header
--checklist
Use check boxes for first column
--radiolist
Use radio buttons for first column
--separator=STRING
Set output separator character
--multiple
Allow multiple rows to be selected
--editable
Allow changes to text
--print-column=NUMBER
Specify what column to print to standard output. The default is to return the first column. 'ALL' may be used to print all columns.
--hide-column=NUMBER
Hide a specific column
--hide-header
Hides the column headers
Notification options
--text=STRING
Set the notification text
--listen
Listen for commands on stdin. Commands include 'message', 'tooltip', 'icon', and 'visible' separated by a colon. For example, 'message: Hello world', 'visible: false', or 'icon:
/path/to/icon'. The icon command also accepts the four stock icon: 'error', 'info', 'question', and 'warning'
Progress options
--text=STRING
Set the dialog text
--percentage=INT
Set initial percentage
--auto-close
Close dialog when 100% has been reached
--auto-kill
Kill parent process if cancel button is pressed
--pulsate
Pulsate progress bar
--no-cancel
Hides the cancel button
Question options
--text=STRING
Set the dialog text
--no-wrap
Do not enable text wrapping
--no-markup
Do not enable pango markup
--ok-label
Set the text of the OK button
--cancel-label
Set the text of the cancel button
Text options
--filename=FILENAME
Open file
--editable
Allow changes to text
--checkbox=TEXT
Enable a checkbox for use like a 'I read and accept the terms.'
--ok-label
Set the text of the OK button
--cancel-label
Set the text of the cancel button
Warning options
--text=STRING
Set the dialog text
--no-wrap
Do not enable text wrapping
--no-markup
Do not enable pango markup
Scale options
--text=STRING
Set the dialog text
--value=VALUE
Set initial value
--min-value=VALUE
Set minimum value
--max-value=VALUE
Set maximum value
--step=VALUE
Set step size
--print-partial
Print partial values
--hide-value
Hide value
Color selection options
--color=VALUE
Set the initial color
--show-palette
Show the palette
Password dialog options
--username
Display the username field
Forms dialog options
--add-entry=FIELDNAME
Add a new Entry in forms dialog
--add-password=FIELDNAME
Add a new Password Entry in forms dialog
--add-calendar=FIELDNAME
Add a new Calendar in forms dialog
--text=STRING
Set the dialog text
--separator=STRING
Set output separator character
--forms-date-format=PATTERN
Set the format for the returned date. The default depends on the user locale or be set with the strftime style. For example %A %d/%m/%y
Miscellaneous options
-?, --help
Show summary of options.
--about
Display an about dialog.
--version
Show version of program.
Also the standard GTK+ options are accepted. For more information about the GTK+ options, execute following command.
zenity --help-gtk
ENVIRONMENT
Normally, zenity detects the terminal window from which it was launched and keeps itself above that window. This behavior can be disabled by unsetting the WINDOWID environment variable.
EXAMPLES
Display a file selector with the title Select a file to remove. The file selected is returned on standard output.
zenity --title="Select a file to remove" --file-selection
Display a text entry dialog with the title Select Host and the text Select the host you would like to flood-ping. The entered text is returned on standard output.
zenity --title "Select Host" --entry --text "Select the host you would like to flood-ping"
Display a dialog, asking Microsoft Windows has been found! Would you like to remove it?. The return code will be 0 (true in shell) if OK is selected, and 1 (false) if Cancel is selected.
zenity --question --title "Alert" --text "Microsoft Windows has been found! Would you like to remove it?"
Show the search results in a list dialog with the title Search Results and the text Finding all header files....
find . -name '*.h' | zenity --list --title "Search Results" --text "Finding all header files.." --column "Files"
Show a notification in the message tray
zenity --notification --window-icon=update.png --text "System update necessary!"
Display a weekly shopping list in a check list dialog with Apples and Oranges pre selected
zenity --list --checklist --column "Buy" --column "Item" TRUE Apples TRUE Oranges FALSE Pears FALSE Toothpaste
Display a progress dialog while searching for all the postscript files in your home directory
find $HOME -name '*.ps' | zenity --progress --pulsate
AUTHOR
Zenity was written by Glynn Foster <[email protected]>.
This manual page was written by Ross Burton <[email protected]>.
SEE ALSO
gdialog(1), dialog(1)
色々と叩いてみる
--list
のオプションを指定してみる。
~ $ zenity --list --title=Texpander --width=275 --height=400 --column="no" --column="Abbreviations" --separator="|" --text=STRING --multiple --print-column=ALL 1 "aaa" 2 "bbb"
どうでしょうか。

ダイアログはこんな感じです。

--separator="|"
と--multiple
を指定して、2つを選択してるためか、出力はこんな感じです。
~ $ 1|aaa|2|bbb
別のオプションを指定してみる。
--editable
と--column="param"
を追加してみました。
~ $ zenity --list --title=Texpander --width=275 --height=400 --column="no" --column="Abbreviations" --column="param" --editable --separator="|" --text=STRING --multiple --print-column=ALL 1 "aaa" "" 2 "bbb" ""

~ $ 1|aaa|引数を
やろうと思えば、任意の文字列を渡せそうです。
zenityは、list以外にも色々とあるので、いじってみます。