Teruhiro Komaki's Blog (Temporary)

Linux(Debian Stretch)でTexpander(TextExpanderのようなスニペットツール)を使ってみた

仕事の関係もあり、普段使っているマシンはMacBookPro15で、スニペットツールとしてTypinatorを利用しています。

macのスニペットツールでググると

TextExpanderAlfredDash、など、いろいろなアプリがありますよね。

macにはいろいろなスニペットツールがあるのですが、Linuxでは選択肢が少ないと思います。

勉強用に使っているDebianでも、スニペットツールを使いたいなと思い、色々と調べたのでメモとして書いておきます。

Linuxのスニペットツール

TextExpanderの代わりになるアプリをググると、alternativeToというサイトがよくでてきます。

おそらく、以下が定番なんでしょう。

色々と試してみた

1# As of version 2.20100623, you can do this simpler version of above:
2xdotool search "Mozilla Firefox" windowactivate --sync key --clearmodifiers ctrl+l

このコマンドを実行するとFirefoxをアクティブにして、ctrl+lを入力する状態にできます。

xdotoolは、Texpander内でも利用されています。

というころで、今回は、Texpanderを使うことにしました!

Texpanderを使ってみる

作者の方のブログを読めば分かりますが、一応…

https://leehblue.com/ubuntu-text-expander/

texpander

必要なコマンドをインストールする

1sudo apt install xsel
2sudo apt install xdotool
3sudo apt install zenity

clone

Downloadsにcloneしてみます。

 1~ $ cd Downloads/
 2Downloads $ git clone [email protected]:leehblue/texpander.git
 3Downloads $ cd texpander/
 4texpander $ ls -la
 5total 64
 6drwxr-xr-x 3 teruhirokomaki teruhirokomaki  4096 Sep  7 21:37 .
 7drwxr-xr-x 5 teruhirokomaki teruhirokomaki  4096 Sep  7 21:36 ..
 8drwxr-xr-x 8 teruhirokomaki teruhirokomaki  4096 Sep  7 21:37 .git
 9-rw-r--r-- 1 teruhirokomaki teruhirokomaki     6 Sep  7 21:37 .gitignore
10-rw-r--r-- 1 teruhirokomaki teruhirokomaki 35142 Sep  7 21:37 LICENSE.md
11-rw-r--r-- 1 teruhirokomaki teruhirokomaki  5169 Sep  7 21:37 README.md
12-rwxr-xr-x 1 teruhirokomaki teruhirokomaki  2080 Sep  7 21:37 texpander.sh

texpander.shをすきな場所におく

home/user/bin/texpander.sh をおく。

~/binがなければ作成してください。

1Downloads $ cp texpander/texpander.sh ~/bin/

.texpanderを作成しテストファイルをおく

1Downloads $ cd
2~ $ mkdir .texpander
3~ $ echo 'test' > .texpander/test.txt

ショートカットを設定する

作者の方と同じようにショートカットを設定します。

texpander

texpander

一部修正した

$HOMEの部分を修正した。

1# If ~/.texpander directory does not exist, create it
2if [ ! -d ${$HOME}/.texpander ]; then
3    mkdir ${$HOME}/.texpander
4fi

修正後

1# If ~/.texpander directory does not exist, create it
2if [ ! -d ${HOME}/.texpander ]; then
3    mkdir ${HOME}/.texpander
4fi

シェルスクリプトを見てみる

一部を抜粋してます…

以下のスクリプトを見てわかるとおり、かなりシンプルなため…

 1name=$(zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations $abbrvs)
 2
 3path="${base_dir}/${name}"
 4
 5if [ -f "${base_dir}/${name}" ]
 6then
 7  if [ -e "$path" ]
 8  then
 9    # Preserve the current value of the clipboard
10    clipboard=$(xsel -b -o)
11
12    # Put text in primary buffer for Shift+Insert pasting
13    echo -n "$(cat "$path")" | xsel -p -i
14
15    # Put text in clipboard selection for apps like Firefox that 
16    # insist on using the clipboard for all pasting
17    echo -n "$(cat "$path")" | xsel -b -i
18
19    # Paste text into current active window
20    sleep 0.3
21    xdotool key shift+Insert

ファイルが.sh.goの場合の条件を追加してみた

 1name=$(zenity --list --title=Texpander --width=275 --height=400 --column=Abbreviations $abbrvs)
 2
 3path="${base_dir}/${name}"
 4
 5if [ -f "${base_dir}/${name}" ]
 6then
 7  if [ -e "$path" ]
 8  then
 9    # Preserve the current value of the clipboard
