learn-lua

Note: This is a very basic lua tutorial to get you familiar with NvChad config

Printing

  1. print("Hi")

Comments

  1. -- A comment
  2. print("Hi") -- another comment

Variables

  1. -- Different types
  2. local x = 10 -- number
  3. local name = "Sid" -- string
  4. local isAlive = true -- boolean
  5. local a = nil --no value or invalid value

Numbers

  1. -- Examples
  2. local a = 1
  3. local b = 2
  4. print(a + b) -- 3
  1. -- increment
  2. local level = 1
  3. level = level + 1
  4. print(level) -- 2

Strings

  1. -- Concatenate strings
  2. local phrase = "I am"
  3. local name = "Sid"
  4. print(phrase .. " " .. name) -- I am Sid
  5. -- Strings and numbers
  6. local name = "NvChad"
  7. print(name .. " v" .. 1.0) -- NvChad v1.0

Boolean

  1. local isAlive = true
  2. print(isAlive) -- true
  3. isAlive = false
  4. print(isAlive) -- false

Comparison Operators

  • \== equality
  • < less than
  • > greater than
  • <= less than or equal to
  • >\= greater than or equal to
  • ~= inequality

Conditional Statements

  1. -- Number comparisons
  2. local age = 10
  3. if age > 18 then
  4. print("over 18") -- this will not be executed
  5. end
  6. -- elseif and else
  7. age = 20
  8. if age > 18 then
  9. print("over 18")
  10. elseif age == 18 then
  11. print("18 huh")
  12. else
  13. print("kiddo")
  14. end
  1. -- Boolean comparison
  2. local isAlive = true
  3. if isAlive then
  4. print("Be grateful!")
  5. end
  6. -- String comparisons
  7. local name = "sid"
  8. if name ~= "sid" then
  9. print("not sid")
  10. end

Combining Statements

  1. local age = 22
  2. if age == 10 and x > 0 then -- both should be true
  3. print("kiddo!")
  4. elseif x == 18 or x > 18 then -- 1 or more are true
  5. print("over 18")
  6. end
  7. -- result: over 18

Invert Value

you can also invert a value with the not keyword

  1. local x = 18
  2. if not x == 18 then
  3. print("kiddo!")
  4. end

Functions

  1. function num(a)
  2. print(a)
  3. end
  4. or
  5. local num = function(a)
  6. print(a)
  7. end
  8. num(5)
  9. -- result : 5
  1. -- multiple parameters
  2. function sum(a, b)
  3. local result = a + b
  4. print(result)
  5. end

Scope

Variables have different scopes. Once the end of the scope is reached the values in that scope are no longer accessable

  1. function foo()
  2. local n = 10
  3. end
  4. print(n) -- nil , n isn't accessible outside foo()

Loops

Different ways to make a loop

  1. -- while loop
  2. local i = 0
  3. while i <= 3 do
  4. print("hi")
  5. i = i + 1
  6. end
  7. OR
  8. -- for loop
  9. for i = 0, 3 do
  10. print("hi")
  11. i = i + 1
  12. end
  13. -- result
  14. hi
  15. hi
  16. hi

Tables

  1. -- Basic table
  2. local colors = { "red", "green", "blue" }
  3. print(colors[1]) -- red
  4. print(colors[2]) -- green
  5. print(colors[3]) -- blue
  6. -- Use a loop to iterate though the table
  7. for i = 1, #colors do
  8. print(colors[i])
  9. end

Two Dimensional Table

  1. -- Tables within tables
  2. local data = {
  3. { "billy", 12 },
  4. { "john", 20 },
  5. }
  6. for i = 1, #data do
  7. print(data[i][1] .. " is " .. data[i][2] .. " years old")
  8. end

Modules

Include code from other files

  1. require("otherfile")
  • Credits - Modified version of this guide