|
1 | | -# canter |
2 | | -lightweight neovim code runner plugin that works with Snacks.terminal |
| 1 | +# canter.nvim |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +lightweight neovim code runner plugin that works with Snacks.nvim terminal |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +- Neovim (>= 0.7) |
| 10 | +- [Snacks.nvim](https://github.com/folke/snacks.nvim) (>= 2.11.0) |
| 11 | + - needed for Terminal |
| 12 | +- [which-key](https://github.com/folke/which-key.nvim) (>= 3.15.0) |
| 13 | + - only needed for custom leader key menu group name |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +### With [lazy.nvim](https://github.com/folke/lazy.nvim) |
| 18 | + |
| 19 | +```lua |
| 20 | +{ |
| 21 | + "dchae/canter.nvim", |
| 22 | + opts = {} |
| 23 | +}, |
| 24 | +``` |
| 25 | + |
| 26 | +### Other package managers |
| 27 | + |
| 28 | +Install normally, and add this line to your `init.lua`: |
| 29 | + |
| 30 | +```lua |
| 31 | +require("canter.nvim").setup() |
| 32 | +``` |
| 33 | + |
| 34 | +## Configuration |
| 35 | + |
| 36 | +Pass your config table into the `setup()` function or `opts` if you use lazy.nvim. |
| 37 | + |
| 38 | +### Options |
| 39 | + |
| 40 | +#### Default options |
| 41 | +```lua |
| 42 | +opts = { |
| 43 | + -- File extension to runner/interpreter mapping |
| 44 | + runners = {}, |
| 45 | + |
| 46 | + -- Terminal configuration (passed to Snacks.nvim) |
| 47 | + Snacks_terminal_opts = { |
| 48 | + win = { |
| 49 | + position = "bottom", |
| 50 | + relative = "editor" |
| 51 | + }, |
| 52 | + interactive = false |
| 53 | + }, |
| 54 | + |
| 55 | + -- Default keymaps (can be overridden) |
| 56 | + keymaps = { |
| 57 | + ["<leader><cr><cr>"] = { |
| 58 | + cmd = ":CanterRun<CR>", |
| 59 | + desc = "Run current file (Auto)" |
| 60 | + }, |
| 61 | + ["<leader><cr>w"] = { |
| 62 | + cmd = ":CanterWait<CR>", |
| 63 | + desc = "Run current file (Wait)" |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +**NOTE** - does not come with runners by default, you must add your own. |
| 70 | + |
| 71 | +- `runners`: table `([file_extension] = runner/interpreter)` |
| 72 | +- `Snacks_terminal_opts`: table of options passed to Snacks.nvim terminal |
| 73 | + - `win`: window positioning options |
| 74 | + - `interactive`: whether terminal starts in interactive mode |
| 75 | +- `keymaps`: table of keybindings and their descriptions |
| 76 | + |
| 77 | +#### Example config |
| 78 | +```lua |
| 79 | +opts = { |
| 80 | + runners = { |
| 81 | + ["js"] = "node", |
| 82 | + ["rb"] = "ruby", |
| 83 | + ["py"] = "python" |
| 84 | + }, |
| 85 | + Snacks_terminal_opts = { |
| 86 | + win = { |
| 87 | + position = "right", -- Change terminal position to right |
| 88 | + relative = "editor" |
| 89 | + }, |
| 90 | + interactive = true -- Always start in interactive mode |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Usage |
| 96 | + |
| 97 | +"Run current file (Auto)" |
| 98 | + |
| 99 | +- if file contains a shebang on the first line, the plugin will attempt to: |
| 100 | + 1. make the file executable via `chmod` |
| 101 | + 2. execute the current file |
| 102 | +- else, if the file has a corresponding runner |
| 103 | + 1. execute the current file via its runner in `Snacks.terminal` |
| 104 | + |
| 105 | +"Run current file (Wait)" |
| 106 | + |
| 107 | +- same as above, but stops before actually executing so you can add flags or confirm the command before pressing enter. |
| 108 | + - necessarily, the terminal is interactive by default in this mode. |
| 109 | + |
| 110 | +### Shebang example with node |
| 111 | +`test.js` |
| 112 | +```js |
| 113 | +#!/usr/bin/env node |
| 114 | + |
| 115 | +console.log("Hello, world!"); |
| 116 | +// "Hello, world!" |
| 117 | +``` |
| 118 | + |
| 119 | +### Default Keybinds |
| 120 | + |
| 121 | +All keybinds can be customized in the config. The defaults are: |
| 122 | + |
| 123 | +- `<Leader><CR><CR>`: Run current file (Auto) |
| 124 | + - executes current file in terminal |
| 125 | + - default behaviour is non-interactive; file will run and then any key will dismiss terminal |
| 126 | +- `<Leader><CR>w`: Run current file (Wait) |
| 127 | + - loads current file run command in terminal |
| 128 | + - default behaviour is interactive |
| 129 | + |
| 130 | +To customize keybinds, modify the `keymaps` table in your config: |
| 131 | + |
| 132 | +```lua |
| 133 | +opts = { |
| 134 | + keymaps = { |
| 135 | + -- Override default run binding |
| 136 | + ["<leader>r"] = { |
| 137 | + cmd = ":CanterRun<CR>", |
| 138 | + desc = "Run current file" |
| 139 | + }, |
| 140 | + -- Add new binding |
| 141 | + ["<leader>rw"] = { |
| 142 | + cmd = ":CanterWait<CR>", |
| 143 | + desc = "Run and wait" |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +## Roadmap |
| 150 | + |
| 151 | +- should work with vsplit terminal when Snacks is not available |
| 152 | +- automatically scan and resolve runners for a given file extension |
| 153 | +- better support for runner flags |
| 154 | + |
| 155 | +## Credits |
| 156 | +- plugin inspired by keymap script by u/linkarzu on r/neovim |
0 commit comments