10    clipboard=$(xsel -b -o)
11
12    # get ext from path
13    ext=$(echo $path | sed 's/^.*\.\([^\.]*\)$/\1/')
14
15    # check ext
16    if [ $ext = "sh" ]; then
17      bash $(echo $path) | xsel -p -i
18      bash $(echo $path) | xsel -b -i
19
20    elif [ $ext = "go" ]; then
21      go run $(echo $path) | xsel -p -i
22      go run $(echo $path) | xsel -b -i
23
24    else
25      # Put text in primary buffer for Shift+Insert pasting
26      echo -n "$(cat "$path")" | xsel -p -i
27
28      # Put text in clipboard selection for apps like Firefox that 
29      # insist on using the clipboard for all pasting
30      echo -n "$(cat "$path")" | xsel -b -i
31
32    fi
33
34    # Paste text into current active window
35    sleep 0.3
36    xdotool key shift+Insert

使ってみる

こんな感じで使えます。

texpander

zenityコマンドを見てみる

Texpnaderで使われているzenityというコマンドは、どんなものか見てみました。

manをみる

1man zenity
  1ZENITY(1)                                                                                  General Commands Manual                                                                                  ZENITY(1)
  2
  3NAME
  4       zenity - display GTK+ dialogs
  5
  6SYNOPSIS
  7       zenity [options]
  8
  9DESCRIPTION
 10       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
 11       from the user, from all manner of shell scripts.
 12
 13       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
 14       typed into the text entry field.
 15
 16       Comprehensive documentation is available in the GNOME Help Browser.
 17
 18OPTIONS
 19       This program follows the usual GNU command line syntax, with long options starting with two dashes (`-').
 20
 21       Dialog options
 22
 23       --calendar
 24              Display calendar dialog
 25
 26       --entry
 27              Display text entry dialog
 28
 29       --error
 30              Display error dialog
 31
 32       --file-selection
 33              Display file selection dialog
 34
 35       --info Display info dialog
 36
 37       --list Display list dialog
 38
 39       --notification
 40              Display notification
 41
 42       --progress
 43              Display progress indication dialog
 44
 45       --question
 46              Display question dialog
 47
 48       --text-info
 49              Display text information dialog
 50
 51       --warning
 52              Display warning dialog
 53
 54       --scale
 55              Display scale dialog
 56
 57       --color-selection
 58              Display color selection dialog
 59
 60       --password
 61              Display password dialog
 62
 63       --forms
 64              Display forms dialog
 65
 66       General options
 67
 68       --title=TITLE
 69              Set the dialog title
 70
 71       --window-icon=ICONPATH
 72              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'
 73
 74       --width=WIDTH
 75              Set the dialog width
 76
 77       --height=HEIGHT
 78              Set the dialog height
 79
 80       --timeout=TIMEOUT
 81              Set the dialog timeout in seconds
 82
 83       Calendar options
 84
 85       --text=STRING
 86              Set the dialog text
 87
 88       --day=INT
 89              Set the calendar day
 90
 91       --month=INT
 92              Set the calendar month
 93
 94       --year=INT
 95              Set the calendar year
 96
 97       --date-format=PATTERN
 98              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
 99
100       Text entry options
101
102       --text=STRING
103              Set the dialog text
104
105       --entry-text=STRING
106              Set the entry text
107
108       --hide-text
109              Hide the entry text
110
111       Error options
112
113       --text=STRING
114              Set the dialog text
115
116       --no-wrap
117              Do not enable text wrapping
118
119       --no-markup
120              Do not enable pango markup
121
122       File selection options
123
124       --filename=FILENAME
125              Set the file or directory to be selected by default
126
127       --multiple
128              Allow selection of multiple filenames in file selection dialog
129
130       --directory
131              Activate directory-only selection
132
133       --save Activate save mode
134
135       --separator=SEPARATOR
136              Specify separator character when returning multiple filenames
137
138       --confirm-overwrite
139              Confirm file selection if filename already exists
140
141       --file-filter=NAME | PATTERN1 PATTERN2
142              Sets a filename filter
143
144       Info options
145
146       --text=STRING
147              Set the dialog text
148
149       --no-wrap
150              Do not enable text wrapping
151
152       --no-markup
153              Do not enable pango markup
154
155       List options
156
157       --text=STRING
158              Set the dialog text
159
160       --column=STRING
161              Set the column header
162
163       --checklist
164              Use check boxes for first column
165
166       --radiolist
167              Use radio buttons for first column
168
169       --separator=STRING
170              Set output separator character
171
172       --multiple
173              Allow multiple rows to be selected
174
175       --editable
176              Allow changes to text
177
178       --print-column=NUMBER
179              Specify what column to print to standard output. The default is to return the first column. 'ALL' may be used to print all columns.
180
181       --hide-column=NUMBER
182              Hide a specific column
183
184       --hide-header
185              Hides the column headers
186
187       Notification options
188
189       --text=STRING
190              Set the notification text
191
192       --listen
193              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:
194              /path/to/icon'. The icon command also accepts the four stock icon: 'error', 'info', 'question', and 'warning'
195
196       Progress options
197
198       --text=STRING
199              Set the dialog text
200
201       --percentage=INT
202              Set initial percentage
203
204       --auto-close
205              Close dialog when 100% has been reached
206
207       --auto-kill
208              Kill parent process if cancel button is pressed
209
210       --pulsate
211              Pulsate progress bar
212
213       --no-cancel
214              Hides the cancel button
215
216       Question options
217
218       --text=STRING
219              Set the dialog text
220
221       --no-wrap
222              Do not enable text wrapping
223
224       --no-markup
225              Do not enable pango markup
226
227       --ok-label
228              Set the text of the OK button
229
230       --cancel-label
231              Set the text of the cancel button
232
233       Text options
234
235       --filename=FILENAME
236              Open file
237
238       --editable
239              Allow changes to text
240
241       --checkbox=TEXT
242              Enable a checkbox for use like a 'I read and accept the terms.'
243
244       --ok-label
245              Set the text of the OK button
246
247       --cancel-label
248              Set the text of the cancel button
249
250       Warning options
251
252       --text=STRING
253              Set the dialog text
254
255       --no-wrap
256              Do not enable text wrapping
257
258       --no-markup
259              Do not enable pango markup
260
261       Scale options
262
263       --text=STRING
264              Set the dialog text
265
266       --value=VALUE
267              Set initial value
268
269       --min-value=VALUE
270              Set minimum value
271
272       --max-value=VALUE
273              Set maximum value
274
275       --step=VALUE
276              Set step size
277
278       --print-partial
279              Print partial values
280
281       --hide-value
282              Hide value
283
284       Color selection options
285
286       --color=VALUE
287              Set the initial color
288
289       --show-palette
290              Show the palette
291
292       Password dialog options
293
294       --username
295              Display the username field
296
297       Forms dialog options
298
299       --add-entry=FIELDNAME
300              Add a new Entry in forms dialog
301
302       --add-password=FIELDNAME
303              Add a new Password Entry in forms dialog
304
305       --add-calendar=FIELDNAME
306              Add a new Calendar in forms dialog
307
308       --text=STRING
309              Set the dialog text
310
311       --separator=STRING
312              Set output separator character
313
314       --forms-date-format=PATTERN
315              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
316
317       Miscellaneous options
318
319       -?, --help
320              Show summary of options.
321
322       --about
323              Display an about dialog.
324
325       --version
326              Show version of program.
327
328       Also the standard GTK+ options are accepted. For more information about the GTK+ options, execute following command.
329
330              zenity --help-gtk
331
332ENVIRONMENT
333       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.
334
335EXAMPLES
336       Display a file selector with the title Select a file to remove. The file selected is returned on standard output.
337
338              zenity  --title="Select a file to remove" --file-selection
339
340       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.
341
342              zenity  --title "Select Host" --entry --text "Select the host you would like to flood-ping"
343
344       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.
345
346              zenity  --question --title "Alert"  --text "Microsoft Windows has been found! Would you like to remove it?"
347
348       Show the search results in a list dialog with the title Search Results and the text Finding all header files....
349
350              find . -name '*.h' | zenity --list --title "Search Results" --text "Finding all header files.." --column "Files"
351
352       Show a notification in the message tray
353
354              zenity --notification --window-icon=update.png --text "System update necessary!"
355
356       Display a weekly shopping list in a check list dialog with Apples and Oranges pre selected
357
358              zenity --list --checklist --column "Buy" --column "Item" TRUE Apples TRUE Oranges FALSE Pears FALSE Toothpaste
359
360       Display a progress dialog while searching for all the postscript files in your home directory
361
362       find $HOME -name '*.ps' | zenity --progress --pulsate
363
364AUTHOR
365       Zenity was written by Glynn Foster <[email protected]>.
366
367       This manual page was written by Ross Burton <[email protected]>.
368
369SEE ALSO
370       gdialog(1), dialog(1)

色々と叩いてみる

--listのオプションを指定してみる。

1~ $ zenity --list --title=Texpander --width=275 --height=400 --column="no" --column="Abbreviations" --separator="|" --text=STRING --multiple --print-column=ALL 1 "aaa" 2 "bbb"

どうでしょうか。

texpander

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

texpander

--separator="|"--multipleを指定して、2つを選択してるためか、出力はこんな感じです。

1~ $ 1|aaa|2|bbb

別のオプションを指定してみる。

--editable--column="param"を追加してみました。

1~ $ 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" ""

texpander

1~ $ 1|aaa|引数を

やろうと思えば、任意の文字列を渡せそうです。

zenityは、list以外にも色々とあるので、いじってみます。

Tags:
comments powered by Disqus