-
Notifications
You must be signed in to change notification settings - Fork 0
/
vscode_functions.zsh
53 lines (45 loc) · 1.49 KB
/
vscode_functions.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
code_change_project() {
base_url="$HOME/$CCP_BASE"
project_to="$(/bin/ls $base_url | fzf --preview "ls -alh $base_url/{}")"
if [[ "${project_to}" != "" ]]; then
code -r "${base_url}/${project_to}"
fi
}
code_open_file() {
fzf_result="$(fd -t f | fzf -e --print-query --preview 'bat --style=numbers --color=always {}')"
if [[ "${fzf_result}" == "" ]]; then
echo "User exited, stopping script"
else
n_lines=$(echo $fzf_result | wc -l)
# only the query exists if this is true
if [[ ${n_lines} == 1 ]]; then
echo "no file selected, operating on query"
file_to_open="$(echo ${fzf_result} | head -n 1)"
else
file_to_open="$(echo ${fzf_result} | tail -n 1)"
fi
echo "Opening '$file_to_open'"
# if the user does not exit
code -a "$file_to_open"
fi
}
code_open_file_with_term() {
# taken from fzf examples
INITIAL_QUERY=""
RG_PREFIX="rg --line-number --no-heading --color=always --smart-case "
result=$(FZF_DEFAULT_COMMAND="$RG_PREFIX '$INITIAL_QUERY'" \
fzf --bind "change:reload:$RG_PREFIX {q} || true" \
--ansi --disabled --query "$INITIAL_QUERY" \
--height=50% --layout=reverse
)
file_name="$(echo $result | awk -F ':' '{print $1}')"
line_number="$(echo $result | awk -F ':' '{print $2}')"
if [[ $result != "" ]]; then
code -g "$file_name:$line_number"
else
echo "No file selected, exiting"
fi
}
alias ccp='code_change_project'
alias cof='code_open_file'
alias coft='code_open_file_with_term'