You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+171-8
Original file line number
Diff line number
Diff line change
@@ -142,7 +142,7 @@ Nice visual cheatsheet from the [article](https://clementc.github.io/blog/2018/0
142
142
143
143
### Bang(!) - The History Expansion
144
144
Bash also has some handy features that use the ! (bang) to allow you to do some funky stuff with bash commands.<br>
145
-
General notation is `'![event][:word[:modifier]]'`.<br>
145
+
General notation is `'![event][:word[:modifier[:modifier]...]]'`.<br>
146
146
You may ommit word separator `':'`, if the word designator begins with a `'^'`, `'$'`, `'*'`, `'-'`, or `'%'`.<br>
147
147
If a word designator is supplied without an event specification, the previous command is used as the event.<br>
148
148
After the optional word designator, you can add a sequence of one or more modifiers, each preceded by a `':'`.<br>
@@ -164,6 +164,15 @@ After the optional word designator, you can add a sequence of one or more modifi
164
164
<td>Refer to command line <i><b>n</b></i>.</td>
165
165
<td>
166
166
<pre>
167
+
$ history
168
+
1 echo foo bar baz
169
+
2 history
170
+
$ !1
171
+
#Print command that will be saved in history
172
+
#+and executed
173
+
echo foo bar baz
174
+
#Actual execution
175
+
foo bar baz
167
176
</pre>
168
177
</td>
169
178
</tr>
@@ -172,6 +181,14 @@ After the optional word designator, you can add a sequence of one or more modifi
172
181
<td>Refer to the command <i><b>n</b></i> lines back.</td>
173
182
<td>
174
183
<pre>
184
+
$ history
185
+
1 echo foo
186
+
2 echo bar
187
+
3 echo baz
188
+
4 history
189
+
$ !-3
190
+
echo bar
191
+
bar
175
192
</pre>
176
193
</td>
177
194
</tr>
@@ -180,6 +197,11 @@ After the optional word designator, you can add a sequence of one or more modifi
180
197
<td>Refer to the previous command. This is a synonym for ‘!-1’.</td>
181
198
<td>
182
199
<pre>
200
+
$ echo foo bar baz
201
+
foo bar baz
202
+
$ !!
203
+
echo foo bar baz
204
+
foo bar baz
183
205
</pre>
184
206
</td>
185
207
</tr>
@@ -188,6 +210,13 @@ After the optional word designator, you can add a sequence of one or more modifi
188
210
<td>Refer to the most recent command preceding the current position in the history list starting with <i><b>string</b></i>.</td>
189
211
<td>
190
212
<pre>
213
+
$printf '%s\n' foo
214
+
foo
215
+
$ echo bar
216
+
bar
217
+
$ !pri
218
+
printf '%s\n' foo
219
+
foo
191
220
</pre>
192
221
</td>
193
222
</tr>
@@ -196,14 +225,30 @@ After the optional word designator, you can add a sequence of one or more modifi
196
225
<td>Refer to the most recent command preceding the current position in the history list containing <i><b>string</b></i>. The trailing ‘?’ may be omitted if the <i><b>string</b></i> is followed immediately by a newline.</td>
197
226
<td>
198
227
<pre>
228
+
$printf '%s\n' foo
229
+
foo
230
+
$ echo bar
231
+
bar
232
+
$ !?ntf
233
+
printf '%s\n' foo
234
+
foo
235
+
$ !?bar
236
+
echo bar
237
+
bar
199
238
</pre>
200
239
</td>
201
240
</tr>
202
241
<tr>
203
242
<td>^<i>string1</i>^<i>tring2</i>^</td>
204
243
<td>Quick Substitution. Repeat the last command, replacing <i><b>string1</b></i> with <i><b>string2</b></i>. Equivalent to `!!:s/string1/string2`.</td>
205
244
<td>
245
+
For more info, refer to `s/old/new/` in <b>Modifiers</b> section.
206
246
<pre>
247
+
$ echo foo
248
+
foo
249
+
$ ^echo^printf '%s\n'^
250
+
printf '%s\n' foo
251
+
foo
207
252
</pre>
208
253
</td>
209
254
</tr>
@@ -212,6 +257,13 @@ After the optional word designator, you can add a sequence of one or more modifi
212
257
<td>Repeat entire command line before this event.</td>
213
258
<td>
214
259
<pre>
260
+
$ echo foo; echo bar; !#echo baz
261
+
echo foo; echo bar; echo foo; echo bar; echo baz
262
+
foo
263
+
bar
264
+
foo
265
+
bar
266
+
baz
215
267
</pre>
216
268
</td>
217
269
</tr>
@@ -223,6 +275,11 @@ After the optional word designator, you can add a sequence of one or more modifi
223
275
<td>The 0th word. For many applications, this is the command word.</td>
224
276
<td>
225
277
<pre>
278
+
$ echo foo
279
+
foo
280
+
$ !:0
281
+
echo
282
+
226
283
</pre>
227
284
</td>
228
285
</tr>
@@ -231,6 +288,11 @@ After the optional word designator, you can add a sequence of one or more modifi
231
288
<td>The <i><b>n</b></i>th word.</td>
232
289
<td>
233
290
<pre>
291
+
$ echo foo bar baz
292
+
foo bar baz
293
+
$ echo !:2
294
+
echo bar
295
+
bar
234
296
</pre>
235
297
</td>
236
298
</tr>
@@ -239,14 +301,24 @@ After the optional word designator, you can add a sequence of one or more modifi
239
301
<td>The first argument; that is, word 1.</td>
240
302
<td>
241
303
<pre>
304
+
$ echo foo bar baz
305
+
foo bar baz
306
+
$ echo !^
307
+
echo foo
308
+
foo
242
309
</pre>
243
310
</td>
244
311
</tr>
245
312
<tr>
246
-
<td>\$</td>
313
+
<td><pre>$</pre></td>
247
314
<td>The last argument.</td>
248
315
<td>
249
316
<pre>
317
+
$ echo foo bar baz
318
+
foo bar baz
319
+
$ echo !$
320
+
echo baz
321
+
baz
250
322
</pre>
251
323
</td>
252
324
</tr>
@@ -255,6 +327,22 @@ After the optional word designator, you can add a sequence of one or more modifi
255
327
<td>The word matched by the most recent `?string?` search</td>
256
328
<td>
257
329
<pre>
330
+
$ echo foo
331
+
foo
332
+
$ printf '%s\n' bar
333
+
bar
334
+
$ !?ch
335
+
echo foo
336
+
foo
337
+
$ !% baz
338
+
echo baz
339
+
baz
340
+
$ !?bar
341
+
printf '%s\n' bar
342
+
bar
343
+
$ echo !%
344
+
echo bar
345
+
bar
258
346
</pre>
259
347
</td>
260
348
</tr>
@@ -263,6 +351,14 @@ After the optional word designator, you can add a sequence of one or more modifi
263
351
<td>A range of words; `-y` abbreviates `0-y`.</td>
264
352
<td>
265
353
<pre>
354
+
$ echo foo bar baz
355
+
foo bar baz
356
+
$ echo !:2-3
357
+
echo bar baz
358
+
bar baz
359
+
$ !:-1
360
+
echo bar
361
+
bar
266
362
</pre>
267
363
</td>
268
364
</tr>
@@ -271,6 +367,13 @@ After the optional word designator, you can add a sequence of one or more modifi
271
367
<td>All of the words, except the 0th. This is a synonym for `1-$`. It is not an error to use `*` if there is just one word in the event - the empty string is returned in that case.</td>
272
368
<td>
273
369
<pre>
370
+
$ echo foo bar baz
371
+
foo bar baz
372
+
$ printf '%s\n' !*
373
+
printf '%s\n' foo bar baz
374
+
foo
375
+
bar
376
+
baz
274
377
</pre>
275
378
</td>
276
379
</tr>
@@ -279,6 +382,12 @@ After the optional word designator, you can add a sequence of one or more modifi
279
382
<td>Abbreviates `x-$`</td>
280
383
<td>
281
384
<pre>
385
+
$ echo foo bar baz
386
+
foo bar baz
387
+
$ printf '%s\n' !:2*
388
+
printf '%s\n' bar baz
389
+
bar
390
+
baz
282
391
</pre>
283
392
</td>
284
393
</tr>
@@ -287,6 +396,13 @@ After the optional word designator, you can add a sequence of one or more modifi
287
396
<td>Abbreviates `x-$` like `x*`, but omits the last word.</td>
288
397
<td>
289
398
<pre>
399
+
$ echo foo bar baz
400
+
foo bar baz
401
+
$ printf '%s\n' !:0-
402
+
printf '%s\n' echo foo bar
403
+
echo
404
+
foo
405
+
bar
290
406
</pre>
291
407
</td>
292
408
</tr>
@@ -295,41 +411,64 @@ After the optional word designator, you can add a sequence of one or more modifi
295
411
</tr>
296
412
<tr>
297
413
<td>p</td>
298
-
<td>Print the new command but do not execute it.</td>
414
+
<td>Print the new command but do not execute it.<br>Printed command is saved in history, so you can use <kbd>Ctrl+p</kbd> to re-enter it in current prompt.</td>
299
415
<td>
300
416
<pre>
417
+
$ echo foo bar baz
418
+
foo bar baz
419
+
$ !:p
420
+
#Printed, but not executed
421
+
echo foo bar baz
422
+
$ !:*:p
423
+
foo bar baz
301
424
</pre>
302
425
</td>
303
426
</tr>
304
427
<tr>
305
428
<td>h</td>
306
-
<td>Remove a trailing pathname component, leaving only the head (Actually, remove all after last `/`).</td>
429
+
<td>Remove a trailing pathname component, leaving only the head (Actually, remove all after last `/`, including).</td>
307
430
<td>
308
431
<pre>
432
+
$ echo foo /example/path/bar.txt baz
433
+
foo /example/path/bar.txt baz
434
+
$ !:p:h
435
+
echo foo /example/path
309
436
</pre>
310
437
</td>
311
438
</tr>
312
439
<tr>
313
440
<td>t</td>
314
-
<td>Remove all leading pathname components, leaving the tail (Actually, remove all before last `/`).</td>
441
+
<td>Remove all leading pathname components, leaving the tail (Actually, remove all before last `/`, including).</td>
315
442
<td>
316
443
<pre>
444
+
$ echo foo /example/path/bar.txt baz
445
+
foo /example/path/bar.txt baz
446
+
$ !:p:t
447
+
bar.txt baz
317
448
</pre>
318
449
</td>
319
450
</tr>
320
451
<tr>
321
452
<td>r</td>
322
-
<td>Remove a trailing suffix of the form `.suffix`, leaving the basename (Actually, remove all after last `.`).</td>
453
+
<td>Remove a trailing suffix of the form `.suffix`, leaving the basename (Actually, remove all after last `.`, including).</td>
323
454
<td>
324
455
<pre>
456
+
$ echo foo /example/path/bar.txt baz
457
+
foo /example/path/bar.txt baz
458
+
$ !:p:r
459
+
echo foo /example/path/bar
325
460
</pre>
326
461
</td>
327
462
</tr>
328
463
<tr>
329
464
<td>e</td>
330
-
<td>Remove all but the trailing suffix (Actually, remove all before last `.`).</td>
465
+
<td>Remove all but the trailing suffix (Actually, remove all before last `.`, including).</td>
331
466
<td>
332
467
<pre>
468
+
$ echo foo /example/path/bar.txt baz
469
+
foo /example/path/bar.txt baz
470
+
$ !:p:e
471
+
txt baz
333
472
</pre>
334
473
</td>
335
474
</tr>
@@ -338,6 +477,10 @@ After the optional word designator, you can add a sequence of one or more modifi
338
477
<td>Quote the substituted words, escaping further substitutions.</td>
339
478
<td>
340
479
<pre>
480
+
$ echo foo 'bar baz'
481
+
foo bar baz
482
+
$ !:p:q
483
+
'echo foo '\'bar baz'\'''
341
484
</pre>
342
485
</td>
343
486
</tr>
@@ -346,6 +489,10 @@ After the optional word designator, you can add a sequence of one or more modifi
346
489
<td>Quote the substituted words as with ‘q’, but break into words at spaces, tabs, and newlines.</td>
347
490
<td>
348
491
<pre>
492
+
$ echo foo 'bar baz'
493
+
foo bar baz
494
+
$ !:p:x
495
+
'echo' 'foo' ''\'bar' 'baz'\'''
349
496
</pre>
350
497
</td>
351
498
</tr>
@@ -354,6 +501,10 @@ After the optional word designator, you can add a sequence of one or more modifi
354
501
<td>Substitute <i><b>new</b></i> for the first occurrence of <i><b>old</b></i> in the event line. Any delimiter may be used in place of `/`. The delimiter may be quoted in <i><b>old</b></i> and <i><b>new</b></i> with a single backslash. If `&` appears in <i><b>new</b></i>, it is replaced by <i><b>old</b></i>. A single backslash will quote the `&`. The final delimiter is optional if it is the last character on the input line.</td>
355
502
<td>
356
503
<pre>
504
+
$ echo foo bar
505
+
foo bar
506
+
$ !:p:s/foo/baz
507
+
echo baz bar
357
508
</pre>
358
509
</td>
359
510
</tr>
@@ -362,6 +513,14 @@ After the optional word designator, you can add a sequence of one or more modifi
362
513
<td>Repeat the previous substitution.</td>
363
514
<td>
364
515
<pre>
516
+
$ echo foo bar
517
+
foo bar
518
+
$ !:p:s/foo/baz
519
+
echo baz bar
520
+
$ printf '%s\n' foo
521
+
foo
522
+
$ !:p:&
523
+
printf '%s\n' baz
365
524
</pre>
366
525
</td>
367
526
</tr>
@@ -370,13 +529,17 @@ After the optional word designator, you can add a sequence of one or more modifi
370
529
<td>Cause changes to be applied over the entire event line. Used in conjunction with `s`, as in gs/old/new/, or with `&`.</td>
371
530
<td>
372
531
<pre>
532
+
$ echo foo bar foo
533
+
foo bar foo
534
+
$ !:p:gs/foo/baz
535
+
echo baz bar baz
373
536
</pre>
374
537
</td>
375
538
</tr>
376
539
<tr>
377
540
<td>G</td>
378
541
<td>Apply the following ‘s’ modifier once to each word in the event.</td>
0 commit comments