|
1 | 1 | [Lua, a misunderstood language • AndreGarzia.com](https://andregarzia.com/2021/01/lua-a-misunderstood-language.html) |
| 2 | + |
| 3 | +[LÖVE - Free 2D Game Engine](https://love2d.org/) |
| 4 | + |
| 5 | +# Lua in Neovim |
| 6 | + |
| 7 | +[Lua - Neovim docs](https://neovim.io/doc/user/lua.html) |
| 8 | + |
| 9 | +[Lua-guide - Neovim docs](https://neovim.io/doc/user/lua-guide.html) |
| 10 | + |
| 11 | +Run Lua scripts with Neovim: |
| 12 | + |
| 13 | +```shell-session |
| 14 | +$ nvim -l script.lua |
| 15 | +``` |
| 16 | + |
| 17 | +Run a Lua script inside Neovim with: |
| 18 | + |
| 19 | +``` |
| 20 | +:luafile script.lua |
| 21 | +``` |
| 22 | + |
| 23 | +Source a Lua script with: |
| 24 | + |
| 25 | +``` |
| 26 | +:source script.lua |
| 27 | +``` |
| 28 | + |
| 29 | +Run current file: |
| 30 | + |
| 31 | +``` |
| 32 | +:luafile % |
| 33 | +``` |
| 34 | + |
| 35 | +Run a Lua command over each line in a file with: |
| 36 | + |
| 37 | +``` |
| 38 | +:luado return print("line: ", line) |
| 39 | +``` |
| 40 | + |
| 41 | +Run a lua command with `vim.print`: |
| 42 | + |
| 43 | +``` |
| 44 | +lua =vim.opt.wildignore |
| 45 | +``` |
| 46 | + |
| 47 | +## Vimscript |
| 48 | + |
| 49 | +Run a Vimscript command: |
| 50 | + |
| 51 | +``` |
| 52 | +vim.cmd("colorscheme habamax") |
| 53 | +``` |
| 54 | + |
| 55 | +Run Vimscript functions: |
| 56 | + |
| 57 | +``` |
| 58 | +vim.fn |
| 59 | +``` |
| 60 | + |
| 61 | +## Strings |
| 62 | + |
| 63 | +String literal with `[[]]`: |
| 64 | + |
| 65 | +``` |
| 66 | +vim.cmd([[%s/\Vfoo/bar/g]]) |
| 67 | +``` |
| 68 | + |
| 69 | +Can use multiple lines: |
| 70 | + |
| 71 | +``` |
| 72 | +vim.cmd([[ |
| 73 | + highlight Error guibg=red |
| 74 | + highlight link Warning Error |
| 75 | +]]) |
| 76 | +``` |
| 77 | + |
| 78 | +## Tables |
| 79 | + |
| 80 | +Object or container data structure. Represent both list and maps. |
| 81 | + |
| 82 | +## Closures |
| 83 | + |
| 84 | +Each scope is a closure: |
| 85 | + |
| 86 | +- functions, |
| 87 | +- modules, |
| 88 | +- `do` blocks. |
| 89 | + |
| 90 | +## Coroutines |
| 91 | + |
| 92 | +Stackful coroutines enable multi-threading, generators & control for both Lua & Neovim. |
| 93 | + |
| 94 | +## Looping |
| 95 | + |
| 96 | +Loop over list: |
| 97 | + |
| 98 | +```lua |
| 99 | +local fruits = { "apple", "banana", "cherry" } |
| 100 | +for index, value in ipairs(fruits) do |
| 101 | + print(index, value) |
| 102 | +end |
| 103 | +``` |
| 104 | + |
| 105 | +```lua |
| 106 | +local colors = { |
| 107 | + red = "#FF0000", |
| 108 | + green = "#00FF00", |
| 109 | + blue = "#0000FF" |
| 110 | +} |
| 111 | +for key, value in pairs(colors) do |
| 112 | + print(key, value) |
| 113 | +end |
| 114 | +``` |
| 115 | + |
| 116 | +## Functions |
| 117 | + |
| 118 | +Running a function without an argument makes that argument `nil`: |
| 119 | + |
| 120 | +```lua |
| 121 | +local func = function(a, b) |
| 122 | + print("A: ", a) |
| 123 | + print("B: ", b) |
| 124 | +end |
| 125 | + |
| 126 | +func() |
| 127 | +func(1) |
| 128 | +func(1, 2) |
| 129 | +``` |
| 130 | + |
| 131 | +You can expand out keywords: |
| 132 | + |
| 133 | +```lua |
| 134 | +local opts_func = function(opts) |
| 135 | + print("opts_func") |
| 136 | + print("A: ", opts.a) |
| 137 | + print("B: ", opts.b) |
| 138 | +end |
| 139 | + |
| 140 | +opts_func({ a = 1, b = 2 }) |
| 141 | +``` |
| 142 | + |
| 143 | +## Regex |
| 144 | + |
| 145 | +Can use Vim regex: |
| 146 | + |
| 147 | +```lua |
| 148 | +-- %d+ = one or more digits, matches first sequence |
| 149 | +print(string.match("foo123bar123", "%d+")) |
| 150 | + |
| 151 | +-- [^%d]+ = one or more non-digit characters |
| 152 | +print(string.match("foo123bar123", "[^%d]+")) |
| 153 | + |
| 154 | +-- [abc]+ = one or more of a, b, or c |
| 155 | +print(string.match("foo123bar123", "[abc]+")) |
| 156 | + |
| 157 | +-- %.bar = literal dot and bar |
| 158 | +print(string.match("foo.bar", "%.bar")) |
| 159 | +``` |
| 160 | + |
| 161 | +## Modules |
| 162 | + |
| 163 | +Searched for under the `runtimepath.` `.` used to separate directories. |
| 164 | + |
| 165 | +`require` is used to run a module. |
| 166 | + |
| 167 | +You can see current runtime path in Neovim with: |
| 168 | + |
| 169 | +``` |
| 170 | +print(vim.o.runtimepath) |
| 171 | +``` |
| 172 | + |
| 173 | +## The Neovim Standard Library |
| 174 | + |
| 175 | +Accessible as `vim`. |
| 176 | + |
| 177 | +Get help: |
| 178 | + |
| 179 | +```command |
| 180 | +:help vim.deepcopy() |
| 181 | +``` |
| 182 | + |
| 183 | +## JSON |
| 184 | + |
| 185 | +Decode JSON: |
| 186 | + |
| 187 | +``` |
| 188 | +vim.print(vim.json.decode('{"foo": "bar"}')) |
| 189 | +``` |
| 190 | + |
| 191 | +## Options |
| 192 | + |
| 193 | +``` |
| 194 | +vim.o.number = true |
| 195 | +vim.opt.number = true |
| 196 | +
|
| 197 | +vim.opt.wildignore:append("*.o") |
| 198 | +vim.opt.wildignore:prepend("*.o") |
| 199 | +vim.opt.wildignore:remove("*.o") |
| 200 | +``` |
| 201 | + |
| 202 | +``` |
| 203 | +print(vim.o.wildignore) |
| 204 | +vim.print(vim.opt.wildignore) |
| 205 | +``` |
| 206 | + |
| 207 | +## Variables |
| 208 | + |
| 209 | +Variables can be set and read using the following wrappers, which directly correspond to their variable-scope: |
| 210 | + |
| 211 | +vim.g: global variables (g:) |
| 212 | +vim.b: variables for the current buffer (b:) |
| 213 | +vim.w: variables for the current window (w:) |
| 214 | +vim.t: variables for the current tabpage (t:) |
| 215 | +vim.v: predefined Vim variables (v:) |
| 216 | +vim.env: environment variables defined in the editor session |
| 217 | + |
| 218 | +Target buffer number 2: |
| 219 | + |
| 220 | +``` |
| 221 | +vim.b[2].myvar = 1 |
| 222 | +``` |
| 223 | + |
| 224 | +## Autocommands |
| 225 | + |
| 226 | +`vim.apt.nvim_create_autocmd` takes two arguments: |
| 227 | + |
| 228 | +- `event` - table of events to listen for, |
| 229 | +- `opts` - controls what happens when events trigger. |
| 230 | + |
| 231 | +Important options are: |
| 232 | + |
| 233 | +- `pattern` - file patterns to match, |
| 234 | +- `command` - the Vim command, |
| 235 | +- `callback` - a Lua function, |
| 236 | +- `desc` - a description. |
| 237 | + |
| 238 | +```lua |
| 239 | +vim.api.nvim_create_autocmd( |
| 240 | + {"BufEnter", "BufWinEnter"}, |
| 241 | + { |
| 242 | + pattern = {"*.c", "*.h"}, |
| 243 | + callback = function() print("Entering a C or C++ file") end |
| 244 | + }) |
| 245 | +``` |
| 246 | + |
| 247 | +The callback function will be called with a table containing info about the autocommand: |
| 248 | + |
| 249 | +- `match`: a string that matched the pattern, |
| 250 | +- `buf`: the number of the buffer the event was triggered in, |
| 251 | +- `file`: the file name of the buffer the event was triggered in, |
| 252 | +- `data`: a table with other relevant data that is passed for some events. |
| 253 | + |
| 254 | +## Autocommand Groups |
| 255 | + |
| 256 | +```lua |
| 257 | +local mygroup = vim.api.nvim_create_augroup('vimrc', { clear = true }) |
| 258 | +vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, { |
| 259 | + pattern = '*.html', |
| 260 | + group = mygroup, |
| 261 | + command = 'set shiftwidth=4', |
| 262 | +}) |
| 263 | +vim.api.nvim_create_autocmd({ 'BufNewFile', 'BufRead' }, { |
| 264 | + pattern = '*.html', |
| 265 | + group = 'vimrc', -- equivalent to group=mygroup |
| 266 | + command = 'set expandtab', |
| 267 | +}) |
| 268 | +``` |
| 269 | + |
| 270 | +## User Commands |
| 271 | + |
| 272 | +```lua |
| 273 | +vim.api.nvim_create_user_command('Test', 'echo "It works!"', {}) |
| 274 | +vim.cmd.Test() |
| 275 | +--> It works! |
| 276 | +``` |
0 commit comments