Debian Codegraph @

docs/features/shortcuts_aliases_macros.md

raw symbols SCIP
1 # Shortcuts, Aliases, and Macros
2
3 ## Shortcuts
4
5 Command shortcuts for long command names and common commands can be more convenient for your users.
6 Shortcuts are used without a space separating them from their arguments, like `!ls`. By default, the
7 following shortcuts are defined:
8
9 - **`?`** - help
10 - **`!`** - shell: run as OS-level command
11 - **`@`** - run script file
12 - **`@@`** - run script file; filename is relative to current script location
13
14 To define more shortcuts, start with the [cmd2.DEFAULT_SHORTCUTS][] constant, which is a dictionary,
15 and then add more shortcuts to it by updating it with a dictionary of additional shortcuts in the
16 format `{'shortcut': 'command_name'}` where you omit `do_` from the command name:
17
18 ```py
19 class App(Cmd):
20 def __init__(self):
21 shortcuts = cmd2.DEFAULT_SHORTCUTS
22 shortcuts.update({'*': 'sneeze', '~': 'squirm'})
23 cmd2.Cmd.__init__(self, shortcuts=shortcuts)
24 ```
25
26 !!! warning
27
28 Shortcuts need to be created by updating the `shortcuts` dictionary attribute before calling the `cmd2.Cmd` super class `__init__()` method. Moreover, that super class init method needs to be called after updating the `shortcuts` attribute.
29
30 This warning applies in general to many other attributes which are not settable at runtime.
31
32 !!! note
33
34 Command, alias, and macro names cannot start with a shortcut
35
36 ## Aliases
37
38 In addition to shortcuts, `cmd2` provides an alias feature via the `alias` command. Aliases work
39 like aliases in the Bash shell.
40
41 The syntax to create an alias is: `alias create name command [args]`, e.g.
42 `alias create ls !ls -lF`.
43
44 Redirectors and pipes should be quoted in the alias definition to prevent the `alias create` command
45 from being redirected:
46
47 alias create save_results print_results ">" out.txt
48
49 Tab completion recognizes an alias, and completes as if the aliased command was on the command line.
50
51 For more details run: `help alias create`
52
53 Use `alias list` to see all or some of your aliases. The output of this command displays your
54 aliases in a format that can be used to create them. Therefore you can place this output in a `cmd2`
55 startup script to recreate your aliases each time you start the application
56
57 > Ex: `alias list`
58
59 For more details run: `help alias list`
60
61 Use `alias delete` to remove aliases
62
63 For more details run: `help alias delete`
64
65 !!! note
66
67 Aliases cannot have the same name as a command or macro
68
69 ## Macros
70
71 `cmd2` provides a feature that is similar to aliases called macros. The major difference between
72 macros and aliases is that macros can contain argument placeholders. Arguments are expressed when
73 creating a macro using `{#}` notation, where `{1}` means the first argument.
74
75 The following creates a macro called `my_macro` that expects two arguments:
76
77 macro create my_macro make_dinner -meat {1} -veggie {2}
78
79 When the macro is called, the provided arguments are substituted and the assembled command is run.
80 For example:
81
82 my_macro beef broccoli ---> make_dinner -meat beef -veggie broccoli
83
84 Similar to aliases, pipes and redirectors need to be quoted in the definition of a macro:
85
86 macro create lc !cat "{1}" "|" less
87
88 To use the literal string `{1}` in your command, escape it this way: `{{1}}`.
89
90 Since macros don't resolve until after you press `<Enter>`, their arguments tab complete as paths.
91 You can change this default behavior by overriding `Cmd.macro_arg_complete()` to implement custom
92 tab completion for macro arguments.
93
94 For more details run: `help macro create`
95
96 The macro command has `list` and `delete` subcommands that function identically to the alias
97 subcommands of the same name. Like aliases, macros can be created via a `cmd2` startup script to
98 preserve them across application sessions.
99
100 For more details on listing macros run: `help macro list`
101
102 For more details on deleting macros run: `help macro delete`
103
104 !!! note
105
106 Macros cannot have the same name as a command or alias