Compare commits
20
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58753ae801 | ||
|
|
603e7a2b7a | ||
|
|
ae26197a78 | ||
|
|
29f4436aa8 | ||
|
|
f9f6aea868 | ||
|
|
ffc4520ba9 | ||
|
|
bf0b652992 | ||
|
|
6e5fc4d1b4 | ||
|
|
64723103b6 | ||
|
|
a2d915bbd1 | ||
|
|
4fbd8fce83 | ||
|
|
7198cb031a | ||
|
|
fbcdd3af0c | ||
|
|
12b0d3d8b8 | ||
|
|
9c6d49cd5d | ||
|
|
fff9a0e70e | ||
|
|
ecbc266165 | ||
|
|
5dd9b59b90 | ||
|
|
1768a69cf1 | ||
|
|
35e45b9fd5 |
@@ -0,0 +1,391 @@
|
||||
# Bash Cheat Sheet
|
||||
|
||||
## Conditionals
|
||||
|
||||
```bash
|
||||
if [[ -z "$string" ]]; then
|
||||
echo "String is empty"
|
||||
elif [[ -n "$string" ]]; then
|
||||
echo "String is not empty"
|
||||
fi
|
||||
```
|
||||
|
||||
### String conditions
|
||||
┌────────────────────────┬──────────────────┐
|
||||
│ `[[ -z STRING ]]` │ Empty string │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ -n STRING ]]` │ Not empty string │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ STRING == STRING ]]` │ Equal │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ STRING != STRING ]]` │ Not Equal │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ STRING =~ STRING ]]` │ Regex │
|
||||
└────────────────────────┴──────────────────┘
|
||||
|
||||
### Number conditions
|
||||
┌────────────────────────┬───────────────────────┐
|
||||
│ `[[ NUM1 -eq NUM2 ]]` │ Equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -ne NUM2 ]]` │ Not Equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -lt NUM2 ]]` │ Less than │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -le NUM2 ]]` │ Less than or equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -gt NUM2 ]]` │ Greater than │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -ge NUM2 ]]` │ Greater than or equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `(( NUM < NUM ))` │ Numeric conditions │
|
||||
└────────────────────────┴───────────────────────┘
|
||||
|
||||
### File conditions
|
||||
┌───────────────────────┬─────────────────────────┐
|
||||
│ `[[ -e FILE ]]` │ Exists │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -r FILE ]]` │ Readable │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -h FILE ]]` │ Symlink │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -d FILE ]]` │ Directory │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -w FILE ]]` │ Writable │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -s FILE ]]` │ Size is > 0 bytes │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -f FILE ]]` │ File │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -x FILE ]]` │ Executable │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ FILE1 -nt FILE2 ]]` │ 1 is more recent than 2 │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ FILE1 -ot FILE2 ]]` │ 2 is more recent than 1 │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ FILE1 -ef FILE2 ]]` │ Same files │
|
||||
└───────────────────────┴─────────────────────────┘
|
||||
|
||||
### Other conditions
|
||||
┌────────────────────┬────────────────────┐
|
||||
│ `[[ -o noclobber ]]` │ Option enabled │
|
||||
├────────────────────┼────────────────────┤
|
||||
│ `[[ ! EXPR ]]` │ Not │
|
||||
├────────────────────┼────────────────────┤
|
||||
│ `[[ X && Y ]]` │ And │
|
||||
├────────────────────┼────────────────────┤
|
||||
│ `[[ X || Y ]]` │ Or │
|
||||
└────────────────────┴────────────────────┘
|
||||
|
||||
## Parameter manipulation
|
||||
┌─────────────────────────────────┬────────────────────────────────────────────────────────────────┐
|
||||
│ `${foo}` │ Variable │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo/from/to}` │ Replace first match │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo/#from/to}`/`${foo/%from/to}` │ Replace first match anchored to start (default) or end │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo//from/to}` │ Replace all │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:start:length}` │ Slice from `start` to `length` (both optional and can be negative) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo%suffix}`/`${foo%%suffix}` │ Remove `suffix` (ungreedy/greedy) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo#prefix}`/`${foo##prefix}` │ Remove `prefix` (ungreedy/greedy) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${#foo}` │ Length of `foo` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo,}` │ Lowercase first letter │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo,,}` │ Lowercase all letters │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo^}` │ Uppercase first letter │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo^^}` │ Uppercase all letters │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:-val}` │ Use `val` if `foo` is unset or null │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:=val}` │ Set `foo` to `val` if unset or null │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:+val}` │ Use `val` if `foo` **is** set and not null │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:?message}` │ Print `message` and exit if `foo` is unset or null │
|
||||
└─────────────────────────────────┴────────────────────────────────────────────────────────────────┘
|
||||
> Omitting the `:` for default values will only check if the variable is set, not if it is null.
|
||||
|
||||
### Examples
|
||||
```bash
|
||||
name="John"
|
||||
echo "${name}"
|
||||
echo "${name/J/j}" #=> "john" (substitution)
|
||||
echo "${name:0:2}" #=> "Jo" (slicing)
|
||||
echo "${name::2}" #=> "Jo" (slicing)
|
||||
echo "${name::-1}" #=> "Joh" (slicing)
|
||||
echo "${name:(-1)}" #=> "n" (slicing from right)
|
||||
echo "${name:(-2):1}" #=> "h" (slicing from right)
|
||||
echo "${food:-Cake}" #=> $food or "Cake"
|
||||
|
||||
length=2
|
||||
echo "${name:0:length}" #=> "Jo" (slicing with variable)
|
||||
|
||||
str="/path/to/foo.cpp"
|
||||
echo "${str%.cpp}" # /path/to/foo
|
||||
echo "${str%.cpp}.o" # /path/to/foo.o
|
||||
echo "${str%/*}" # /path/to
|
||||
|
||||
echo "${str##*.}" # cpp (extension)
|
||||
echo "${str##*/}" # foo.cpp (basepath)
|
||||
echo "${str#*o}" # o/foo.cpp (first occurrence of 'o')
|
||||
echo "${str##*o}" # oo.cpp (last occurrence of 'o')
|
||||
|
||||
echo "${str#*/}" # path/to/foo.cpp
|
||||
echo "${str##*/}" # foo.cpp
|
||||
|
||||
echo "${str/foo/bar}" # /path/to/bar.cpp
|
||||
|
||||
str="HELLO WORLD!"
|
||||
echo "${str,}" #=> "hELLO WORLD!" (lowercase 1st letter)
|
||||
echo "${str,,}" #=> "hello world!" (all lowercase)
|
||||
|
||||
str="hello world!"
|
||||
echo "${str^}" #=> "Hello world!" (uppercase 1st letter)
|
||||
echo "${str^^}" #=> "HELLO WORLD!" (all uppercase)
|
||||
```
|
||||
|
||||
## Comments
|
||||
```bash
|
||||
# This is a comment
|
||||
|
||||
: '
|
||||
This is a multi-line comment workaround
|
||||
'
|
||||
```
|
||||
|
||||
## Loops
|
||||
### Basic for loop
|
||||
```bash
|
||||
for i in /etc/rc.*; do
|
||||
echo "$i"
|
||||
done
|
||||
```
|
||||
|
||||
### C-style for loop
|
||||
```bash
|
||||
for (( i = 0; i < 10; i++ )); do
|
||||
echo "$i"
|
||||
done
|
||||
```
|
||||
|
||||
### Ranges
|
||||
```bash
|
||||
for i in {1..10}; do
|
||||
echo "$i"
|
||||
done
|
||||
|
||||
# With step size
|
||||
for i in {1..10..2}; do
|
||||
echo "$i"
|
||||
done
|
||||
```
|
||||
|
||||
### Reading lines
|
||||
```bash
|
||||
while IFS= read -r line; do
|
||||
echo "$line"
|
||||
done < file.txt
|
||||
```
|
||||
|
||||
### Infinite loop
|
||||
```bash
|
||||
while true; do
|
||||
echo "Press [CTRL+C] to stop.."
|
||||
sleep 1
|
||||
done
|
||||
```
|
||||
|
||||
## History
|
||||
┌───────────────┬────────────────────────────────────────────────────────────────┐
|
||||
│ `!!` │ Expand latest command in history │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$` │ Expand last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!*` │ Expand all parameters of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!-n` │ Expand nth latest command in history │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!n` │ Expand nth command in history │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!string` │ Expand latest command starting with string │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:s/from/to` │ Replace first occurrence of "from" with "to" in latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:gs/from/to` │ Replace all occurrences of "from" with "to" in latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:p` │ Print latest command without executing it │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$:t` │ Expand basename of last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$:h` │ Expand directory of last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:n` │ Expand only nth parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!^` │ Expand first parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$` │ Expand last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:n-m` │ Expand parameters from nth to mth of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:n-$` │ Expand parameters from nth to last of latest command │
|
||||
└───────────────┴────────────────────────────────────────────────────────────────┘
|
||||
|
||||
## Redirection
|
||||
┌────────────────────────────┬─────────────────────────────────┐
|
||||
│ `SCRIPT > FILE` │ stdout to FILE │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT >> FILE` │ Append stdout to FILE │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT 2> FILE` │ stderr to FILE │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT 2>&1`/`SCRIPT &> FILE` │ stderr to stdout │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT 2> /dev/null` │ stderr to /dev/null │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT > FILE 2>&1` │ stdout and stderr to FILE │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT &> /dev/null` │ stdout and stderr to /dev/null │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `echo "$0: warning" >&2` │ Print to stderr │
|
||||
├────────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT < FILE` │ stdin from FILE │
|
||||
└────────────────────────────┴─────────────────────────────────┘
|
||||
|
||||
## Switch statement
|
||||
```bash
|
||||
case "$1" in
|
||||
start | up)
|
||||
vagrant up
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|ssh}"
|
||||
;;
|
||||
esac
|
||||
```
|
||||
|
||||
### Options switch statement
|
||||
```bash
|
||||
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
|
||||
-V | --version )
|
||||
echo "$version"
|
||||
exit
|
||||
;;
|
||||
-s | --string )
|
||||
shift; string=$1
|
||||
;;
|
||||
-f | --flag )
|
||||
flag=1
|
||||
;;
|
||||
esac; shift; done
|
||||
if [[ "$1" == '--' ]]; then shift; fi
|
||||
```
|
||||
|
||||
## Functions
|
||||
```bash
|
||||
my_function() {
|
||||
echo "Hello, World!"
|
||||
}
|
||||
```
|
||||
|
||||
### Arguments
|
||||
┌────┬───────────────────────────────────┐
|
||||
│ `$#` │ Number of arguments │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$*` │ All arguments │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$@` │ All arguments as separate strings │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$1` │ First argument │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$_` │ Last argument │
|
||||
└────┴───────────────────────────────────┘
|
||||
|
||||
## Arrays
|
||||
```bash
|
||||
# Create an array in one line
|
||||
Fruits=('Apple' 'Banana' 'Orange')
|
||||
|
||||
# Or assign each value
|
||||
Fruits[0]="Apple"
|
||||
Fruits[1]="Banana"
|
||||
Fruits[2]="Orange"
|
||||
|
||||
# Associative array
|
||||
declare -A sounds
|
||||
|
||||
sounds[dog]="bark"
|
||||
sounds[cow]="moo"
|
||||
sounds[bird]="tweet"
|
||||
sounds[wolf]="howl"
|
||||
```
|
||||
|
||||
### Array element expansion
|
||||
┌──────────────────────┬────────────────────────────────────────────────────┐
|
||||
│ `${array[key]}` │ Element at `key` │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${array[-1]}` │ Last element (not for associative) │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${array[@]}` │ All elements │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${#array[@]}` │ Number of elements │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${#array}` │ String length of 1st element (not for associative) │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${#array[key]}` │ String length of element at `key` │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${array[@]:n:length}` │ `length` eements from position `n` │
|
||||
├──────────────────────┼────────────────────────────────────────────────────┤
|
||||
│ `${!array[@]}` │ Keys of all elements │
|
||||
└──────────────────────┴────────────────────────────────────────────────────┘
|
||||
|
||||
### Operations
|
||||
```bash
|
||||
Fruits=("${Fruits[@]}" "Watermelon") # Push
|
||||
Fruits+=('Watermelon') # Also Push
|
||||
Fruits=( "${Fruits[@]/Ap*/}" ) # Remove by regex match
|
||||
unset Fruits[2] # Remove one item
|
||||
Fruits=("${Fruits[@]}") # Duplicate
|
||||
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
|
||||
words=($(< datafile)) # From file (split by IFS)
|
||||
```
|
||||
|
||||
### Looping
|
||||
```bash
|
||||
for i in "${Fruits[@]}"; do
|
||||
echo "$i"
|
||||
done
|
||||
|
||||
for key in "${!sounds[@]}"; do
|
||||
echo "$key"
|
||||
done
|
||||
```
|
||||
|
||||
## Misc
|
||||
|
||||
### Directory of script
|
||||
```bash
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
||||
```
|
||||
|
||||
### Special variables
|
||||
┌──────────────────┬────────────────────────────────────────┐
|
||||
│ `$?` │ Exit status of last task │
|
||||
├──────────────────┼────────────────────────────────────────┤
|
||||
│ `$!` │ PID of last background task │
|
||||
├──────────────────┼────────────────────────────────────────┤
|
||||
│ `$$` │ PID of shell │
|
||||
├──────────────────┼────────────────────────────────────────┤
|
||||
│ `$0` │ Filename of the shell script │
|
||||
├──────────────────┼────────────────────────────────────────┤
|
||||
│ `$_` │ Last arugment of altest command │
|
||||
├──────────────────┼────────────────────────────────────────┤
|
||||
│ `${PIPESTATUS[n]}` │ Return value of piped commands (array) │
|
||||
└──────────────────┴────────────────────────────────────────┘
|
||||
@@ -0,0 +1,205 @@
|
||||
# Regex Cheat Sheet
|
||||
|
||||
## Anchors
|
||||
┌────┬─────────────────────────────────────────────────────────┐
|
||||
│ ^ │ Start of string, or start of line in multi-line pattern │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ \A │ Start of string │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ $ │ End of string, or end of line in multi-line pattern │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ \Z │ End of string │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ \b │ Word boundary │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ \B │ Not word boundary │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ \< │ Start of word │
|
||||
├────┼─────────────────────────────────────────────────────────┤
|
||||
│ \> │ End of word │
|
||||
└────┴─────────────────────────────────────────────────────────┘
|
||||
|
||||
## Character Classes
|
||||
┌────┬────────────────────┐
|
||||
│ \c │ Control character │
|
||||
├────┼────────────────────┤
|
||||
│ \s │ White space │
|
||||
├────┼────────────────────┤
|
||||
│ \S │ Not white space │
|
||||
├────┼────────────────────┤
|
||||
│ \d │ Digit │
|
||||
├────┼────────────────────┤
|
||||
│ \D │ Not digit │
|
||||
├────┼────────────────────┤
|
||||
│ \w │ Word │
|
||||
├────┼────────────────────┤
|
||||
│ \W │ Not word │
|
||||
├────┼────────────────────┤
|
||||
│ \x │ Hexadecimal digit │
|
||||
├────┼────────────────────┤
|
||||
│ \O │ Octal digit │
|
||||
└────┴────────────────────┘
|
||||
|
||||
## POSIX
|
||||
┌───────────┬───────────────────────────────┐
|
||||
│ `[:upper:]` │ Upper case letters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:lower:]` │ Lower case letters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:alpha:]` │ All letters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:alnum:]` │ Digits and letters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:digit:]` │ Digits │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:xdigit:]`│ Hexadecimal digits │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:punct:]` │ Punctuation │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:blank:]` │ Space and tab │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:space:]` │ Blank characters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:cntrl:]` │ Control characters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:graph:]` │ Printed characters │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:print:]` │ Printed characters and spaces │
|
||||
├───────────┼───────────────────────────────┤
|
||||
│ `[:word:]` │ Digits, letters and underscore│
|
||||
└───────────┴───────────────────────────────┘
|
||||
|
||||
## Assertions
|
||||
┌──────┬──────────────────────────────┐
|
||||
│ ?= │ Lookahead assertion │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?! │ Negative lookahead │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?<= │ Lookbehind assertion │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?!= │ Negative lookbehind │
|
||||
│ ?<! │ │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?> │ Once-only subexpression │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?() │ Condition `[if then]` │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?()| │ Condition `[if then else]` │
|
||||
├──────┼──────────────────────────────┤
|
||||
│ ?# │ Comment │
|
||||
└──────┴──────────────────────────────┘
|
||||
|
||||
## Quantifiers
|
||||
┌───────┬─────────────┐
|
||||
│ * │ 0 or more │
|
||||
├───────┼─────────────┤
|
||||
│ + │ 1 or more │
|
||||
├───────┼─────────────┤
|
||||
│ ? │ 0 or 1 │
|
||||
├───────┼─────────────┤
|
||||
│ {3} │ Exactly 3 │
|
||||
├───────┼─────────────┤
|
||||
│ {3,} │ 3 or more │
|
||||
├───────┼─────────────┤
|
||||
│ {3,5} │ 3, 4 or 5 │
|
||||
└───────┴─────────────┘
|
||||
> Add a `?` to a quantifier to make it ungreedy.
|
||||
|
||||
## Escape Sequences
|
||||
┌────┬─────────────────────────────┐
|
||||
│ \ │ Escape following character │
|
||||
├────┼─────────────────────────────┤
|
||||
│ \Q │ Begin literal sequence │
|
||||
├────┼─────────────────────────────┤
|
||||
│ \E │ End literal sequence │
|
||||
└────┴─────────────────────────────┘
|
||||
> "Escaping" is a way of treating characters which have a special meaning in regular expressions literally, rather than as special characters.
|
||||
|
||||
## Common Metacharacters
|
||||
┌───┬───┬───┬───┐
|
||||
│ ^ │ [ │ . │ $ │
|
||||
├───┼───┼───┼───┤
|
||||
│ { │ * │ ( │ \ │
|
||||
├───┼───┼───┼───┤
|
||||
│ + │ ) │ | │ ? │
|
||||
├───┼───┼───┼───┤
|
||||
│ < │ > │ │ │
|
||||
└───┴───┴───┴───┘
|
||||
> The escape character is usually `\`.
|
||||
|
||||
## Special Characters
|
||||
┌──────┬────────────────────┐
|
||||
│ \n │ New line │
|
||||
├──────┼────────────────────┤
|
||||
│ \r │ Carriage return │
|
||||
├──────┼────────────────────┤
|
||||
│ \t │ Tab │
|
||||
├──────┼────────────────────┤
|
||||
│ \v │ Vertical tab │
|
||||
├──────┼────────────────────┤
|
||||
│ \f │ Form feed │
|
||||
├──────┼────────────────────┤
|
||||
│ \xxx │ Octal character xxx│
|
||||
├──────┼────────────────────┤
|
||||
│ \xhh │ Hex character hh │
|
||||
└──────┴────────────────────┘
|
||||
|
||||
## Groups and Ranges
|
||||
┌─────────┬────────────────────────────────────┐
|
||||
│ . │ Any character except new line (\n) │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ (a|b) │ a or b │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ (...) │ Group │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ (?:...) │ Passive (non-capturing) group │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ `[abc]` │ Range (a or b or c) │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ `[^abc]` │ Not (a or b or c) │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ `[a-q]` │ Lower case letter from a to q │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ `[A-Q]` │ Upper case letter from A to Q │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ `[0-7]` │ Digit from 0 to 7 │
|
||||
├─────────┼────────────────────────────────────┤
|
||||
│ \x │ Group/subpattern number "x" │
|
||||
└─────────┴────────────────────────────────────┘
|
||||
> Ranges are inclusive.
|
||||
|
||||
## Pattern Modifiers
|
||||
┌────┬────────────────────────────────────┐
|
||||
│ g │ Global match │
|
||||
├────┼────────────────────────────────────┤
|
||||
│ i │ Case-insensitive │
|
||||
├────┼────────────────────────────────────┤
|
||||
│ m │ Multiple lines │
|
||||
├────┼────────────────────────────────────┤
|
||||
│ s │ Treat string as single line │
|
||||
├────┼────────────────────────────────────┤
|
||||
│ x │ Allow comments and whitespace │
|
||||
├────┼────────────────────────────────────┤
|
||||
│ e │ Evaluate replacement │
|
||||
├────┼────────────────────────────────────┤
|
||||
│ U │ Ungreedy pattern │
|
||||
└────┴────────────────────────────────────┘
|
||||
> * PCRE modifier
|
||||
|
||||
## String Replacement
|
||||
┌────┬──────────────────────────────────┐
|
||||
│ `$n` │ nth non-passive group │
|
||||
├────┼──────────────────────────────────┤
|
||||
│ `$2` │ "xyz" in `/^(abc(xyz))$/` │
|
||||
├────┼──────────────────────────────────┤
|
||||
│ `$1` │ "xyz" in `/^(?:abc)(xyz)$/` │
|
||||
├────┼──────────────────────────────────┤
|
||||
│ `$`` │ Before matched string │
|
||||
├────┼──────────────────────────────────┤
|
||||
│ `$'` │ After matched string │
|
||||
├────┼──────────────────────────────────┤
|
||||
│ `$+` │ Last matched string │
|
||||
├────┼──────────────────────────────────┤
|
||||
│ `$&` │ Entire matched string │
|
||||
└────┴──────────────────────────────────┘
|
||||
> Some regex implementations use `\` instead of `$`.
|
||||
@@ -0,0 +1,437 @@
|
||||
# ZSH Cheat Sheet
|
||||
|
||||
## Conditionals
|
||||
|
||||
```zsh
|
||||
if [[ -z "$string" ]]; then
|
||||
echo "String is empty"
|
||||
elif [[ -n "$string" ]]; then
|
||||
echo "String is not empty"
|
||||
fi
|
||||
```
|
||||
|
||||
### String conditions
|
||||
┌────────────────────────┬──────────────────┐
|
||||
│ `[[ -z STRING ]]` │ Empty string │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ -n STRING ]]` │ Not empty string │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ STRING == STRING ]]` │ Equal │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ STRING != STRING ]]` │ Not Equal │
|
||||
├────────────────────────┼──────────────────┤
|
||||
│ `[[ STRING =~ STRING ]]` │ Regex │
|
||||
└────────────────────────┴──────────────────┘
|
||||
|
||||
### Number conditions
|
||||
┌────────────────────────┬───────────────────────┐
|
||||
│ `[[ NUM1 -eq NUM2 ]]` │ Equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -ne NUM2 ]]` │ Not Equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -lt NUM2 ]]` │ Less than │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -le NUM2 ]]` │ Less than or equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -gt NUM2 ]]` │ Greater than │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `[[ NUM1 -ge NUM2 ]]` │ Greater than or equal │
|
||||
├────────────────────────┼───────────────────────┤
|
||||
│ `(( NUM < NUM ))` │ Numeric conditions │
|
||||
└────────────────────────┴───────────────────────┘
|
||||
|
||||
### File conditions
|
||||
┌───────────────────────┬─────────────────────────┐
|
||||
│ `[[ -e FILE ]]` │ Exists │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -r FILE ]]` │ Readable │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -h FILE ]]` │ Symlink │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -d FILE ]]` │ Directory │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -w FILE ]]` │ Writable │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -s FILE ]]` │ Size is > 0 bytes │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -f FILE ]]` │ File │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ -x FILE ]]` │ Executable │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ FILE1 -nt FILE2 ]]` │ 1 is more recent than 2 │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ FILE1 -ot FILE2 ]]` │ 2 is more recent than 1 │
|
||||
├───────────────────────┼─────────────────────────┤
|
||||
│ `[[ FILE1 -ef FILE2 ]]` │ Same files │
|
||||
└───────────────────────┴─────────────────────────┘
|
||||
|
||||
### Other conditions
|
||||
┌────────────────────┬────────────────────┐
|
||||
│ `[[ -o noclobber ]]` │ Option enabled │
|
||||
├────────────────────┼────────────────────┤
|
||||
│ `[[ ! EXPR ]]` │ Not │
|
||||
├────────────────────┼────────────────────┤
|
||||
│ `[[ X && Y ]]` │ And │
|
||||
├────────────────────┼────────────────────┤
|
||||
│ `[[ X || Y ]]` │ Or │
|
||||
└────────────────────┴────────────────────┘
|
||||
|
||||
## Parameter manipulation
|
||||
┌─────────────────────────────────┬────────────────────────────────────────────────────────────────┐
|
||||
│ `${foo}` │ Variable │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo/from/to}` │ Replace first match │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo/#from/to}`/`${foo/%from/to}` │ Replace first match anchored to start (default) or end │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo//from/to}` │ Replace all │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo[start,length]}` │ Slice from `start` to `length` (`length` can be negative) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo%suffix}`/`${foo%%suffix}` │ Remove `suffix` (ungreedy/greedy) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo#prefix}`/`${foo##prefix}` │ Remove `prefix` (ungreedy/greedy) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${#foo}` │ Length of `foo` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${(L)foo}` │ Lowercase all letters │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${(U)foo}` │ Uppercase all letters │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${(q)foo}` │ Quote safely │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${(qq)foo}` │ Quote more explicitely │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${(s.del.)foo}` │ Split on `del` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${(j.del.)foo}` │ Join with `del` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${file:h}` │ Directory of `file` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${file:t}` │ Filename of `file` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${file:e}` │ Extension of `file` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${file:r}` │ Root of `file` (without extension) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${file:a}` │ Absolute path of `file` │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${file:A}` │ Absolute path of `file` (resolves symlinks) │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:-val}` │ Use `val` if `foo` is unset or null │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:=val}` │ Set `foo` to `val` if unset or null │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:+val}` │ Use `val` if `foo` **is** set and not null │
|
||||
├─────────────────────────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `${foo:?message}` │ Print `message` and exit if `foo` is unset or null │
|
||||
└─────────────────────────────────┴────────────────────────────────────────────────────────────────┘
|
||||
> Omitting the `:` for default values will only check if the variable is set, not if it is null.
|
||||
|
||||
### Examples
|
||||
```zsh
|
||||
name="John"
|
||||
echo "${name}"
|
||||
echo "${name/J/j}" #=> "john" (substitution)
|
||||
echo "${name[0,2]}" #=> "Jo" (slicing)
|
||||
echo "${food:-Cake}" #=> $food or "Cake"
|
||||
|
||||
length=2
|
||||
echo "${name[1,length]}" #=> "Jo" (slicing with variable)
|
||||
|
||||
str="/path/to/foo.cpp"
|
||||
echo "${str%.cpp}" # /path/to/foo
|
||||
echo "${str%.cpp}.o" # /path/to/foo.o
|
||||
echo "${str%/*}" # /path/to
|
||||
|
||||
echo "${str##*.}" # cpp (extension)
|
||||
echo "${str##*/}" # foo.cpp (basepath)
|
||||
echo "${str#*o}" # o/foo.cpp (first occurrence of 'o')
|
||||
echo "${str##*o}" # oo.cpp (last occurrence of 'o')
|
||||
|
||||
echo "${str#*/}" # path/to/foo.cpp
|
||||
echo "${str##*/}" # foo.cpp
|
||||
|
||||
echo "${str/foo/bar}" # /path/to/bar.cpp
|
||||
|
||||
str="HELLO WORLD!"
|
||||
echo "${(L)str}" #=> "hello world!" (all lowercase)
|
||||
|
||||
str="hello world!"
|
||||
echo "${(U)str}" #=> "HELLO WORLD!" (all uppercase)
|
||||
```
|
||||
## Globs
|
||||
┌───────────────┬───────────────────────┐
|
||||
│ `ls **/*.js` │ Recursive expansion │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(.)` │ Plain files │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(/)` │ Directories │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(@)` │ Symlinks │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(*)` │ Executables │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(L+1M)` │ Files larger than 1MB │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(om[1,5])` │ 5 newest files │
|
||||
├───────────────┼───────────────────────┤
|
||||
│ `ls *(.)~*.bak` │ Files except *.bak │
|
||||
└───────────────┴───────────────────────┘
|
||||
|
||||
## Comments
|
||||
```zsh
|
||||
# This is a comment
|
||||
|
||||
: '
|
||||
This is a multi-line comment workaround
|
||||
'
|
||||
```
|
||||
|
||||
## Loops
|
||||
### Basic for loop
|
||||
```zsh
|
||||
for i in /etc/rc.*; do
|
||||
echo "$i"
|
||||
done
|
||||
```
|
||||
|
||||
### C-style for loop
|
||||
```zsh
|
||||
for (( i = 0; i < 10; i++ )); do
|
||||
echo "$i"
|
||||
done
|
||||
```
|
||||
|
||||
### Ranges
|
||||
```zsh
|
||||
for i in {1..10}; do
|
||||
echo "$i"
|
||||
done
|
||||
|
||||
# With step size
|
||||
for i in {1..10..2}; do
|
||||
echo "$i"
|
||||
done
|
||||
```
|
||||
|
||||
### Reading lines
|
||||
```zsh
|
||||
while IFS= read -r line; do
|
||||
print -r -- "$line"
|
||||
done < file.txt
|
||||
```
|
||||
|
||||
### Infinite loop
|
||||
```zsh
|
||||
while true; do
|
||||
echo "Press [CTRL+C] to stop.."
|
||||
sleep 1
|
||||
done
|
||||
```
|
||||
|
||||
## History
|
||||
┌───────────────┬────────────────────────────────────────────────────────────────┐
|
||||
│ `!!` │ Expand latest command in history │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$` │ Expand last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!*` │ Expand all parameters of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!-n` │ Expand nth latest command in history │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!n` │ Expand nth command in history │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!string` │ Expand latest command starting with string │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:s/from/to` │ Replace first occurrence of "from" with "to" in latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:gs/from/to` │ Replace all occurrences of "from" with "to" in latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:p` │ Print latest command without executing it │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$:t` │ Expand basename of last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$:h` │ Expand directory of last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:n` │ Expand only nth parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!^` │ Expand first parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!$` │ Expand last parameter of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:n-m` │ Expand parameters from nth to mth of latest command │
|
||||
├───────────────┼────────────────────────────────────────────────────────────────┤
|
||||
│ `!!:n-$` │ Expand parameters from nth to last of latest command │
|
||||
└───────────────┴────────────────────────────────────────────────────────────────┘
|
||||
|
||||
## Redirection
|
||||
┌────────────────────────┬─────────────────────────────────┐
|
||||
│ `SCRIPT > FILE` │ stdout to FILE │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT >> FILE` │ Append stdout to FILE │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT 2> FILE` │ stderr to FILE │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT 2>&1` │ stderr to stdout │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT &> FILE` │ stderr and stdout to FILE │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT 2> /dev/null` │ stderr to /dev/null │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT > FILE 2>&1` │ stdout and stderr to FILE │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT &> /dev/null` │ stdout and stderr to /dev/null │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `echo "$0: warning" >&2` │ Print to stderr │
|
||||
├────────────────────────┼─────────────────────────────────┤
|
||||
│ `SCRIPT < FILE` │ stdin from FILE │
|
||||
└────────────────────────┴─────────────────────────────────┘
|
||||
|
||||
## Switch statement
|
||||
```zsh
|
||||
case "$1" in
|
||||
start | up)
|
||||
vagrant up
|
||||
;;
|
||||
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|ssh}"
|
||||
;;
|
||||
esac
|
||||
```
|
||||
|
||||
### Options switch statement
|
||||
```zsh
|
||||
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
|
||||
-V | --version )
|
||||
echo "$version"
|
||||
exit
|
||||
;;
|
||||
-s | --string )
|
||||
shift; string=$1
|
||||
;;
|
||||
-f | --flag )
|
||||
flag=1
|
||||
;;
|
||||
esac; shift; done
|
||||
if [[ "$1" == '--' ]]; then shift; fi
|
||||
```
|
||||
|
||||
## Functions
|
||||
```zsh
|
||||
my_function() {
|
||||
echo "Hello, World!"
|
||||
}
|
||||
```
|
||||
|
||||
### Arguments
|
||||
┌────┬───────────────────────────────────┐
|
||||
│ `$#` │ Number of arguments │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$*` │ All arguments │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$@` │ All arguments as separate strings │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$1` │ First argument │
|
||||
├────┼───────────────────────────────────┤
|
||||
│ `$_` │ Last argument │
|
||||
└────┴───────────────────────────────────┘
|
||||
|
||||
## Arrays
|
||||
```zsh
|
||||
# Create an array in one line
|
||||
Fruits=('Apple' 'Banana' 'Orange')
|
||||
|
||||
# Or assign each value
|
||||
Fruits[1]="Apple"
|
||||
Fruits[2]="Banana"
|
||||
Fruits[3]="Orange"
|
||||
|
||||
# Associative array
|
||||
typeset -A sounds
|
||||
|
||||
sounds[dog]="bark"
|
||||
sounds[cow]="moo"
|
||||
sounds[bird]="tweet"
|
||||
sounds[wolf]="howl"
|
||||
```
|
||||
|
||||
### Array element expansion
|
||||
┌──────────────────────────────────────┬────────────────────────────────────┐
|
||||
│ `${array[key]}` │ Element at `key` │
|
||||
├──────────────────────────────────────┼────────────────────────────────────┤
|
||||
│ `${array[-1]}` │ Last element (not for associative) │
|
||||
├──────────────────────────────────────┼────────────────────────────────────┤
|
||||
│ `${array[@]}` │ All elements │
|
||||
├──────────────────────────────────────┼────────────────────────────────────┤
|
||||
│ `${#array}` │ Number of elements │
|
||||
├──────────────────────────────────────┼────────────────────────────────────┤
|
||||
│ `${#array[key]}` │ String length of element at `key` │
|
||||
├──────────────────────────────────────┼────────────────────────────────────┤
|
||||
│ `${array:n:length}`/`${array[n,length]}` │ `length` eements from position `n` │
|
||||
├──────────────────────────────────────┼────────────────────────────────────┤
|
||||
│ `${(k)array}` │ Keys of all elements │
|
||||
└──────────────────────────────────────┴────────────────────────────────────┘
|
||||
|
||||
### Operations
|
||||
```zsh
|
||||
Fruits=("${Fruits[@]}" "Watermelon") # Push
|
||||
Fruits+=('Watermelon') # Also Push
|
||||
Fruits=( "${Fruits[@]/Ap*/}" ) # Remove by regex match
|
||||
unset Fruits[3] # Remove one item
|
||||
Fruits=("${Fruits[@]}") # Duplicate
|
||||
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
|
||||
words=($(< datafile)) # From file (split by IFS)
|
||||
```
|
||||
|
||||
### Looping
|
||||
```zsh
|
||||
for i in "${Fruits[@]}"; do
|
||||
echo "$i"
|
||||
done
|
||||
|
||||
for val in "${(v)sounds}"; do
|
||||
echo "$val"
|
||||
done
|
||||
|
||||
for key in "${(k)sounds}"; do
|
||||
echo "$key"
|
||||
done
|
||||
```
|
||||
|
||||
## Misc
|
||||
|
||||
### Directory of script
|
||||
```zsh
|
||||
DIR="$( cd "$( dirname "${ZSH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
||||
```
|
||||
|
||||
### Special variables
|
||||
┌──────────────────┬───────────────────────────────────────────────┐
|
||||
│ `$?` │ Exit status of last task │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$!` │ PID of last background task │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$$` │ PID of shell │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$#` │ Number of arguments passed to script │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$0` │ Filename of the shell script │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$_` │ Last arugment of altest command │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$argv` │ Array of all arguments passed to script │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$ARGC` │ Number of arguments passed to script │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `${pipestatus[n]}` │ Return value of piped commands (array) │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$RANDOM` │ Random number between 0 and 32767 │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$SECONDS` │ Number of seconds since the shell was started │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$PWD` │ Current working directory │
|
||||
├──────────────────┼───────────────────────────────────────────────┤
|
||||
│ `$OLDPWD` │ Previous working directory │
|
||||
└──────────────────┴───────────────────────────────────────────────┘
|
||||
@@ -1,114 +0,0 @@
|
||||
{
|
||||
"name": "Neovim Configuration",
|
||||
"version": "1.0.0",
|
||||
"system_prompt": "You are an expert Neovim configuration assistant. Help the user understand and modify their Neovim setup. Focus on clear explanations and suggest improvements when appropriate.",
|
||||
"vars": {
|
||||
"lua_dir": "lua"
|
||||
},
|
||||
"groups": [
|
||||
{
|
||||
"name": "Core Configuration",
|
||||
"system_prompt": "These files define the core Neovim behavior including options, keymaps, and autocommands. When suggesting changes, ensure they align with Neovim best practices.",
|
||||
"data": [
|
||||
"core_opt",
|
||||
"core_keymap",
|
||||
"core_autocmd",
|
||||
"core_helpers"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Plugin Management",
|
||||
"system_prompt": "This file contains the plugin manager setup. Help the user understand plugin dependencies and installation patterns.",
|
||||
"data": [
|
||||
"lazy_init"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "LSP Configuration",
|
||||
"system_prompt": "These files define the Language Server Protocol setup. Help the user configure language servers, diagnostics, and code actions.",
|
||||
"data": [
|
||||
"lsp_config"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Frequently Used Plugins",
|
||||
"system_prompt": "These are plugins the user frequently configures. Provide detailed explanations about their options and how they interact with the rest of the configuration.",
|
||||
"data": [
|
||||
"plugin_codecompanion",
|
||||
"plugin_edgy",
|
||||
"plugin_snacks",
|
||||
"plugin_mini",
|
||||
"plugin_telescope"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "UI and Theme",
|
||||
"system_prompt": "These files define the visual appearance of Neovim. Help the user understand how to customize colors and UI elements.",
|
||||
"data": [
|
||||
"plugin_colorscheme"
|
||||
]
|
||||
}
|
||||
],
|
||||
"data": {
|
||||
"core_opt": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/opt.lua",
|
||||
"description": "Neovim options configuration"
|
||||
},
|
||||
"core_keymap": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/keymap.lua",
|
||||
"description": "Key mappings configuration"
|
||||
},
|
||||
"core_autocmd": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/autocmd.lua",
|
||||
"description": "Automatic commands configuration"
|
||||
},
|
||||
"core_helpers": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/helpers.lua",
|
||||
"description": "Helper functions for Neovim configuration"
|
||||
},
|
||||
"lazy_init": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/lazy_init.lua",
|
||||
"description": "Lazy plugin manager initialization"
|
||||
},
|
||||
"lsp_config": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/lsp.lua",
|
||||
"description": "LSP plugins and configuration"
|
||||
},
|
||||
"plugin_codecompanion": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/codecompanion.lua",
|
||||
"description": "CodeCompanion plugin configuration"
|
||||
},
|
||||
"plugin_edgy": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/edgy.lua",
|
||||
"description": "Edgy plugin configuration"
|
||||
},
|
||||
"plugin_snacks": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/snacks.lua",
|
||||
"description": "Snacks plugin configuration"
|
||||
},
|
||||
"plugin_mini": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/mini.lua",
|
||||
"description": "Mini plugins configuration"
|
||||
},
|
||||
"plugin_telescope": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/telescope.lua",
|
||||
"description": "Telescope fuzzy finder configuration"
|
||||
},
|
||||
"plugin_colorscheme": {
|
||||
"type": "file",
|
||||
"path": "${lua_dir}/plugins/color.lua",
|
||||
"description": "Color scheme and UI appearance configuration"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
-- Set <space> as the leader key
|
||||
-- See `:help mapleader`
|
||||
-- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used)
|
||||
vim.lsp.log.set_level 'debug'
|
||||
vim.g.mapleader = ','
|
||||
vim.g.maplocalleader = ','
|
||||
|
||||
@@ -62,3 +63,4 @@ TODO: Neovim configurations I want to add:
|
||||
-- " - To operate on multiple lines use <c-v> to select the lines, <c-i> to go to
|
||||
-- " insert mode, perform edit, press <esc>.
|
||||
-- " - Use the command :r to paste the output of a command to the buffer.
|
||||
|
||||
|
||||
+174
-150
@@ -18,12 +18,14 @@ vim.api.nvim_create_autocmd('FocusLost', {
|
||||
group = autosave_group,
|
||||
pattern = '*',
|
||||
command = 'silent! wa', -- Save all files when moving away from the window
|
||||
nested = true, -- Allow other autocommands to run after this one
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd('InsertLeave', {
|
||||
vim.api.nvim_create_autocmd('BufLeave', {
|
||||
group = autosave_group,
|
||||
pattern = '*',
|
||||
command = 'silent! wa', -- Save all files when leaving insert mode
|
||||
command = 'silent! wa', -- Save all files when switching buffers
|
||||
nested = true, -- Allow other autocommands to run after this one
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd('SwapExists', {
|
||||
@@ -35,176 +37,198 @@ vim.api.nvim_create_autocmd('SwapExists', {
|
||||
})
|
||||
|
||||
-- vim.api.nvim_create_autocmd('TextChanged', {
|
||||
-- group = autosave_group,
|
||||
-- pattern = '*',
|
||||
-- command = 'silent! wa', -- Save all files when text is changed
|
||||
-- })
|
||||
-- group = autosave_group,
|
||||
-- pattern = '*',
|
||||
-- command = 'silent! wa', -- Save all files when text is changed
|
||||
-- })
|
||||
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'OilActionsPost',
|
||||
callback = function(event)
|
||||
if event.data.actions.type == 'move' then
|
||||
Snacks.rename.on_rename_file(event.data.actions.src_url, event.data.actions.dest_url)
|
||||
end
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd('User', {
|
||||
pattern = 'OilActionsPost',
|
||||
callback = function(event)
|
||||
if event.data.actions.type == 'move' then
|
||||
Snacks.rename.on_rename_file(event.data.actions.src_url, event.data.actions.dest_url)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
local fidget_group = vim.api.nvim_create_augroup('CodeCompanionFidgetHooks', { clear = true })
|
||||
local fidget_group = vim.api.nvim_create_augroup('CodeCompanionFidgetHooks', { clear = true })
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'User' }, {
|
||||
pattern = 'CodeCompanionRequestStarted',
|
||||
group = fidget_group,
|
||||
callback = function(event)
|
||||
local FidgetHelper = require 'utils.fidget_helper'
|
||||
-- Pass event instead of request if the callback receives the full event object
|
||||
local handle = FidgetHelper:create_progress_handle(event)
|
||||
FidgetHelper:store_progress_handle(event.data.id, handle)
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd({ 'User' }, {
|
||||
pattern = 'CodeCompanionRequestStarted',
|
||||
group = fidget_group,
|
||||
callback = function(event)
|
||||
local FidgetHelper = require 'utils.fidget_helper'
|
||||
-- Pass event instead of request if the callback receives the full event object
|
||||
local handle = FidgetHelper:create_progress_handle(event)
|
||||
FidgetHelper:store_progress_handle(event.data.id, handle)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'User' }, {
|
||||
pattern = 'CodeCompanionRequestFinished',
|
||||
group = fidget_group,
|
||||
callback = function(event)
|
||||
local FidgetHelper = require 'utils.fidget_helper'
|
||||
local handle = FidgetHelper:pop_progress_handle(event.data.id)
|
||||
if handle then
|
||||
FidgetHelper:report_exit_status(handle, event)
|
||||
handle:finish()
|
||||
end
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd({ 'User' }, {
|
||||
pattern = 'CodeCompanionRequestFinished',
|
||||
group = fidget_group,
|
||||
callback = function(event)
|
||||
local FidgetHelper = require 'utils.fidget_helper'
|
||||
local handle = FidgetHelper:pop_progress_handle(event.data.id)
|
||||
if handle then
|
||||
FidgetHelper:report_exit_status(handle, event)
|
||||
handle:finish()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
-- vim.api.nvim_create_autocmd('BufEnter', {
|
||||
-- callback = function(event)
|
||||
-- local windows = vim.api.nvim_list_wins()
|
||||
--
|
||||
-- for _, window in ipairs(windows) do
|
||||
-- local bufnr = vim.api.nvim_win_get_buf(window)
|
||||
-- local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
|
||||
-- if vim.api.nvim_get_option_value('buflisted', { buf = bufnr })
|
||||
-- or ft == 'oil'
|
||||
-- or ft == 'snacks_dashboard' then
|
||||
-- return
|
||||
-- end
|
||||
-- end
|
||||
-- vim.cmd 'qa'
|
||||
-- end,
|
||||
-- })
|
||||
-- vim.api.nvim_create_autocmd('BufEnter', {
|
||||
-- callback = function(event)
|
||||
-- local windows = vim.api.nvim_list_wins()
|
||||
--
|
||||
-- for _, window in ipairs(windows) do
|
||||
-- local bufnr = vim.api.nvim_win_get_buf(window)
|
||||
-- local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
|
||||
-- if vim.api.nvim_get_option_value('buflisted', { buf = bufnr })
|
||||
-- or ft == 'oil'
|
||||
-- or ft == 'snacks_dashboard' then
|
||||
-- return
|
||||
-- end
|
||||
-- end
|
||||
-- vim.cmd 'qa'
|
||||
-- end,
|
||||
-- })
|
||||
|
||||
local modes_group = vim.api.nvim_create_augroup('modes', { clear = true })
|
||||
vim.api.nvim_create_autocmd('FocusLost', {
|
||||
group = modes_group,
|
||||
pattern = '*',
|
||||
command = 'call feedkeys("\\<Esc>")',
|
||||
})
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
|
||||
pattern = { '*.lua' },
|
||||
callback = function(args)
|
||||
require('helpers').format_buffer('stylua', args.buf)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd('BufNewFile', {
|
||||
group = modes_group,
|
||||
pattern = '*',
|
||||
command = 'call feedkeys("i")',
|
||||
})
|
||||
local modes_group = vim.api.nvim_create_augroup('modes', { clear = true })
|
||||
vim.api.nvim_create_autocmd('FocusLost', {
|
||||
group = modes_group,
|
||||
pattern = '*',
|
||||
command = 'call feedkeys("\\<Esc>")',
|
||||
})
|
||||
|
||||
-- Reload LuaSnip snippets when saving files in the snippets directory
|
||||
local snippets_dir = vim.fn.stdpath 'config' .. '/lua/snippets'
|
||||
vim.api.nvim_create_autocmd('BufWritePost', {
|
||||
pattern = snippets_dir .. '/*.json', -- Adjust the path to match your snippets directory
|
||||
desc = 'Reload LuaSnip snippets on save',
|
||||
callback = function()
|
||||
require('luasnip.loaders.from_vscode').lazy_load { paths = { snippets_dir } }
|
||||
vim.notify('Snippets reloaded!', vim.log.levels.INFO)
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd('BufNewFile', {
|
||||
group = modes_group,
|
||||
pattern = '*',
|
||||
command = 'call feedkeys("i")',
|
||||
})
|
||||
|
||||
local configs = {
|
||||
{
|
||||
path = os.getenv("HOME") .. "/.config/nixos",
|
||||
worktree = nil,
|
||||
git_dir = nil,
|
||||
track_untracked = false,
|
||||
force_add = false
|
||||
},
|
||||
{
|
||||
path = os.getenv("HOME") .. "/.config/nvim",
|
||||
worktree = nil,
|
||||
git_dir = nil,
|
||||
track_untracked = false,
|
||||
force_add = false
|
||||
},
|
||||
{
|
||||
path = os.getenv("HOME"),
|
||||
worktree = os.getenv("HOME"),
|
||||
git_dir = os.getenv("HOME") .. "/.config/dotfiles/.git",
|
||||
track_untracked = true,
|
||||
force_add = true, -- Handles ignored files in dotfiles
|
||||
include_dirs = { ".config", ".local/bin", ".local/share" }
|
||||
}
|
||||
}
|
||||
-- Reload LuaSnip snippets when saving files in the snippets directory
|
||||
local snippets_dir = vim.fn.stdpath 'config' .. '/lua/snippets'
|
||||
vim.api.nvim_create_autocmd('BufWritePost', {
|
||||
pattern = snippets_dir .. '/*.json', -- Adjust the path to match your snippets directory
|
||||
desc = 'Reload LuaSnip snippets on save',
|
||||
callback = function()
|
||||
require('luasnip.loaders.from_vscode').lazy_load { paths = { snippets_dir } }
|
||||
vim.notify('Snippets reloaded!', vim.log.levels.INFO)
|
||||
end,
|
||||
})
|
||||
|
||||
local function get_git_status(config)
|
||||
local base = "git"
|
||||
if config.git_dir and config.worktree then
|
||||
base = string.format("git --git-dir=%s --worktree=%s", config.git_dir, config.worktree)
|
||||
end
|
||||
-- Run shortcuts script when saving shortcut files
|
||||
vim.api.nvim_create_autocmd('BufWritePost', {
|
||||
pattern = {
|
||||
os.getenv 'XDG_CONFIG_HOME' .. '/shell/bm-files',
|
||||
os.getenv 'XDG_CONFIG_HOME' .. '/shell/bm-dirs',
|
||||
},
|
||||
desc = 'Run shortcuts script',
|
||||
callback = function()
|
||||
os.execute 'station-shortcuts'
|
||||
vim.notify('Shortcuts updated!', vim.log.levels.INFO)
|
||||
end,
|
||||
})
|
||||
|
||||
-- Check modified and untracked (including ignored if force_add is true)
|
||||
local cmd = base .. " status --porcelain"
|
||||
if config.force_add then cmd = cmd .. " --ignored" end
|
||||
local configs = {
|
||||
{
|
||||
path = os.getenv 'HOME' .. '/.config/nixos',
|
||||
worktree = nil,
|
||||
git_dir = nil,
|
||||
track_untracked = false,
|
||||
force_add = false,
|
||||
},
|
||||
{
|
||||
path = os.getenv 'HOME' .. '/.config/nvim',
|
||||
worktree = nil,
|
||||
git_dir = nil,
|
||||
track_untracked = false,
|
||||
force_add = false,
|
||||
},
|
||||
{
|
||||
path = os.getenv 'HOME',
|
||||
worktree = os.getenv 'HOME',
|
||||
git_dir = os.getenv 'HOME' .. '/.config/dotfiles/.git',
|
||||
track_untracked = true,
|
||||
force_add = true, -- Handles ignored files in dotfiles
|
||||
include_dirs = { '.config', '.local/bin', '.local/share' },
|
||||
},
|
||||
}
|
||||
|
||||
local handle = io.popen(cmd)
|
||||
local result = handle:read("*a")
|
||||
handle:close()
|
||||
local function get_git_status(config)
|
||||
local base = 'git'
|
||||
if config.git_dir and config.worktree then
|
||||
base = string.format('git --git-dir=%s --worktree=%s', config.git_dir, config.worktree)
|
||||
end
|
||||
|
||||
local has_changes = false
|
||||
for line in result:gmatch("[^\r\n]+") do
|
||||
local status = line:sub(1, 2)
|
||||
local file = line:sub(4)
|
||||
-- Check modified and untracked (including ignored if force_add is true)
|
||||
local cmd = base .. ' status --porcelain'
|
||||
if config.force_add then
|
||||
cmd = cmd .. ' --ignored'
|
||||
end
|
||||
|
||||
if status:match("[MAR]") then
|
||||
local handle = io.popen(cmd)
|
||||
local result = handle:read '*a'
|
||||
handle:close()
|
||||
|
||||
local has_changes = false
|
||||
for line in result:gmatch '[^\r\n]+' do
|
||||
local status = line:sub(1, 2)
|
||||
local file = line:sub(4)
|
||||
|
||||
if status:match '[MAR]' then
|
||||
has_changes = true
|
||||
elseif (status == '??' or status == '!!') and config.track_untracked then
|
||||
if config.include_dirs then
|
||||
for _, dir in ipairs(config.include_dirs) do
|
||||
if file:sub(1, #dir) == dir then
|
||||
has_changes = true
|
||||
elseif (status == "??" or status == "!!") and config.track_untracked then
|
||||
if config.include_dirs then
|
||||
for _, dir in ipairs(config.include_dirs) do
|
||||
if file:sub(1, #dir) == dir then
|
||||
has_changes = true
|
||||
break
|
||||
end
|
||||
end
|
||||
else
|
||||
has_changes = true
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
return has_changes, base
|
||||
else
|
||||
has_changes = true
|
||||
end
|
||||
end
|
||||
end
|
||||
return has_changes, base
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd("VimLeavePre", {
|
||||
callback = function()
|
||||
local cwd = vim.fn.getcwd()
|
||||
vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
callback = function()
|
||||
local cwd = vim.fn.getcwd()
|
||||
|
||||
for _, config in ipairs(configs) do
|
||||
if cwd:sub(1, #config.path) == config.path then
|
||||
local has_changes, git_base = get_git_status(config)
|
||||
for _, config in ipairs(configs) do
|
||||
if cwd:sub(1, #config.path) == config.path then
|
||||
local has_changes, git_base = get_git_status(config)
|
||||
|
||||
if has_changes then
|
||||
local confirm = vim.fn.confirm("Uncommitted changes in managed path. Commit?", "&Yes\n&No", 2)
|
||||
if confirm == 1 then
|
||||
local msg = vim.fn.input("Commit message: ", "chore: update files")
|
||||
if msg ~= "" then
|
||||
msg = "Update files"
|
||||
end
|
||||
local add_cmd = " add -A"
|
||||
os.execute(git_base .. add_cmd)
|
||||
os.execute(git_base .. " commit -m " .. vim.fn.shellescape(msg))
|
||||
os.execute(git_base .. " push")
|
||||
print("\nChanges committed.")
|
||||
end
|
||||
end
|
||||
break
|
||||
if has_changes then
|
||||
local confirm = vim.fn.confirm('Uncommitted changes in managed path. Commit?', '&Yes\n&No', 2)
|
||||
if confirm == 1 then
|
||||
local msg = vim.fn.input('Commit message: ', 'chore: update files')
|
||||
if msg ~= '' then
|
||||
msg = 'Update files'
|
||||
end
|
||||
local add_cmd = ' add -A'
|
||||
os.execute(git_base .. add_cmd)
|
||||
os.execute(git_base .. ' commit -m ' .. vim.fn.shellescape(msg))
|
||||
os.execute(git_base .. ' push')
|
||||
print '\nChanges committed.'
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
require('helpers').edit_cf('a', '/lua/autocmd.lua')
|
||||
|
||||
require('helpers').edit_cf('a', '/lua/autocmd.lua')
|
||||
|
||||
+124
-9
@@ -16,17 +16,28 @@ end
|
||||
|
||||
helpers.edit_cf('h', '/lua/helpers.lua')
|
||||
|
||||
---@param opts? { cmd?: string }
|
||||
helpers.open_term = function(opts)
|
||||
opts = opts or { cmd = '' }
|
||||
---@param opts? { cmd?: string, win_opts?: table, buf_opts?: table }
|
||||
helpers.open_term = function(opts, buf_opts)
|
||||
opts = opts or {}
|
||||
local cmd = opts.cmd or ''
|
||||
local win_opts = opts.win_opts
|
||||
local buf_options = buf_opts or opts.buf_opts or {
|
||||
bufhidden = 'wipe',
|
||||
modifiable = false,
|
||||
}
|
||||
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_set_option_value('bufhidden', 'wipe', { buf = buf })
|
||||
vim.api.nvim_set_option_value('modifiable', false, { buf = buf })
|
||||
if buf_options.name then
|
||||
vim.api.nvim_buf_set_name(buf, buf_options.name)
|
||||
buf_options.name = nil
|
||||
end
|
||||
for k, v in pairs(buf_options) do
|
||||
vim.api.nvim_set_option_value(k, v, { buf = buf })
|
||||
end
|
||||
|
||||
local height = math.ceil(vim.o.lines * 0.9)
|
||||
local width = math.ceil(vim.o.columns * 0.9)
|
||||
local win = vim.api.nvim_open_win(buf, true, {
|
||||
local win = vim.api.nvim_open_win(buf, true, win_opts or {
|
||||
style = 'minimal',
|
||||
relative = 'editor',
|
||||
width = width,
|
||||
@@ -38,7 +49,7 @@ helpers.open_term = function(opts)
|
||||
|
||||
vim.api.nvim_set_current_win(win)
|
||||
|
||||
vim.fn.jobstart(opts.cmd, {
|
||||
vim.fn.jobstart(cmd, {
|
||||
term = true,
|
||||
on_exit = function(_, _, _)
|
||||
if vim.api.nvim_win_is_valid(win) then
|
||||
@@ -51,8 +62,112 @@ helpers.open_term = function(opts)
|
||||
end
|
||||
|
||||
helpers.has_copilot = function()
|
||||
return vim.fn.getenv('COPILOT_API_KEY') ~= vim.NIL
|
||||
return vim.fn.getenv 'COPILOT_API_KEY' ~= vim.NIL
|
||||
end
|
||||
|
||||
helpers.open_file_modal = function(path, title)
|
||||
local width = math.floor(vim.o.columns * 0.85)
|
||||
local height = math.floor(vim.o.lines * 0.85)
|
||||
|
||||
local row = math.floor((vim.o.lines - height) / 2)
|
||||
local col = math.floor((vim.o.columns - width) / 2)
|
||||
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
|
||||
vim.api.nvim_open_win(buf, true, {
|
||||
relative = 'editor',
|
||||
width = width,
|
||||
height = height,
|
||||
row = row,
|
||||
col = col,
|
||||
style = 'minimal',
|
||||
border = 'rounded',
|
||||
title = ' ' .. title .. ' ',
|
||||
title_pos = 'center',
|
||||
})
|
||||
|
||||
vim.api.nvim_buf_set_name(buf, path)
|
||||
|
||||
local lines = vim.fn.readfile(path)
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
|
||||
|
||||
vim.bo[buf].filetype = 'markdown'
|
||||
vim.bo[buf].buftype = 'nofile'
|
||||
vim.bo[buf].bufhidden = 'wipe'
|
||||
vim.bo[buf].swapfile = false
|
||||
vim.bo[buf].modifiable = false
|
||||
vim.bo[buf].readonly = true
|
||||
|
||||
vim.wo.wrap = false
|
||||
vim.wo.number = false
|
||||
vim.wo.relativenumber = false
|
||||
vim.wo.cursorline = true
|
||||
|
||||
vim.keymap.set('n', 'q', '<cmd>close<CR>', {
|
||||
buffer = buf,
|
||||
silent = true,
|
||||
desc = 'Close cheatsheet',
|
||||
})
|
||||
vim.keymap.set('n', '<Esc>', '<cmd>close<CR>', {
|
||||
buffer = buf,
|
||||
silent = true,
|
||||
desc = 'Close cheatsheet',
|
||||
})
|
||||
end
|
||||
|
||||
local function get_formatter_bin(formatter)
|
||||
return vim.fn.stdpath 'data' .. '/mason/bin/' .. formatter
|
||||
end
|
||||
|
||||
local formatter_args = {
|
||||
pint = function(file)
|
||||
return {
|
||||
get_formatter_bin 'pint',
|
||||
'--stdin-filename',
|
||||
file,
|
||||
}
|
||||
end,
|
||||
eslint_d = function(file)
|
||||
return {
|
||||
get_formatter_bin 'eslint_d',
|
||||
'--stdin',
|
||||
'--stdin-filename',
|
||||
file,
|
||||
'--fix-to-stdout',
|
||||
}
|
||||
end,
|
||||
stylua = function(file)
|
||||
return {
|
||||
get_formatter_bin 'stylua',
|
||||
'--stdin-filepath',
|
||||
file,
|
||||
'-',
|
||||
}
|
||||
end,
|
||||
}
|
||||
|
||||
helpers.format_buffer = function(formatter, buf, cwd)
|
||||
local file = vim.api.nvim_buf_get_name(buf)
|
||||
local input = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), '\n')
|
||||
local result = vim.system(formatter_args[formatter](file), {
|
||||
text = true,
|
||||
stdin = input,
|
||||
cwd = cwd,
|
||||
}):wait()
|
||||
|
||||
if result.code ~= 0 then
|
||||
vim.notify(result.stderr, vim.log.levels.ERROR)
|
||||
return
|
||||
end
|
||||
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(result.stdout, '\n', { plain = true }))
|
||||
end
|
||||
|
||||
helpers.create_bookmark = function(key, bookmark)
|
||||
helpers.map('<Leader>b' .. key, function()
|
||||
vim.cmd('edit ' .. bookmark)
|
||||
end, { desc = 'Navigate to ' .. bookmark })
|
||||
end
|
||||
|
||||
return helpers
|
||||
-- nnoremap <Leader>ev :tabedit $MYVIMRC<CR>
|
||||
|
||||
|
||||
+172
-96
@@ -26,7 +26,7 @@ vim.keymap.set('v', 'p', '"zdP', { desc = 'Paste over selection without yanking
|
||||
-- or just use <C-\><C-n> to exit terminal mode
|
||||
vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })
|
||||
|
||||
vim.keymap.set('n', '<Leader>c', function()
|
||||
vim.keymap.set('n', '<Leader>z', function()
|
||||
vim.treesitter.inspect_tree()
|
||||
end, { desc = 'Treesitter' })
|
||||
|
||||
@@ -121,6 +121,11 @@ vim.keymap.set('n', '<C-l>', '<C-w><C-l>', { desc = 'Move focus to the right win
|
||||
vim.keymap.set('n', '<C-j>', (win_or_treesj)('j', 'Move focus to the lower window or treesj.toggle()'))
|
||||
vim.keymap.set('n', '<C-k>', (win_or_treesj)('k', 'Move focus to the upper window or treesj.toggle()'))
|
||||
|
||||
vim.keymap.set('t', '<C-h>', '<C-\\><C-n><C-w>h', { desc = 'Move focus to the left window' })
|
||||
vim.keymap.set('t', '<C-l>', '<C-\\><C-n><C-w>l', { desc = 'Move focus to the right window' })
|
||||
vim.keymap.set('t', '<C-j>', '<C-\\><C-n><C-w>j', { desc = 'Move focus to the lower window' })
|
||||
vim.keymap.set('t', '<C-k>', '<C-\\><C-n><C-w>k', { desc = 'Move focus to the upper window' })
|
||||
|
||||
vim.keymap.set({ 'i' }, '<C-J>', function()
|
||||
local ls = require 'luasnip'
|
||||
if ls.choice_active() then
|
||||
@@ -177,17 +182,67 @@ vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-f>', function()
|
||||
end, { desc = 'Search within the whole project' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-Y>', '<CMD>Telescope quickfix<CR>', { desc = 'Show quickfix list' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-S-Y>', '<CMD>Telescope quickfixhistory<CR>', { desc = 'Show quickfix history' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-A>', '<CMD>CodeCompanionChat Toggle<CR>', { desc = 'Open AI Actions' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-S-A>', '<CMD>CodeCompanionActions<CR>', { desc = 'Open AI Actions' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i', 't' }, '<C-A>', function()
|
||||
local ai_tools = { 'codex', 'agy', 'opencode' }
|
||||
local ai_win_opts = {
|
||||
width = math.floor(vim.o.columns / 3),
|
||||
vertical = true,
|
||||
split = 'right',
|
||||
}
|
||||
|
||||
local term_bufnr = -1
|
||||
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
|
||||
if vim.bo[bufnr].buftype == 'terminal' then
|
||||
local name = vim.api.nvim_buf_get_name(bufnr)
|
||||
|
||||
for _, tool in ipairs(ai_tools) do
|
||||
if name:match(':' .. tool .. '$') or name:match('/' .. tool .. '$') then
|
||||
term_bufnr = bufnr
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if term_bufnr ~= -1 then
|
||||
local term_winid = vim.fn.bufwinid(term_bufnr)
|
||||
if term_winid ~= -1 then
|
||||
vim.api.nvim_win_hide(term_winid)
|
||||
return
|
||||
end
|
||||
|
||||
vim.api.nvim_open_win(term_bufnr, true, ai_win_opts)
|
||||
vim.cmd.startinsert()
|
||||
return
|
||||
end
|
||||
for _, tool in ipairs(ai_tools) do
|
||||
if vim.fn.executable(tool) == 1 then
|
||||
helpers.open_term {
|
||||
cmd = tool,
|
||||
win_opts = ai_win_opts,
|
||||
buf_opts = {
|
||||
bufhidden = 'hide',
|
||||
modifiable = false,
|
||||
},
|
||||
}
|
||||
return
|
||||
end
|
||||
end
|
||||
vim.api.nvim_echo({ { 'No AI tool found. Please install one of the following: codex, agy, opencode', 'WarningMsg' } }, false, {})
|
||||
end, { desc = 'Toggle AI tool' })
|
||||
-- vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-A>', '<CMD>vsplit term://codex<CR>i', { desc = 'Open AI tool' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-S>', function()
|
||||
require('snacks').scratch()
|
||||
end, { desc = 'Open scratchpad' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-S-S>', function()
|
||||
require('snacks').scratch.select()
|
||||
end, { desc = 'Open scratchpad buffers' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-T>', function()
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i', 't' }, '<C-T>', function()
|
||||
require('snacks').terminal.toggle()
|
||||
end, { desc = 'Open terminal' })
|
||||
end, { desc = 'Toggle terminal' })
|
||||
vim.keymap.set({ 'n', 'v', 'c', 'i' }, '<C-N>', '<Esc>', { desc = 'Enter normal mode' })
|
||||
vim.keymap.set({ 't' }, '<C-N>', '<C-\\><C-N>', { desc = 'Enter normal mode' })
|
||||
vim.keymap.set({ 't' }, '<C-U>', '<C-\\><C-N><C-U>', { desc = 'Scroll up in terminal' })
|
||||
vim.keymap.set({ 't' }, '<C-D>', '<C-\\><C-N><C-D>', { desc = 'Scroll down in terminal' })
|
||||
|
||||
-- Editing helpers
|
||||
vim.keymap.set('i', '<C-O>', '<Esc>o', { desc = 'Add line below' })
|
||||
@@ -257,103 +312,113 @@ vim.keymap.set('n', '<Leader>sGS', '<CMD>Telescope git_stash<CR>', { desc = 'Sea
|
||||
vim.keymap.set('n', '<Leader>]', '<CMD>cnext<CR>', { desc = 'Next item in quickfix list' })
|
||||
vim.keymap.set('n', '<Leader>[', '<CMD>cprevious<CR>', { desc = 'Previous item in quickfix list' })
|
||||
vim.keymap.set('n', 'gd', '<CMD>Telescope lsp_definitions<CR>', { desc = 'Go to definition' })
|
||||
vim.keymap.set('n', '<Leader>r', '<CMD>e!<CR>', { desc = 'Reload buffer' })
|
||||
|
||||
vim.keymap.set('n', '<Leader>r', function()
|
||||
for _, c in ipairs(vim.lsp.get_clients { bufnr = 0 }) do
|
||||
if c.name == 'phpantom_lsp' then
|
||||
c:stop(true)
|
||||
end
|
||||
end
|
||||
vim.cmd 'edit'
|
||||
end, { desc = 'Reload buffer' })
|
||||
|
||||
local function open_test()
|
||||
require('neotest').summary.open()
|
||||
require('neotest').output_panel.open()
|
||||
end
|
||||
-- Testing
|
||||
-- local test_maps = {
|
||||
-- {
|
||||
-- keys = { '<F12>', '<Leader>tn' },
|
||||
-- action = function()
|
||||
-- require('neotest').run.run()
|
||||
-- open_test()
|
||||
-- end,
|
||||
-- desc = 'Run nearest test',
|
||||
-- },
|
||||
-- {
|
||||
-- keys = { '<F9>', '<Leader>ta' },
|
||||
-- action = function()
|
||||
-- require('neotest').run.run { suite = true }
|
||||
-- open_test()
|
||||
-- end,
|
||||
-- desc = 'Run all tests in the project',
|
||||
-- },
|
||||
-- {
|
||||
-- keys = { '<F11>', '<Leader>tp' },
|
||||
-- action = function()
|
||||
-- require('neotest').run.run_last()
|
||||
-- open_test()
|
||||
-- end,
|
||||
-- desc = 'Run previous test again',
|
||||
-- },
|
||||
-- {
|
||||
-- keys = { '<F10>', '<Leader>td' },
|
||||
-- action = function()
|
||||
-- local dap = require 'dap'
|
||||
-- if dap.session() == nil then
|
||||
-- dap.continue()
|
||||
-- end
|
||||
-- require('dapui').open()
|
||||
-- local neotest = require 'neotest'
|
||||
-- local bufnr = vim.api.nvim_get_current_buf()
|
||||
-- local row = vim.api.nvim_win_get_cursor(0)[1] - 1
|
||||
--
|
||||
-- local adapters = neotest.state.adapter_ids()
|
||||
-- local found = false
|
||||
--
|
||||
-- for _, adapter_id in ipairs(adapters) do
|
||||
-- local tree = neotest.state.positions(adapter_id, { buffer = bufnr })
|
||||
-- if tree then
|
||||
-- local nearest = require('neotest.lib.positions').nearest(tree, row)
|
||||
-- if nearest and nearest:data().type ~= 'file' then
|
||||
-- neotest.run.run()
|
||||
-- found = true
|
||||
-- break
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
--
|
||||
-- if not found then
|
||||
-- neotest.run.run_last()
|
||||
-- end
|
||||
-- end,
|
||||
-- desc = 'Run last test with debugger',
|
||||
-- },
|
||||
-- }
|
||||
--
|
||||
-- for _, map_info in ipairs(test_maps) do
|
||||
-- for _, key in ipairs(map_info.keys) do
|
||||
-- vim.keymap.set('n', key, map_info.action, { desc = map_info.desc })
|
||||
-- end
|
||||
-- end
|
||||
-- vim.keymap.set('n', '<Leader>tf', function()
|
||||
-- require('neotest').run.run(vim.fn.expand '%')
|
||||
-- open_test()
|
||||
-- end, { desc = 'Run all tests in the current file' })
|
||||
-- vim.keymap.set('n', '<Leader>tc', function()
|
||||
-- require('neotest').summary.close()
|
||||
-- require('neotest').output_panel.close()
|
||||
-- end, { desc = 'Close test panels' })
|
||||
local test_maps = {
|
||||
{
|
||||
keys = { '<Leader>tc' },
|
||||
action = function()
|
||||
require('neotest').summary.close()
|
||||
require('neotest').output_panel.close()
|
||||
end,
|
||||
desc = 'Close [T]est panels',
|
||||
},
|
||||
{
|
||||
keys = { '<Leader>tn' },
|
||||
action = function()
|
||||
require('neotest').run.run()
|
||||
open_test()
|
||||
end,
|
||||
desc = 'Run nearest [T]est',
|
||||
},
|
||||
{
|
||||
keys = { '<Leader>ta' },
|
||||
action = function()
|
||||
require('neotest').run.run { suite = true }
|
||||
open_test()
|
||||
end,
|
||||
desc = 'Run all [T]ests in the project',
|
||||
},
|
||||
{
|
||||
keys = { '<Leader>tp' },
|
||||
action = function()
|
||||
require('neotest').run.run_last()
|
||||
open_test()
|
||||
end,
|
||||
desc = 'Run previous [T]est again',
|
||||
},
|
||||
{
|
||||
keys = { '<Leader>td' },
|
||||
action = function()
|
||||
local dap = require 'dap'
|
||||
if dap.session() == nil then
|
||||
dap.continue()
|
||||
end
|
||||
require('dapui').open()
|
||||
local neotest = require 'neotest'
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1] - 1
|
||||
|
||||
-- vim.keymap.set('i', '<Tab>', function()
|
||||
-- local copilot = require 'copilot.suggestion'
|
||||
-- local ls = require 'luasnip'
|
||||
-- if copilot.is_visible() then
|
||||
-- vim.api.nvim_echo({{'Accepting copilot suggestion'}}, false, {})
|
||||
-- copilot.accept()
|
||||
-- copilot.dismiss()
|
||||
-- elseif ls.jumpable(1) then
|
||||
-- vim.api.nvim_echo({{'Jumping in snippet'}}, false, {})
|
||||
-- ls.jump()
|
||||
-- else
|
||||
-- vim.api.nvim_echo({{'Inserting tab'}}, false, {})
|
||||
-- vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Tab>', true, true, true), 'n', true)
|
||||
-- end
|
||||
-- end, { desc = 'Luasnip accept copilot or jump forward' })
|
||||
local adapters = neotest.state.adapter_ids()
|
||||
local found = false
|
||||
|
||||
vim.g.neotest_debug = true
|
||||
for _, adapter_id in ipairs(adapters) do
|
||||
local tree = neotest.state.positions(adapter_id, { buffer = bufnr })
|
||||
if tree then
|
||||
local nearest = require('neotest.lib.positions').nearest(tree, row)
|
||||
if nearest and nearest:data().type ~= 'file' then
|
||||
neotest.run.run()
|
||||
found = true
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not found then
|
||||
neotest.run.run_last()
|
||||
end
|
||||
vim.g.neotest_debug = nil
|
||||
end,
|
||||
desc = 'Run last [T]est with debugger',
|
||||
},
|
||||
}
|
||||
|
||||
for _, map_info in ipairs(test_maps) do
|
||||
for _, key in ipairs(map_info.keys) do
|
||||
vim.keymap.set('n', key, map_info.action, { desc = map_info.desc })
|
||||
end
|
||||
end
|
||||
vim.keymap.set('n', '<Leader>tf', function()
|
||||
require('neotest').run.run(vim.fn.expand '%')
|
||||
open_test()
|
||||
end, { desc = 'Run all tests in the current file' })
|
||||
vim.keymap.set('n', '<Leader>tc', function()
|
||||
require('neotest').summary.close()
|
||||
require('neotest').output_panel.close()
|
||||
end, { desc = 'Close test panels' })
|
||||
|
||||
vim.keymap.set('i', '<Tab>', function()
|
||||
if ls.jumpable(1) then
|
||||
vim.api.nvim_echo({ { 'Jumping in snippet' } }, false, {})
|
||||
ls.jump()
|
||||
else
|
||||
vim.api.nvim_echo({ { 'Inserting tab' } }, false, {})
|
||||
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Tab>', true, true, true), 'n', true)
|
||||
end
|
||||
end, { desc = 'Luasnip accept copilot or jump forward' })
|
||||
|
||||
-- Leaving this commented out, I will try the format command instead
|
||||
-- "A command to properly indent json code
|
||||
@@ -363,7 +428,7 @@ end
|
||||
-- directory if the file does not exist
|
||||
vim.keymap.set('n', '<Leader>es', function()
|
||||
local ft = vim.bo.filetype
|
||||
if ft == 'vue' then
|
||||
if ft == 'vue' or ft == 'typescript' then
|
||||
ft = 'javascript'
|
||||
end
|
||||
local snippets_dir = vim.fn.stdpath 'config' .. '/lua/snippets'
|
||||
@@ -371,4 +436,15 @@ vim.keymap.set('n', '<Leader>es', function()
|
||||
vim.cmd('tabedit ' .. snippets_file)
|
||||
end, { desc = 'Edit snippets file' })
|
||||
|
||||
vim.keymap.set('n', '<Leader>cz', function()
|
||||
require('utils/cheatsheets').open 'zsh'
|
||||
end, { desc = 'Open zsh cheat sheet' })
|
||||
vim.keymap.set('n', '<Leader>cb', function()
|
||||
require('utils/cheatsheets').open 'bash'
|
||||
end, { desc = 'Open bash cheat sheet' })
|
||||
vim.keymap.set('n', '<Leader>cr', function()
|
||||
require('utils/cheatsheets').open 'regex'
|
||||
end, { desc = 'Open RegExp cheat sheet' })
|
||||
|
||||
require('helpers').edit_cf('k', '/lua/keymap.lua')
|
||||
|
||||
|
||||
@@ -47,3 +47,4 @@ require('lazy').setup({
|
||||
})
|
||||
|
||||
require('helpers').edit_cf('l', '/lua/lazy_init.lua')
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
require('helpers').edit_cf('pa', '/lua/plugins/aider.lua')
|
||||
|
||||
local change_model_function = function(model)
|
||||
return function()
|
||||
require('nvim_aider').api.send_command('/model', model)
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
'GeorgesAlkhouri/nvim-aider',
|
||||
cmd = 'Aider',
|
||||
keys = {
|
||||
{ '<leader>a/', '<cmd>Aider toggle<cr>', desc = 'Toggle Aider' },
|
||||
{ '<leader>as', '<cmd>Aider send<cr>', desc = 'Send to Aider', mode = { 'n', 'v' } },
|
||||
{ '<leader>ac', '<cmd>Aider command<cr>', desc = 'Aider Commands' },
|
||||
{ '<leader>ab', '<cmd>Aider buffer<cr>', desc = 'Send Buffer' },
|
||||
{ '<leader>a+', '<cmd>Aider add<cr>', desc = 'Add File' },
|
||||
{ '<leader>a-', '<cmd>Aider drop<cr>', desc = 'Drop File' },
|
||||
{ '<leader>ar', '<cmd>Aider add readonly<cr>', desc = 'Add Read-Only' },
|
||||
{ '<leader>aR', '<cmd>Aider reset<cr>', desc = 'Reset Session' },
|
||||
-- Example nvim-tree.lua integration if needed
|
||||
{ '<leader>a+', '<cmd>AiderTreeAddFile<cr>', desc = 'Add File from Tree to Aider', ft = 'NvimTree' },
|
||||
{ '<leader>a-', '<cmd>AiderTreeDropFile<cr>', desc = 'Drop File from Tree from Aider', ft = 'NvimTree' },
|
||||
{ '<leader>am4', change_model_function 'gpt-4.1', desc = 'Switch aider model to GPT-4.1' },
|
||||
{ '<leader>amo', change_model_function 'openai/o4-mini', desc = 'Switch aider model to o4-mini' },
|
||||
{ '<leader>amg', change_model_function 'openai/gemini-2.5-pro', desc = 'Switch aider model to Gemini 2.5 Pro' },
|
||||
{ '<leader>ams', change_model_function 'openai/claude-sonnet-4', desc = 'Switch aider model to Claude Sonnet 4' },
|
||||
},
|
||||
dependencies = {
|
||||
'folke/snacks.nvim',
|
||||
--- The below dependencies are optional
|
||||
'catppuccin/nvim',
|
||||
'nvim-tree/nvim-tree.lua',
|
||||
--- Neo-tree integration
|
||||
{
|
||||
'nvim-neo-tree/neo-tree.nvim',
|
||||
opts = function(_, opts)
|
||||
-- Example mapping configuration (already set by default)
|
||||
-- opts.window = {
|
||||
-- mappings = {
|
||||
-- ["+"] = { "nvim_aider_add", desc = "add to aider" },
|
||||
-- ["-"] = { "nvim_aider_drop", desc = "drop from aider" }
|
||||
-- ["="] = { "nvim_aider_add_read_only", desc = "add read-only to aider" }
|
||||
-- }
|
||||
-- }
|
||||
require('nvim_aider.neo_tree').setup(opts)
|
||||
end,
|
||||
},
|
||||
},
|
||||
config = function()
|
||||
require('nvim_aider').setup {
|
||||
aider_cmd = 'aider',
|
||||
args = {
|
||||
'--config=$HOME/.config/aider/aider.yaml',
|
||||
'--env-file=$(pwd)/aider.env',
|
||||
'--watch',
|
||||
'--architect',
|
||||
},
|
||||
auto_reload = true,
|
||||
win = {
|
||||
wo = { winbar = 'Aider' },
|
||||
style = 'nvim_aider',
|
||||
position = 'bottom',
|
||||
},
|
||||
}
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
return {
|
||||
'ricardoramirezr/blade-nav.nvim',
|
||||
ft = { 'blade', 'php' }, -- optional, improves startup time
|
||||
config = function()
|
||||
require('blade-nav').setup {
|
||||
close_tag_on_complete = false,
|
||||
integrations = {
|
||||
cmp = false,
|
||||
coq = false,
|
||||
},
|
||||
}
|
||||
end,
|
||||
}
|
||||
|
||||
@@ -56,9 +56,10 @@ return {
|
||||
providers = {
|
||||
lazydev = { module = 'lazydev.integrations.blink', score_offset = 100 },
|
||||
-- avante = { module = 'blink-cmp-avante', name = 'Avante', opts = {} },
|
||||
},
|
||||
per_filetype = {
|
||||
codecompanion = { 'codecompanion' },
|
||||
['blade-nav'] = {
|
||||
name = 'blade-nav',
|
||||
module = 'blade-nav.integrations.blink',
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -77,3 +78,4 @@ return {
|
||||
signature = { enabled = true },
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,339 +0,0 @@
|
||||
return {
|
||||
'olimorris/codecompanion.nvim',
|
||||
config = function()
|
||||
require('codecompanion').setup {
|
||||
adapters = {
|
||||
http = {
|
||||
default = function()
|
||||
if (require('helpers').has_copilot()) then
|
||||
return require('codecompanion.adapters').extend('copilot', {
|
||||
schema = {
|
||||
model = {
|
||||
default = vim.env.DEFAULT_AI_MODEL,
|
||||
},
|
||||
max_tokens = {
|
||||
default = 1000000,
|
||||
},
|
||||
}
|
||||
})
|
||||
end
|
||||
return require('codecompanion.adapters').extend('openai_compatible', {
|
||||
env = {
|
||||
url = vim.env.DEFAULT_OPENAI_API_BASE,
|
||||
api_key = vim.env.DEFAULT_OPENAI_API_KEY,
|
||||
chat_url = '/v1/chat/completions',
|
||||
models_endpoint = '/v1/models',
|
||||
},
|
||||
schema = {
|
||||
model = {
|
||||
default = vim.env.DEFAULT_AI_MODEL,
|
||||
},
|
||||
max_tokens = {
|
||||
default = 1000000,
|
||||
},
|
||||
},
|
||||
})
|
||||
end,
|
||||
},
|
||||
},
|
||||
display = {
|
||||
chat = {
|
||||
show_settings = true,
|
||||
start_in_insert_mode = false,
|
||||
},
|
||||
},
|
||||
strategies = {
|
||||
chat = {
|
||||
adapter = 'default',
|
||||
slash_commands = {
|
||||
-- codebase = require('vectorcode.integrations').codecompanion.chat.make_slash_command(),
|
||||
},
|
||||
tools = {
|
||||
-- vectorcode = {
|
||||
-- description = 'Run VectorCode to retrieve the project context.',
|
||||
-- callback = require('vectorcode.integrations').codecompanion.chat.make_tool(),
|
||||
-- },
|
||||
['cmd_runner'] = {
|
||||
opts = {
|
||||
requires_approval = false,
|
||||
},
|
||||
},
|
||||
},
|
||||
roles = {
|
||||
---@type string|fun(adapter: CodeCompanion.Adapter): string
|
||||
llm = function(adapter)
|
||||
return 'CodeCompanion (' .. adapter.formatted_name .. ': ' .. adapter.parameters.model .. ')'
|
||||
end,
|
||||
},
|
||||
keymaps = {
|
||||
send = {
|
||||
modes = { n = '<C-s>', i = '<C-s>' },
|
||||
},
|
||||
close = {
|
||||
modes = { n = '<C-c>', i = '<C-c>' },
|
||||
},
|
||||
},
|
||||
},
|
||||
inline = {
|
||||
adapter = {
|
||||
name = 'default',
|
||||
model = vim.env.FAST_MODEL,
|
||||
},
|
||||
},
|
||||
cmd = {
|
||||
adapter = {
|
||||
name = 'default',
|
||||
model = vim.env.FAST_MODEL,
|
||||
},
|
||||
},
|
||||
},
|
||||
extensions = {
|
||||
mcphub = {
|
||||
callback = 'mcphub.extensions.codecompanion',
|
||||
opts = {
|
||||
show_result_in_chat = true,
|
||||
make_vars = true,
|
||||
make_slash_commands = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
system_prompt = function(opts)
|
||||
local language = opts.language or 'English'
|
||||
return string.format(
|
||||
[[You are an AI programming assistant named "CodeCompanion". You are currently plugged into the Neovim text editor on a user's machine.
|
||||
|
||||
Your core tasks include:
|
||||
- Answering general programming questions.
|
||||
- Explaining how the code in a Neovim buffer works.
|
||||
- Reviewing the selected code from a Neovim buffer.
|
||||
- Generating unit tests for the selected code.
|
||||
- Proposing fixes for problems in the selected code.
|
||||
- Scaffolding code for a new workspace.
|
||||
- Finding relevant code to the user's query.
|
||||
- Proposing fixes for test failures.
|
||||
- Answering questions about Neovim.
|
||||
- Running tools.
|
||||
|
||||
You must:
|
||||
- Follow the user's requirements carefully and to the letter.
|
||||
- Keep your answers short and impersonal, especially if the user's context is outside your core tasks.
|
||||
- Minimize additional prose unless clarification is needed.
|
||||
- Use Markdown formatting in your answers.
|
||||
- Include the programming language name at the start of each Markdown code block.
|
||||
- Avoid including line numbers in code blocks.
|
||||
- Avoid wrapping the whole response in triple backticks.
|
||||
- Only return code that's directly relevant to the task at hand. You may omit code that isn’t necessary for the solution.
|
||||
- Avoid using H1, H2 or H3 headers in your responses as these are reserved for the user.
|
||||
- Use actual line breaks in your responses; only use "\n" when you want a literal backslash followed by 'n'.
|
||||
- All non-code text responses must be written in the %s language indicated.
|
||||
- Multiple, different tools can be called as part of the same response.
|
||||
- Use full path names when using tools to read and modify files.
|
||||
|
||||
When given a task:
|
||||
1. Think step-by-step and, unless the user requests otherwise or the task is very simple, describe your plan in detailed pseudocode.
|
||||
2. Output the final code in a single code block, ensuring that only relevant code is included.
|
||||
3. End your response with a short suggestion for the next user turn that directly supports continuing the conversation.
|
||||
4. Provide exactly one complete reply per conversation turn.
|
||||
5. If necessary, execute multiple tools in a single turn.]],
|
||||
language
|
||||
)
|
||||
end,
|
||||
prompt_library = {
|
||||
['Code Expert'] = {
|
||||
strategy = 'chat',
|
||||
description = 'Get some special advice from an LLM.',
|
||||
opts = {
|
||||
mapping = '<Leader>ce',
|
||||
modes = { 'v' },
|
||||
short_name = 'expert',
|
||||
auto_submit = true,
|
||||
stop_context_insertion = true,
|
||||
user_prompt = true,
|
||||
},
|
||||
prompts = {
|
||||
{
|
||||
role = 'system',
|
||||
content = function(context)
|
||||
return 'I want you to act as a senior'
|
||||
.. context.filetype
|
||||
.. 'developer I will ask you specific questions and I want you to return concise explanations and codeblock examples.'
|
||||
end,
|
||||
},
|
||||
{
|
||||
role = 'user',
|
||||
content = function(context)
|
||||
local text = require('codecompanion.helpers.actions').get_code(context.start_line, context.end_line)
|
||||
|
||||
return 'I have the following code:\n\n```' .. context.filetype .. '\n' .. text .. '\n```\n\n'
|
||||
end,
|
||||
opts = {
|
||||
contains_code = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
['Games Master'] = {
|
||||
strategy = 'chat',
|
||||
description = 'A personal Games Master Assistant.',
|
||||
opts = {
|
||||
user_prompt = false,
|
||||
},
|
||||
prompts = {
|
||||
{
|
||||
role = 'system',
|
||||
content = [[
|
||||
You are a personal Games Master Assistant. You are currently plugged in to the Neovim text editor on a user's machine.
|
||||
|
||||
Your core tasks include:
|
||||
- Crafting engaging role playing sessions
|
||||
- Building immersive and authentic worlds
|
||||
- Creating compelling characters for the players to interact with and drive the story
|
||||
- Reviewing session summaries and building on them
|
||||
- Providing advice on how to bring sessions to life
|
||||
- Create exciting encounters to challenge the players
|
||||
- Encounters can be combat, social, or exploration based
|
||||
- There should always be a story reason for the encounter
|
||||
- Combine player character backgrounds and motivations into the world
|
||||
- Build tension and drama into the game
|
||||
- Ensure each session provides opportunities for the players to engage with and drive the story
|
||||
|
||||
You must:
|
||||
- Follow the user's requirements carefully and to the letter.
|
||||
- Keep answers focused on the task at hand.
|
||||
- Provide original ideas and suggestions.
|
||||
- Use Markdown formatting in your answers.
|
||||
- Use actual line breaks instead of '\n' in your response to begin new lines.
|
||||
- Use '\n' only when you want a literal backslash followed by a character 'n'.
|
||||
- All responses must be written in English.
|
||||
- Multiple, different tools can be called as part of the same response.
|
||||
- Help sessions be fast paced and fun.
|
||||
- Ensure there are multiple soltuions to each problem.
|
||||
- Avoid railroading the players.
|
||||
|
||||
When given a task:
|
||||
1. Consider the existing world and characters. Use tools to gather information that may be relevant.
|
||||
2. Provide exactly one complete reply per conversation turn.
|
||||
3. If necessary, execute multiple tools in a single turn.
|
||||
]],
|
||||
},
|
||||
{
|
||||
role = 'user',
|
||||
content = '',
|
||||
},
|
||||
},
|
||||
},
|
||||
['PHPStan Fixer'] = {
|
||||
strategy = 'workflow',
|
||||
description = 'Use a workflow to fix PHPStan errors until there are none left.',
|
||||
opts = {
|
||||
short_name = 'phpstan',
|
||||
},
|
||||
prompts = {
|
||||
{
|
||||
{
|
||||
name = 'Run PHPStan',
|
||||
role = 'user',
|
||||
opts = { auto_submit = false },
|
||||
content = function()
|
||||
-- Enable turbo mode!!!
|
||||
vim.g.codecompanion_auto_tool_mode = true
|
||||
|
||||
return [[PHPStan is a static analysis tool for PHP. It is currently reporting errors in the code. Your task is to fix these errors and run PHPStan again until there are no errors left.
|
||||
|
||||
First of all use the @cmd_runner tool to run the `composer type-check` command. This will run PHPStan and output type errors in the code.]]
|
||||
end,
|
||||
},
|
||||
},
|
||||
{
|
||||
{
|
||||
name = 'Fetch files',
|
||||
role = 'user',
|
||||
opts = { auto_submit = false },
|
||||
content = function()
|
||||
-- Enable turbo mode!!!
|
||||
vim.g.codecompanion_auto_tool_mode = true
|
||||
|
||||
return 'PHPStan has reported errors. Look at the output and see where the files are reported. Use the @mcp tool to read all the offending files so you can implement the fixes.'
|
||||
end,
|
||||
},
|
||||
},
|
||||
{
|
||||
{
|
||||
name = 'Fix errors and run',
|
||||
role = 'user',
|
||||
opts = { auto_submit = false },
|
||||
content = function()
|
||||
-- Enable turbo mode!!!
|
||||
vim.g.codecompanion_auto_tool_mode = true
|
||||
|
||||
return [[### Instructions
|
||||
Now you have the errors and the appropriate context you can fix the errors.
|
||||
|
||||
### Steps to Follow
|
||||
|
||||
You are required to analyse the PHPStan errors and write code to fix them.
|
||||
|
||||
Reason through the errors and write out the possible causes and fixes for each.
|
||||
|
||||
1. Then use the @mcp tool to update the files with the fixes. When editing files use the full path name including the project name /Users/chris/Code/Sites/hylark/
|
||||
2. After editing the files use the @cmd_runner tool again to run the `composer type-check` command to see if any errors remain.
|
||||
|
||||
We'll repeat this cycle until there are no errors. Ensure no deviations from these steps.
|
||||
|
||||
### Tips for fixing PHPStan errors
|
||||
- Do not ignore the errors
|
||||
- Use Webmozart Assert library to narrow types
|
||||
- Most errors are due to missing docblocks or calling methods/parameters on types that PHPStan cannot infer
|
||||
- Use fully qualified namespaces for classes in doc blocks
|
||||
- The code is working so fixes should not change the behaviour of the code
|
||||
- There are some custom types in this project that help define types
|
||||
- closure<T> creates T|Closure(): T, you can add a second argument to specify the depth so closure<T, 2> creates T|Closure(): T|Closure(): (Closure(): T). This is useful because GraphQL queries can return closures.
|
||||
- promise<T> creates T|SyncPromise<T>, as with closure, you can add a second argument for depth. You can also add a third argument if T is an array to allow adding promises to each value, so promise<array{a: int}, 1, 1> will create array{a: int}|SyncPromise<array{a: int}>|array{a: SyncPromise<int>}|SyncPromise<array{a: SyncPromise<int>}>. This is useful because GraphQL queries can return promises and arrays of promises.
|
||||
- GraphQL schema types (these should only be used in the Query classes)
|
||||
- GArgs<T> will look at the GraphQL schema and create an array type for the arguments of a field on one of the root types (Query or Mutation)
|
||||
- GArgs<TType, TField> will create a type for the arguments of a field on a type
|
||||
- GType<T> will create an type for a specific GraphQL type, input, interface, or enum
|
||||
- GVal<T> will create a type for the return value of a field on one of the root types (Query or Mutation)
|
||||
- GVal<TType, TField> will create a type for the return value of a field on a type
|
||||
- pick<T, TKeys> will create an array from T with only the keys in TKeys (e.g. pick<array{a: int, b: int, c: int}, 'a'|'b'> will create array{a: int, b: int})
|
||||
- except<T, TKeys> works like pick only it excludes the keys in TKeys (e.g. except<array{a: int, b: int, c: int}, 'a'|'b'> will create array{c: int})
|
||||
- union<T, U> creates a union array of T and U (e.g. union<array{a: int}, array{b: int}> will create array{a: int, b: int})]]
|
||||
end,
|
||||
},
|
||||
},
|
||||
{
|
||||
{
|
||||
name = 'Repeat On Failure',
|
||||
role = 'user',
|
||||
opts = { auto_submit = false },
|
||||
-- Scope this prompt to the cmd_runner tool
|
||||
condition = function()
|
||||
return _G.codecompanion_current_tool == 'cmd_runner'
|
||||
end,
|
||||
-- Repeat until the tests pass, as indicated by the testing flag
|
||||
-- which the cmd_runner tool sets on the chat buffer
|
||||
repeat_until = function(chat)
|
||||
-- Check if the last message in the chat buffer contains "[ERROR] found"
|
||||
local messages = chat.messages
|
||||
local last_message = messages[#messages]
|
||||
if last_message and last_message.role == 'assistant' then
|
||||
local content = last_message.content
|
||||
return not content:find '[ERROR] found'
|
||||
end
|
||||
return true
|
||||
end,
|
||||
content = 'PHPStan is still reporting errors. Edit the code to fix the errors and run PHPStan again.',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
end,
|
||||
dependencies = {
|
||||
'nvim-lua/plenary.nvim',
|
||||
'nvim-treesitter/nvim-treesitter',
|
||||
-- 'Davidyz/VectorCode',
|
||||
'ravitemer/mcphub.nvim',
|
||||
},
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
return { -- Autoformat
|
||||
'stevearc/conform.nvim',
|
||||
event = { 'BufWritePre' },
|
||||
cmd = { 'ConformInfo' },
|
||||
keys = {
|
||||
{
|
||||
'<leader>f',
|
||||
function()
|
||||
require('conform').format { async = true, lsp_format = 'fallback' }
|
||||
end,
|
||||
mode = '',
|
||||
desc = '[F]ormat buffer',
|
||||
},
|
||||
},
|
||||
config = function()
|
||||
require('conform').setup({
|
||||
notify_on_error = false,
|
||||
format_on_save = function(bufnr)
|
||||
-- Disable "format_on_save lsp_fallback" for languages that don't
|
||||
-- have a well standardized coding style. You can add additional
|
||||
-- languages here or re-enable it for the disabled ones.
|
||||
local disable_filetypes = { c = true, cpp = true, vue = true, js = true }
|
||||
if disable_filetypes[vim.bo[bufnr].filetype] then
|
||||
return nil
|
||||
else
|
||||
return {
|
||||
timeout_ms = 500,
|
||||
lsp_format = 'fallback',
|
||||
}
|
||||
end
|
||||
end,
|
||||
formatters_by_ft = {
|
||||
lua = { 'stylua' },
|
||||
php = { 'pint' },
|
||||
-- Conform can also run multiple formatters sequentially
|
||||
-- python = { "isort", "black" },
|
||||
--
|
||||
-- You can use 'stop_after_first' to run the first available formatter from the list
|
||||
-- javascript = { "prettierd", "prettier", stop_after_first = true },
|
||||
},
|
||||
log_level = vim.log.levels.DEBUG
|
||||
})
|
||||
end
|
||||
}
|
||||
@@ -180,40 +180,6 @@ return {
|
||||
vim.fn.sign_define(tp, { text = icon, texthl = hl, numhl = hl })
|
||||
end
|
||||
|
||||
local function get_php_ini_dir()
|
||||
local handle = io.popen 'php --ini 2>/dev/null'
|
||||
if not handle then
|
||||
return nil
|
||||
end
|
||||
local result = handle:read '*a'
|
||||
handle:close()
|
||||
local dir = result:match 'Scan for additional .ini files in:%s+([^\n]+)'
|
||||
if dir and dir:find '%(none%)' == nil then
|
||||
return vim.trim(dir)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function enable_xdebug()
|
||||
local ini_dir = get_php_ini_dir()
|
||||
if not ini_dir then
|
||||
return
|
||||
end
|
||||
os.execute(string.format('mv "%s/20-xdebug.ini.disabled" "%s/20-xdebug.ini" 2>/dev/null', ini_dir, ini_dir))
|
||||
end
|
||||
|
||||
local function disable_xdebug()
|
||||
local ini_dir = get_php_ini_dir()
|
||||
if not ini_dir then
|
||||
return
|
||||
end
|
||||
os.execute(string.format('mv "%s/20-xdebug.ini" "%s/20-xdebug.ini.disabled" 2>/dev/null', ini_dir, ini_dir))
|
||||
end
|
||||
|
||||
dap.listeners.after.event_initialized['xdebug_enable'] = enable_xdebug
|
||||
dap.listeners.before.event_terminated['xdebug_disable'] = disable_xdebug
|
||||
dap.listeners.before.event_exited['xdebug_disable'] = disable_xdebug
|
||||
|
||||
-- dap.listeners.after.event_output['neotest_display'] = function(_, body)
|
||||
-- require('neotest').output.open { enter = true, last = true }
|
||||
-- end
|
||||
|
||||
+18
-4
@@ -13,9 +13,10 @@ return {
|
||||
'saghen/blink.cmp',
|
||||
'folke/lazydev.nvim',
|
||||
},
|
||||
build = function()
|
||||
vim.fn.system 'composer global require jetbrains/phpstorm-stubs friendsofphp/php-cs-fixer'
|
||||
end,
|
||||
-- Might not be necessary for phpantom
|
||||
-- build = function()
|
||||
-- vim.fn.system 'composer global require jetbrains/phpstorm-stubs friendsofphp/php-cs-fixer'
|
||||
-- end,
|
||||
config = function()
|
||||
-- This function gets run when an LSP attaches to a particular buffer.
|
||||
-- That is to say, every time a new file is opened that is associated with
|
||||
@@ -115,6 +116,12 @@ return {
|
||||
},
|
||||
}
|
||||
|
||||
-- vim.lsp.config['phpantom'] = {
|
||||
-- filetypes = { 'php' },
|
||||
-- cmd = { 'phpantom_lsp' },
|
||||
-- root_markers = { 'composer.json', '.git' },
|
||||
-- }
|
||||
|
||||
-- Enable the following language servers
|
||||
--
|
||||
-- Add any additional override configuration in the following tables. Available keys are:
|
||||
@@ -139,7 +146,11 @@ return {
|
||||
},
|
||||
},
|
||||
},
|
||||
phpactor = {},
|
||||
phpantom_lsp = {
|
||||
filetypes = { 'php' },
|
||||
cmd = { 'phpantom_lsp' },
|
||||
root_markers = { 'composer.json', '.git' },
|
||||
},
|
||||
vtsls = {
|
||||
settings = {
|
||||
vtsls = {
|
||||
@@ -195,6 +206,9 @@ return {
|
||||
local ensure_installed = vim.tbl_keys(servers or {})
|
||||
vim.list_extend(ensure_installed, {
|
||||
'stylua', -- Used to format Lua code
|
||||
'blade-formatter',
|
||||
'eslint_d',
|
||||
'pint',
|
||||
})
|
||||
|
||||
-- LSP servers and clients are able to communicate to each other what features they support.
|
||||
|
||||
@@ -5,6 +5,16 @@ return {
|
||||
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' }, -- if you prefer nvim-web-devicons
|
||||
---@module 'render-markdown'
|
||||
---@type render.md.UserConfig
|
||||
opts = {},
|
||||
ft = { 'markdown', 'codecompanion' },
|
||||
config = function()
|
||||
require('render-markdown').setup {
|
||||
latex = {
|
||||
enabled = true,
|
||||
render_modes = true,
|
||||
},
|
||||
anti_conceal = {
|
||||
enabled = false,
|
||||
},
|
||||
}
|
||||
end,
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ return {
|
||||
config = function()
|
||||
local ls = require 'luasnip'
|
||||
ls.filetype_extend('vue', { 'javascript' })
|
||||
ls.filetype_extend('typescript', { 'javascript' })
|
||||
local snippets_dir = vim.fn.stdpath 'config' .. '/lua/snippets'
|
||||
require('luasnip.loaders.from_lua').load {
|
||||
paths = { snippets_dir },
|
||||
@@ -24,3 +25,4 @@ return {
|
||||
end,
|
||||
opts = {},
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
require('helpers').edit_cf('pt', '/lua/plugins/test.lua')
|
||||
|
||||
return {
|
||||
'nvim-neotest/neotest',
|
||||
dependencies = {
|
||||
'V13Axel/neotest-pest',
|
||||
},
|
||||
config = function()
|
||||
require('neotest').setup {
|
||||
adapters = {
|
||||
require 'neotest-pest' {
|
||||
sail_enabled = function()
|
||||
return false
|
||||
end,
|
||||
parallel = 8,
|
||||
pest_cmd = function()
|
||||
local cmd = vim.g.neotest_pest_cmd or { 'vendor/bin/pest' }
|
||||
if vim.g.neotest_debug then
|
||||
cmd = { 'sh', '-c', 'XDEBUG_MODE=debug exec ' .. table.concat(cmd, ' ') .. ' "$@"' }
|
||||
end
|
||||
return cmd
|
||||
end,
|
||||
},
|
||||
},
|
||||
}
|
||||
end,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
return {
|
||||
'nvim-treesitter/nvim-treesitter-textobjects',
|
||||
branch = 'main',
|
||||
config = function()
|
||||
-- put your config here
|
||||
end,
|
||||
}
|
||||
|
||||
@@ -7,9 +7,11 @@ return {
|
||||
config = function()
|
||||
local langs = {
|
||||
'bash',
|
||||
'zsh',
|
||||
'c',
|
||||
'diff',
|
||||
'html',
|
||||
'latex',
|
||||
'lua',
|
||||
'luadoc',
|
||||
'markdown',
|
||||
@@ -23,6 +25,7 @@ return {
|
||||
'javascript',
|
||||
'typescript',
|
||||
'css',
|
||||
'blade',
|
||||
}
|
||||
require('nvim-treesitter').install(langs)
|
||||
|
||||
|
||||
+29
-246
@@ -6,263 +6,48 @@ local M = {}
|
||||
local helpers = require 'helpers'
|
||||
local map = helpers.map
|
||||
|
||||
local function relevant_directory_from_cwd(cwd)
|
||||
local basename = vim.fn.fnamemodify(cwd, ':t')
|
||||
local nested_folder_names = { 'server', 'frontend', 'frontend2', 'client', 'backend' }
|
||||
if vim.tbl_contains(nested_folder_names, basename) then
|
||||
local parent = vim.fn.fnamemodify(cwd, ':h')
|
||||
return relevant_directory_from_cwd(parent)
|
||||
end
|
||||
return cwd
|
||||
end
|
||||
|
||||
-- Get the last folder name from a path
|
||||
local function project_name_from_cwd(cwd)
|
||||
return vim.fn.fnamemodify(cwd, ':t')
|
||||
local function project_name_from_dir(dir)
|
||||
return vim.fn.fnamemodify(dir, ':t')
|
||||
end
|
||||
|
||||
local function command_with_dir(dir, cmd)
|
||||
if dir then
|
||||
return '!cd ' .. dir .. ' && ' .. cmd
|
||||
end
|
||||
return '!' .. cmd
|
||||
end
|
||||
|
||||
local function make_laravel_file(dir, cmd)
|
||||
local cwd = vim.fn.getcwd()
|
||||
if dir then
|
||||
vim.fn.chdir(dir)
|
||||
end
|
||||
vim.ui.input({ prompt = 'Make: ' .. cmd }, function(input)
|
||||
if input == nil then
|
||||
vim.fn.chdir(cwd)
|
||||
return
|
||||
end
|
||||
|
||||
local output = vim.system({ 'vendor/bin/sail', 'artisan', 'make:' .. cmd, input }):wait().stdout
|
||||
local new_file = output:match '%[([%w%./]+)%]'
|
||||
if new_file ~= nil then
|
||||
vim.cmd('edit ' .. new_file)
|
||||
end
|
||||
vim.fn.chdir(cwd)
|
||||
end)
|
||||
end
|
||||
|
||||
local function get_scope_from_file(filename)
|
||||
local ext = filename:match '%.(%w+)$'
|
||||
local base_name = filename:match '^(%w+)%.?%w*$'
|
||||
local extensionSuffixes = {
|
||||
php = { 'Controller', 'Repository', 'StoreRequest', 'UpdateRequest', 'Request', 'Resource', 'Test', 'Observer', 'Policy', 'Seeder', 'Factory' },
|
||||
['[jt]s'] = { 'Service' },
|
||||
}
|
||||
for suffix_type, _ in pairs(extensionSuffixes) do
|
||||
if ext:match(suffix_type) then
|
||||
local suffixes = extensionSuffixes[suffix_type]
|
||||
for _, suffix in ipairs(suffixes) do
|
||||
local scope = base_name:match('^(%w+)' .. suffix .. '$')
|
||||
if scope then
|
||||
return scope
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return base_name
|
||||
end
|
||||
|
||||
local function navigate_using_suffix(suffix)
|
||||
local scope = get_scope_from_file(vim.fn.expand '%:t')
|
||||
local file_name = scope .. suffix
|
||||
local file_path = vim.system({ 'git', 'ls-files', file_name, '**/' .. file_name }):wait().stdout
|
||||
if file_path ~= '' then
|
||||
vim.cmd('edit ' .. file_path)
|
||||
else
|
||||
vim.notify('File ' .. file_name .. ' not found in git ls-files', vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
|
||||
local function create_navigation_maps(maps)
|
||||
for key, suffix_and_name in pairs(maps) do
|
||||
map('<Leader>n' .. key, function()
|
||||
navigate_using_suffix(suffix_and_name[1])
|
||||
end, { desc = 'Navigate to relevant ' .. suffix_and_name[2] })
|
||||
end
|
||||
end
|
||||
|
||||
local function create_bookmark(key, bookmark)
|
||||
map('<Leader>b' .. key, function()
|
||||
vim.cmd('edit ' .. bookmark)
|
||||
end, { desc = 'Navigate to ' .. bookmark })
|
||||
end
|
||||
|
||||
local function create_bookmark_maps(maps)
|
||||
for key, bookmark in pairs(maps) do
|
||||
create_bookmark(key, bookmark)
|
||||
end
|
||||
end
|
||||
|
||||
local function laravel_bookmarks_with_dir(dir)
|
||||
create_bookmark_maps {
|
||||
['e'] = dir .. '.env',
|
||||
['l'] = dir .. 'storage/logs/laravel.log',
|
||||
['w'] = dir .. 'routes/web.php',
|
||||
['a'] = dir .. 'routes/api.php',
|
||||
['m'] = dir .. 'database/migrations',
|
||||
|
||||
['dc'] = dir .. 'app/Core/',
|
||||
['dd'] = dir .. 'app/Data/',
|
||||
['dE'] = dir .. 'app/Enums/',
|
||||
['de'] = dir .. 'app/Events/',
|
||||
['dh'] = dir .. 'app/Http/',
|
||||
['dj'] = dir .. 'app/Jobs/',
|
||||
['dl'] = dir .. 'app/Listeners/',
|
||||
['dM'] = dir .. 'app/Mail/',
|
||||
['dm'] = dir .. 'app/Models/',
|
||||
['dn'] = dir .. 'app/Notifications/',
|
||||
['do'] = dir .. 'app/Observers/',
|
||||
['dp'] = dir .. 'app/Providers/',
|
||||
|
||||
['pa'] = dir .. 'app/Providers/AppServiceProvider.php',
|
||||
['pe'] = dir .. 'app/Providers/EventServiceProvider.php',
|
||||
|
||||
['cA'] = dir .. 'config/app.php',
|
||||
['ca'] = dir .. 'config/auth.php',
|
||||
['cb'] = dir .. 'config/broadcasting.php',
|
||||
['cd'] = dir .. 'config/database.php',
|
||||
['cf'] = dir .. 'config/filesystems.php',
|
||||
['ch'] = dir .. 'config/filesystems.php',
|
||||
['cl'] = dir .. 'config/logging.php',
|
||||
['cm'] = dir .. 'config/mail.php',
|
||||
['cq'] = dir .. 'config/queue.php',
|
||||
['cS'] = dir .. 'config/services.php',
|
||||
['cs'] = dir .. 'config/session.php',
|
||||
}
|
||||
end
|
||||
|
||||
local function laravel_keymaps(dir)
|
||||
map('sl ', command_with_dir(dir, 'vendor/bin/sail '), { desc = 'Run sail command' }, 'c')
|
||||
map('art ', command_with_dir(dir, 'php artisan '), { desc = 'Run artisan command' }, 'c')
|
||||
map('sart ', command_with_dir(dir, 'vendor/bin/sail artisan '), { desc = 'Run artisan command with sail' }, 'c')
|
||||
map('cmp ', command_with_dir(dir, 'composer '), { desc = 'Run composer script' }, 'c')
|
||||
map('<Leader>pm', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan migrate<CR>'))
|
||||
map('<Leader>pr', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan migrate:rollback<CR>'))
|
||||
map('<Leader>pM', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan make:'))
|
||||
create_navigation_maps {
|
||||
['m'] = { '.php', 'model' },
|
||||
['c'] = { 'Controller.php', 'controller' },
|
||||
['p'] = { 'Policy.php', 'policy' },
|
||||
['R'] = { 'Resource.php', 'resource' },
|
||||
['r'] = { 'Request.php', 'request' },
|
||||
['t'] = { 'Test.php', 'test file' },
|
||||
}
|
||||
if dir == nil then
|
||||
dir = ''
|
||||
end
|
||||
laravel_bookmarks_with_dir(dir)
|
||||
end
|
||||
|
||||
local function laravel_makes(dir)
|
||||
for key, name in pairs {
|
||||
c = 'controller',
|
||||
d = 'data',
|
||||
e = 'event',
|
||||
f = 'factory',
|
||||
j = 'job',
|
||||
l = 'listener',
|
||||
ma = 'mail',
|
||||
mi = 'migration',
|
||||
mo = 'model',
|
||||
mw = 'middleware',
|
||||
n = 'notification',
|
||||
o = 'observer',
|
||||
pi = 'model --pivot',
|
||||
po = 'policy',
|
||||
pr = 'provider',
|
||||
t = 'test --pest',
|
||||
v = 'view',
|
||||
x = 'exception',
|
||||
} do
|
||||
map('<Leader>m' .. key, function()
|
||||
make_laravel_file(dir, name)
|
||||
end, { desc = 'Make and navigate to relevant ' .. name })
|
||||
end
|
||||
end
|
||||
|
||||
-- Define per-project configuration here.
|
||||
-- Keys are folder names (last segment of your cwd).
|
||||
local PROJECTS = {
|
||||
-- Example: a repo folder
|
||||
['runcats'] = function(dir)
|
||||
-- local dump_buf = vim.api.nvim_create_buf(false, true)
|
||||
-- vim.api.nvim_set_option_value('bufhidden', 'hide', { buf = dump_buf })
|
||||
-- vim.api.nvim_set_option_value('modifiable', false, { buf = dump_buf })
|
||||
--
|
||||
-- vim.fn.jobstart('cd server && sail artisan dump-server', {
|
||||
-- term = true,
|
||||
-- })
|
||||
|
||||
-- map('<leader>dd', function()
|
||||
-- local height = math.ceil(vim.o.lines * 0.8)
|
||||
-- local width = math.ceil(vim.o.columns * 0.8)
|
||||
-- vim.api.nvim_open_win(dump_buf, true, {
|
||||
-- style = 'minimal',
|
||||
-- relative = 'editor',
|
||||
-- width = width,
|
||||
-- height = height,
|
||||
-- row = math.ceil((vim.o.lines - height) / 2),
|
||||
-- col = math.ceil((vim.o.columns - width) / 2),
|
||||
-- border = 'single',
|
||||
-- })
|
||||
--
|
||||
-- vim.cmd.startinsert()
|
||||
-- end, { desc = 'Open the dump-server window' })
|
||||
|
||||
map(
|
||||
'<leader>pl',
|
||||
':cexpr system("cd server && vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") <CR>',
|
||||
{ desc = 'Run lint' }
|
||||
)
|
||||
map('<leader>pd', function()
|
||||
helpers.open_term { cmd = 'lazysql mysql://root@localhost:3306/runcats' }
|
||||
end, { desc = 'Open database manager' })
|
||||
map('s ', '!cd server && ', { desc = 'Run command in server directory' }, 'c')
|
||||
map('c ', '!cd client && ', { desc = 'Run command in client directory' }, 'c')
|
||||
laravel_keymaps 'server/'
|
||||
laravel_makes 'server/'
|
||||
map('yrn ', '!cd client && yarn ', { desc = 'Run yarn script' }, 'c')
|
||||
map('<Leader>pt', ':!cd server && php artisan typescript:transform --format<CR>', { desc = 'Compile typescript' })
|
||||
require('conform').formatters.pint = {
|
||||
append_args = {
|
||||
'--config=' .. dir .. '/server/pint.json',
|
||||
},
|
||||
}
|
||||
|
||||
create_bookmark('E', dir .. '/client/src/types/api/endpointMap.d.ts')
|
||||
create_bookmark('q', dir .. '/client/quasar.config.ts')
|
||||
create_bookmark('db', dir .. '/client/src/boot')
|
||||
create_bookmark('ds', dir .. '/client/src/stores')
|
||||
create_bookmark('dS', dir .. '/client/src/services')
|
||||
end,
|
||||
|
||||
['hylark'] = function(dir)
|
||||
map('<leader>pl', ':cexpr system("vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") <CR>', { desc = 'Run lint' })
|
||||
map('<leader>pd', function()
|
||||
helpers.open_term { cmd = 'lazysql pgsql://homestead:password@localhost:5432/homestead' }
|
||||
end, { desc = 'Open database manager' })
|
||||
laravel_keymaps()
|
||||
laravel_makes()
|
||||
map('yrn ', '!cd frontend && yarn ', { desc = 'Run yarn script' }, 'c')
|
||||
map('<Leader>pm', ':vendor/bin/sail composer migrate<CR>')
|
||||
end,
|
||||
|
||||
default = function() end,
|
||||
}
|
||||
|
||||
local last_applied = nil
|
||||
|
||||
function M.apply(cwd)
|
||||
local dir = cwd or vim.fn.getcwd()
|
||||
local name = project_name_from_cwd(dir)
|
||||
local dir = relevant_directory_from_cwd(cwd or vim.fn.getcwd())
|
||||
local name = project_name_from_dir(dir)
|
||||
if last_applied == name then
|
||||
return
|
||||
end
|
||||
last_applied = name
|
||||
|
||||
local setup = PROJECTS[name]
|
||||
if type(setup) == 'function' then
|
||||
-- Check if project lua file exists in nvim config path
|
||||
if vim.fn.filereadable(vim.fn.stdpath 'config' .. '/lua/projects/' .. name .. '.lua') == 1 then
|
||||
local setup = require('projects.' .. name)
|
||||
helpers.edit_cf('w', '/lua/projects/' .. name .. '.lua')
|
||||
setup(dir)
|
||||
vim.notify(('Project config loaded: %s'):format(name), vim.log.levels.INFO, { title = 'projects.lua' })
|
||||
else
|
||||
PROJECTS.default(dir)
|
||||
|
||||
-- Source the project file to apply any new settings
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
|
||||
pattern = { vim.fn.stdpath 'config' .. '/lua/projects/' .. name .. '.lua' },
|
||||
callback = function()
|
||||
package.loaded['projects.' .. name] = nil
|
||||
local setup = require('projects.' .. name)
|
||||
setup(dir)
|
||||
vim.notify(('Project config reloaded: %s'):format(name), vim.log.levels.INFO, { title = 'projects.lua' })
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -292,5 +77,3 @@ vim.api.nvim_create_user_command('ProjectReload', function()
|
||||
M.apply()
|
||||
end, {})
|
||||
|
||||
-- Keep your helper line
|
||||
require('helpers').edit_cf('w', '/lua/projects.lua')
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
local helpers = require 'helpers'
|
||||
local map = helpers.map
|
||||
|
||||
local M = {}
|
||||
|
||||
local function command_with_dir(dir, cmd)
|
||||
if dir then
|
||||
return '!cd ' .. dir .. ' && ' .. cmd
|
||||
end
|
||||
return '!' .. cmd
|
||||
end
|
||||
|
||||
local function make_laravel_file(dir, cmd)
|
||||
local cwd = vim.fn.getcwd()
|
||||
if dir then
|
||||
vim.fn.chdir(dir)
|
||||
end
|
||||
vim.ui.input({ prompt = 'Make: ' .. cmd }, function(input)
|
||||
if input == nil then
|
||||
vim.fn.chdir(cwd)
|
||||
return
|
||||
end
|
||||
|
||||
local output = vim.system({ 'vendor/bin/sail', 'artisan', 'make:' .. cmd, input }):wait().stdout
|
||||
local new_file = output:match '%[([%w%./]+)%]'
|
||||
if new_file ~= nil then
|
||||
vim.cmd('edit ' .. new_file)
|
||||
end
|
||||
vim.fn.chdir(cwd)
|
||||
end)
|
||||
end
|
||||
|
||||
local function get_scope_from_file(filename)
|
||||
local ext = filename:match '%.(%w+)$'
|
||||
local base_name = filename:match '^(%w+)%.?%w*$'
|
||||
local extensionSuffixes = {
|
||||
php = { 'Controller', 'Repository', 'StoreRequest', 'UpdateRequest', 'Request', 'Resource', 'Test', 'Observer', 'Policy', 'Seeder', 'Factory' },
|
||||
['[jt]s'] = { 'Service' },
|
||||
}
|
||||
for suffix_type, _ in pairs(extensionSuffixes) do
|
||||
if ext:match(suffix_type) then
|
||||
local suffixes = extensionSuffixes[suffix_type]
|
||||
for _, suffix in ipairs(suffixes) do
|
||||
local scope = base_name:match('^(%w+)' .. suffix .. '$')
|
||||
if scope then
|
||||
return scope
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return base_name
|
||||
end
|
||||
|
||||
local function navigate_using_suffix(suffix)
|
||||
local scope = get_scope_from_file(vim.fn.expand '%:t')
|
||||
local file_name = scope .. suffix
|
||||
local file_path = vim.system({ 'git', 'ls-files', file_name, '**/' .. file_name }):wait().stdout
|
||||
if file_path ~= '' then
|
||||
vim.cmd('edit ' .. file_path)
|
||||
else
|
||||
vim.notify('File ' .. file_name .. ' not found in git ls-files', vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
|
||||
local function create_navigation_maps(maps)
|
||||
for key, suffix_and_name in pairs(maps) do
|
||||
map('<Leader>n' .. key, function()
|
||||
navigate_using_suffix(suffix_and_name[1])
|
||||
end, { desc = 'Navigate to relevant ' .. suffix_and_name[2] })
|
||||
end
|
||||
end
|
||||
|
||||
local function create_bookmark_maps(maps)
|
||||
for key, bookmark in pairs(maps) do
|
||||
helpers.create_bookmark(key, bookmark)
|
||||
end
|
||||
end
|
||||
|
||||
local function laravel_bookmarks_with_dir(dir)
|
||||
create_bookmark_maps {
|
||||
['e'] = dir .. '.env',
|
||||
['l'] = dir .. 'storage/logs/laravel.log',
|
||||
['w'] = dir .. 'routes/web.php',
|
||||
['a'] = dir .. 'routes/api.php',
|
||||
['m'] = dir .. 'database/migrations',
|
||||
|
||||
['dc'] = dir .. 'app/Core/',
|
||||
['dd'] = dir .. 'app/Data/',
|
||||
['dE'] = dir .. 'app/Enums/',
|
||||
['de'] = dir .. 'app/Events/',
|
||||
['dh'] = dir .. 'app/Http/',
|
||||
['dj'] = dir .. 'app/Jobs/',
|
||||
['dl'] = dir .. 'app/Listeners/',
|
||||
['dM'] = dir .. 'app/Mail/',
|
||||
['dm'] = dir .. 'app/Models/',
|
||||
['dn'] = dir .. 'app/Notifications/',
|
||||
['do'] = dir .. 'app/Observers/',
|
||||
['dp'] = dir .. 'app/Providers/',
|
||||
|
||||
['pa'] = dir .. 'app/Providers/AppServiceProvider.php',
|
||||
['pe'] = dir .. 'app/Providers/EventServiceProvider.php',
|
||||
|
||||
['cA'] = dir .. 'config/app.php',
|
||||
['ca'] = dir .. 'config/auth.php',
|
||||
['cb'] = dir .. 'config/broadcasting.php',
|
||||
['cd'] = dir .. 'config/database.php',
|
||||
['cf'] = dir .. 'config/filesystems.php',
|
||||
['ch'] = dir .. 'config/filesystems.php',
|
||||
['cl'] = dir .. 'config/logging.php',
|
||||
['cm'] = dir .. 'config/mail.php',
|
||||
['cq'] = dir .. 'config/queue.php',
|
||||
['cS'] = dir .. 'config/services.php',
|
||||
['cs'] = dir .. 'config/session.php',
|
||||
}
|
||||
end
|
||||
|
||||
local function laravel_utils(dir)
|
||||
map('<Leader>D', function()
|
||||
helpers.open_term {
|
||||
cmd = dir .. 'vendor/bin/var-dump-server',
|
||||
buf_opts = {
|
||||
bufhidden = 'hide',
|
||||
modifiable = false,
|
||||
},
|
||||
}
|
||||
end, { desc = 'Open dump-server window' })
|
||||
end
|
||||
|
||||
local function laravel_keymaps(dir)
|
||||
map('sl ', command_with_dir(dir, 'vendor/bin/sail '), { desc = 'Run sail command' }, 'c')
|
||||
map('art ', command_with_dir(dir, 'php artisan '), { desc = 'Run artisan command' }, 'c')
|
||||
map('sart ', command_with_dir(dir, 'vendor/bin/sail artisan '), { desc = 'Run artisan command with sail' }, 'c')
|
||||
map('cmp ', command_with_dir(dir, 'composer '), { desc = 'Run composer script' }, 'c')
|
||||
map('<Leader>pm', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan migrate<CR>'))
|
||||
map('<Leader>pr', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan migrate:rollback<CR>'))
|
||||
map('<Leader>pM', ':' .. command_with_dir(dir, 'vendor/bin/sail artisan make:'))
|
||||
create_navigation_maps {
|
||||
['m'] = { '.php', 'model' },
|
||||
['c'] = { 'Controller.php', 'controller' },
|
||||
['p'] = { 'Policy.php', 'policy' },
|
||||
['R'] = { 'Resource.php', 'resource' },
|
||||
['r'] = { 'Request.php', 'request' },
|
||||
['t'] = { 'Test.php', 'test file' },
|
||||
}
|
||||
if dir == nil then
|
||||
dir = ''
|
||||
end
|
||||
laravel_bookmarks_with_dir(dir)
|
||||
end
|
||||
|
||||
local function laravel_makes(dir)
|
||||
for key, name in pairs {
|
||||
c = 'controller',
|
||||
d = 'data',
|
||||
e = 'event',
|
||||
f = 'factory',
|
||||
j = 'job',
|
||||
l = 'listener',
|
||||
ma = 'mail',
|
||||
mi = 'migration',
|
||||
mo = 'model',
|
||||
mw = 'middleware',
|
||||
n = 'notification',
|
||||
o = 'observer',
|
||||
pi = 'model --pivot',
|
||||
po = 'policy',
|
||||
pr = 'provider',
|
||||
t = 'test --pest',
|
||||
v = 'view',
|
||||
x = 'exception',
|
||||
} do
|
||||
map('<Leader>m' .. key, function()
|
||||
make_laravel_file(dir, name)
|
||||
end, { desc = 'Make and navigate to relevant ' .. name })
|
||||
end
|
||||
end
|
||||
|
||||
M.laravel_keymaps = laravel_keymaps
|
||||
M.laravel_makes = laravel_makes
|
||||
M.laravel_utils = laravel_utils
|
||||
M.laravel_bookmarks = laravel_bookmarks_with_dir
|
||||
|
||||
return M
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
local laravel_utils = require 'projects.framework_utils.laravel'
|
||||
local helpers = require 'helpers'
|
||||
local map = helpers.map
|
||||
local create_bookmark = helpers.create_bookmark
|
||||
|
||||
local function switch_to(dir)
|
||||
if vim.fn.getcwd() == dir then
|
||||
return
|
||||
end
|
||||
vim.fn.chdir(dir)
|
||||
vim.notify('Changed working directory to ' .. dir, vim.log.levels.INFO, { title = 'hylark.lua' })
|
||||
end
|
||||
|
||||
return function(dir)
|
||||
local function switch_to_server()
|
||||
switch_to(dir .. '/server')
|
||||
end
|
||||
|
||||
local function switch_to_frontend()
|
||||
switch_to(dir .. '/frontend2')
|
||||
end
|
||||
|
||||
local function switch_to_root()
|
||||
switch_to(dir)
|
||||
end
|
||||
|
||||
local function compile_typescript()
|
||||
vim.fn.jobstart('php artisan typescript:transform', { cwd = dir .. '/server', stdout_buffered = true })
|
||||
vim.notify('Compiling typescript types', vim.log.levels.INFO, { title = 'hylark.lua' })
|
||||
end
|
||||
|
||||
map('<leader>pl', ':cexpr system("vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") <CR>', { desc = 'Run lint' })
|
||||
map('<leader>T', function()
|
||||
helpers.open_term { cmd = 'lazysql pgsql://homestead:password@localhost:5432/homestead' }
|
||||
end, { desc = 'Open database manager' })
|
||||
map('yrn ', '!cd frontend && yarn ', { desc = 'Run yarn script' }, 'c')
|
||||
map('<Leader>pm', ':vendor/bin/sail composer migrate<CR>')
|
||||
map('<Leader>pcs', switch_to_server, { desc = 'Change working directory to server' })
|
||||
map('<Leader>pcf', switch_to_frontend, { desc = 'Change working directory to frontend2' })
|
||||
map('<Leader>pcr', switch_to_root, { desc = 'Change working directory to root' })
|
||||
map('<Leader>pt', compile_typescript, { desc = 'Compile typescript' })
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
|
||||
pattern = { '*.php', '.env', '.graphql' },
|
||||
callback = function()
|
||||
if not vim.fn.expand('%:p'):match(dir .. '/server/') then
|
||||
vim.fn.jobstart('vendor/bin/sail artisan octane:reload', { stdout_buffered = true })
|
||||
vim.fn.jobstart('vendor/bin/sail artisan horizon:terminate', { stdout_buffered = true })
|
||||
end
|
||||
if vim.fn.search '#\\[TypeScript\\]' ~= 0 then
|
||||
compile_typescript()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
|
||||
pattern = { '*.js', '*.ts', '*.vue' },
|
||||
callback = function(args)
|
||||
vim.env.ESLINT_D_PPID = vim.fn.getpid()
|
||||
local cwd = vim.fn.getcwd()
|
||||
if vim.fn.expand('%:p'):match(dir .. '/frontend2/') then
|
||||
cwd = dir .. '/frontend2/'
|
||||
end
|
||||
require('helpers').format_buffer('eslint_d', args.buf, cwd)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
|
||||
pattern = { '*.php' },
|
||||
callback = function(args)
|
||||
local cwd = vim.fn.getcwd()
|
||||
if vim.fn.expand('%:p'):match(dir .. '/server/') then
|
||||
cwd = dir .. '/server/'
|
||||
end
|
||||
require('helpers').format_buffer('pint', args.buf, cwd)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePost' }, {
|
||||
pattern = { '.graphql' },
|
||||
callback = function()
|
||||
vim.fn.jobstart('vendor/bin/sail artisan lighthouse:clear-cache', { stdout_buffered = true })
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufEnter' }, {
|
||||
pattern = { dir .. '**' },
|
||||
callback = function()
|
||||
if vim.fn.expand('%:p'):match(dir .. '/server/') then
|
||||
switch_to_server()
|
||||
elseif vim.fn.expand('%:p'):match(dir .. '/frontend2/') then
|
||||
switch_to_frontend()
|
||||
else
|
||||
switch_to_root()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
laravel_utils.laravel_keymaps 'server/'
|
||||
laravel_utils.laravel_makes 'server/'
|
||||
laravel_utils.laravel_utils 'server/'
|
||||
laravel_utils.laravel_bookmarks(dir .. '/server/')
|
||||
|
||||
create_bookmark('s', dir .. '/frontend2/src/types/api/server-types.d.ts')
|
||||
end
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
local laravel_utils = require 'projects.framework_utils.laravel'
|
||||
local helpers = require 'helpers'
|
||||
local map = helpers.map
|
||||
local create_bookmark = helpers.create_bookmark
|
||||
|
||||
return function(dir)
|
||||
map(
|
||||
'<leader>pl',
|
||||
':cexpr system("cd server && vendor/bin/phpstan analyse --no-progress --error-format=raw --memory-limit=2G -vv") <CR>',
|
||||
{ desc = 'Run lint' }
|
||||
)
|
||||
map('<leader>T', function()
|
||||
helpers.open_term { cmd = 'lazysql mysql://root@localhost:3306/runcats' }
|
||||
end, { desc = 'Open database manager' })
|
||||
map('s ', '!cd server && ', { desc = 'Run command in server directory' }, 'c')
|
||||
map('c ', '!cd client && ', { desc = 'Run command in client directory' }, 'c')
|
||||
map('yrn ', '!cd client && yarn ', { desc = 'Run yarn script' }, 'c')
|
||||
map('<Leader>pt', ':!cd server && php artisan typescript:transform --format<CR>', { desc = 'Compile typescript' })
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
|
||||
pattern = { '*.php' },
|
||||
callback = function(args)
|
||||
helpers.format_buffer('pint', args.buf)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
|
||||
pattern = { '*.js', '*.ts', '*.vue' },
|
||||
callback = function(args)
|
||||
helpers.format_buffer('eslint_d', args.buf)
|
||||
end,
|
||||
})
|
||||
|
||||
laravel_utils.laravel_keymaps 'server/'
|
||||
laravel_utils.laravel_makes 'server/'
|
||||
laravel_utils.laravel_utils 'server/'
|
||||
laravel_utils.laravel_bookmarks 'server/'
|
||||
|
||||
create_bookmark('E', dir .. '/client/src/types/api/endpointMap.d.ts')
|
||||
create_bookmark('q', dir .. '/client/quasar.config.ts')
|
||||
create_bookmark('db', dir .. '/client/src/boot')
|
||||
create_bookmark('ds', dir .. '/client/src/stores')
|
||||
create_bookmark('dS', dir .. '/client/src/services')
|
||||
end
|
||||
|
||||
@@ -45,7 +45,7 @@ return {
|
||||
|
||||
s(etr('const ', 'const declaration'), {
|
||||
c(1, {
|
||||
sn(nil, fmta('const #~ = #~;', { i(1, 'variableName'), i(2, 'value') })),
|
||||
sn(nil, fmta('const #~ = #~;', { i(1), i(2) })),
|
||||
sn(
|
||||
nil,
|
||||
fmta(
|
||||
@@ -60,6 +60,38 @@ return {
|
||||
}),
|
||||
}),
|
||||
|
||||
s(etr('ref ', 'ref'), {
|
||||
c(1, {
|
||||
sn(nil, fmta('const #~ = ref(#~);', { i(1), i(2) })),
|
||||
sn(
|
||||
nil,
|
||||
fmta(
|
||||
[[
|
||||
const #~ = ref<#~>(#~);
|
||||
]],
|
||||
{ i(1), i(2), i(3) }
|
||||
)
|
||||
),
|
||||
}),
|
||||
}),
|
||||
|
||||
s(etr('com ', 'computed'), {
|
||||
c(1, {
|
||||
sn(nil, fmta('const #~ = computed(() => #~);', { i(1), i(2) })),
|
||||
sn(
|
||||
nil,
|
||||
fmta(
|
||||
[[
|
||||
const #~ = computed(() => {
|
||||
#~
|
||||
})
|
||||
]],
|
||||
{ i(1), i(2) }
|
||||
)
|
||||
),
|
||||
}),
|
||||
}),
|
||||
|
||||
s(
|
||||
etr('fn ', 'function block'),
|
||||
fmta(
|
||||
@@ -126,3 +158,4 @@ return {
|
||||
}),
|
||||
}),
|
||||
}
|
||||
|
||||
|
||||
@@ -378,7 +378,11 @@ return {
|
||||
{ i(1), i(0) }
|
||||
)
|
||||
),
|
||||
s(etr('nn ', 'Assert not null'), fmta('Assert::notNull(#~)', { i(0) })),
|
||||
s(etr('nn ', 'Assert not null'), fmta('Assert::notNull(#~);', { i(0) })),
|
||||
s(etr('ast ', 'Assert string'), fmta('Assert::string(#~);', { i(0) })),
|
||||
s(etr('ain ', 'Assert integer'), fmta('Assert::integer(#~);', { i(0) })),
|
||||
s(etr('aio ', 'Assert instance of'), fmta('Assert::isInstanceOf(#~);', { i(0) })),
|
||||
s(etr('at ', 'Assert true'), fmta('Assert::true(#~);', { i(0) })),
|
||||
-------------
|
||||
-- LARAVEL --
|
||||
-------------
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
local M = {}
|
||||
|
||||
function M.open(name)
|
||||
local titles = {
|
||||
zsh = 'ZSH Cheat Sheet',
|
||||
bash = 'Bash Cheat Sheet',
|
||||
regex = 'RegExp Cheat Sheet',
|
||||
}
|
||||
|
||||
local path = vim.fn.stdpath 'config' .. '/cheatsheets/' .. name .. '.md'
|
||||
print('Opening cheat sheet: ' .. path)
|
||||
|
||||
require('../helpers').open_file_modal(path, titles[name] or name)
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -0,0 +1,3 @@
|
||||
WRN 2026-07-03T16:02:54.215 c/nvim.57084.0 server_start:197: Failed to start server: operation not permitted: /var/folders/j3/6yv08_8s1s740ggr40_kgvrr0000gn/T/nvim.chris/esnddj/nvim.57725.0
|
||||
WRN 2026-07-03T16:03:18.385 c/nvim.57084.0 server_start:197: Failed to start server: operation not permitted: /var/folders/j3/6yv08_8s1s740ggr40_kgvrr0000gn/T/nvim.chris/O4WnB1/nvim.57764.0
|
||||
WRN 2026-07-14T11:43:01.105 c/nvim.17401.0 server_start:197: Failed to start server: operation not permitted: /var/folders/j3/6yv08_8s1s740ggr40_kgvrr0000gn/T/nvim.chris/KkEtYu/nvim.19436.0
|
||||
Reference in New Issue
Block a user