| 1 |
# Tutorial |
| 2 |
|
| 3 |
This is a brief intro to micro's configuration system that will give some |
| 4 |
simple examples showing how to configure settings, rebind keys, and use |
| 5 |
`init.lua` to configure micro to your liking. |
| 6 |
|
| 7 |
Hopefully you'll find this useful. |
| 8 |
|
| 9 |
See `> help defaultkeys` for a list an explanation of the default keybindings. |
| 10 |
|
| 11 |
### Settings |
| 12 |
|
| 13 |
In micro, your settings are stored in `~/.config/micro/settings.json`, a file |
| 14 |
that is created the first time you run micro. It is a json file which holds all |
| 15 |
the settings and their values. To change an option, you can either change the |
| 16 |
value in the `settings.json` file, or you can type it in directly while using |
| 17 |
micro. |
| 18 |
|
| 19 |
Press Ctrl-e to go to command mode, and type `set option value` (in the |
| 20 |
future, I will use `> set option value` to indicate pressing Ctrl-e). The change |
| 21 |
will take effect immediately and will also be saved to the `settings.json` file |
| 22 |
so that the setting will stick even after you close micro. |
| 23 |
|
| 24 |
You can also set options locally which means that the setting will only have |
| 25 |
the value you give it in the buffer you set it in. For example, if you have two |
| 26 |
splits open, and you type `> setlocal tabsize 2`, the tabsize will only be 2 in |
| 27 |
the current buffer. Also micro will not save this local change to the |
| 28 |
`settings.json` file. However, you can still set options locally in the |
| 29 |
`settings.json` file. For example, if you want the `tabsize` to be 2 only in |
| 30 |
Ruby files, and 4 otherwise, you could put the following in `settings.json`: |
| 31 |
|
| 32 |
```json |
| 33 |
{ |
| 34 |
"*.rb": { |
| 35 |
"tabsize": 2 |
| 36 |
}, |
| 37 |
"tabsize": 4 |
| 38 |
} |
| 39 |
``` |
| 40 |
|
| 41 |
Micro will set the `tabsize` to 2 only in files which match the glob `*.rb`. |
| 42 |
|
| 43 |
If you would like to know more about all the available options, see the |
| 44 |
`options` topic (`> help options`). |
| 45 |
|
| 46 |
### Keybindings |
| 47 |
|
| 48 |
Keybindings work in much the same way as options. You configure them using the |
| 49 |
`~/.config/micro/bindings.json` file. |
| 50 |
|
| 51 |
For example if you would like to bind `Ctrl-r` to redo you could put the |
| 52 |
following in `bindings.json`: |
| 53 |
|
| 54 |
```json |
| 55 |
{ |
| 56 |
"Ctrl-r": "Redo" |
| 57 |
} |
| 58 |
``` |
| 59 |
|
| 60 |
Very simple. |
| 61 |
|
| 62 |
You can also bind keys while in micro by using the `> bind key action` command, |
| 63 |
but the bindings you make with the command won't be saved to the |
| 64 |
`bindings.json` file. |
| 65 |
|
| 66 |
For more information about keybindings, like which keys can be bound, and what |
| 67 |
actions are available, see the `keybindings` help topic (`> help keybindings`). |
| 68 |
|
| 69 |
### Configuration with Lua |
| 70 |
|
| 71 |
If you need more power than the json files provide, you can use the `init.lua` |
| 72 |
file. Create it in `~/.config/micro`. This file is a lua file that is run when |
| 73 |
micro starts and is essentially a one-file plugin. The plugin name is |
| 74 |
`initlua`. |
| 75 |
|
| 76 |
This example will show you how to use the `init.lua` file by creating a binding |
| 77 |
to `Ctrl-r` which will execute the bash command `go run` on the current file, |
| 78 |
given that the current file is a Go file. |
| 79 |
|
| 80 |
You can do that by putting the following in `init.lua`: |
| 81 |
|
| 82 |
```lua |
| 83 |
local config = import("micro/config") |
| 84 |
local shell = import("micro/shell") |
| 85 |
|
| 86 |
function init() |
| 87 |
-- true means overwrite any existing binding to Ctrl-r |
| 88 |
-- this will modify the bindings.json file |
| 89 |
config.TryBindKey("Ctrl-r", "lua:initlua.gorun", true) |
| 90 |
end |
| 91 |
|
| 92 |
function gorun(bp) |
| 93 |
local buf = bp.Buf |
| 94 |
if buf:FileType() == "go" then |
| 95 |
-- the true means run in the foreground |
| 96 |
-- the false means send output to stdout (instead of returning it) |
| 97 |
shell.RunInteractiveShell("go run " .. buf.Path, true, false) |
| 98 |
end |
| 99 |
end |
| 100 |
``` |
| 101 |
|
| 102 |
Alternatively, you could get rid of the `TryBindKey` line, and put this line in |
| 103 |
the `bindings.json` file: |
| 104 |
|
| 105 |
```json |
| 106 |
{ |
| 107 |
"Ctrl-r": "lua:initlua.gorun" |
| 108 |
} |
| 109 |
``` |
| 110 |
|
| 111 |
For more information about plugins and the lua system that micro uses, see the |
| 112 |
`plugins` help topic (`> help plugins`). |