Skip to content

Instantly share code, notes, and snippets.

@meepak
Last active August 28, 2024 02:11
Show Gist options
  • Save meepak/a524a683d8c9f30f373f9f7027ee2e7a to your computer and use it in GitHub Desktop.
Save meepak/a524a683d8c9f30f373f9f7027ee2e7a to your computer and use it in GitHub Desktop.
Setup CLI editor (neovim + plugins) for laravel development. Supports file browsing, intellisense, CTRL click to jump to references, PHP debugging, blade syntax, vuejs, prettify , etc, etc.
#!/bin/bash
# Function to print error messages
error_exit() {
echo "Error: $1"
exit 1
}
# Function to generate ctags with a progress bar, only for new/modified files
generate_ctags() {
echo "Checking for existing ctags..."
if [ ! -f "./tags" ]; then
echo "No existing ctags found. Generating ctags for the first time..."
else
echo "Updating existing ctags..."
fi
find . -type f -name "*.[ch]" | tee /tmp/ctags_files.txt | wc -l | {
total=$(< /dev/stdin)
count=0
while read -r file; do
if [ ! -f "./tags" ] || [ "$file" -nt "./tags" ]; then
count=$((count + 1))
ctags -a "$file"
printf "Progress: %d/%d files processed\r" "$count" "$total"
fi
done < /tmp/ctags_files.txt
if [ "$count" -eq 0 ]; then
echo "All files are up to date. No need to regenerate ctags."
else
echo "Ctags generation completed!"
fi
}
rm /tmp/ctags_files.txt
}
# Prompt to generate ctags and add to .gitignore if needed
prompt_for_ctags() {
echo "Ctags are used to generate an index file (called 'tags') that allows for easy navigation within the code, like jumping to function definitions."
echo "Do you want to generate ctags for this project?"
if [ "$(confirm "Generate ctags for project, This will take few minutes depending upon size of project to index?")" == "1" ]; then
generate_ctags
# Prompt to add to .gitignore
if ! grep -q "^tags$" .gitignore; then
echo "Do you want to add the 'tags' file to .gitignore?"
if [ "$(confirm "Add 'tags' to .gitignore?")" == "1" ]; then
echo "tags" >> .gitignore
echo "'tags' file added to .gitignore."
fi
fi
else
echo "Skipping ctags generation."
fi
}
# Purge Neovim, Vim, and related configurations
echo "Purging Neovim, Vim, and related configurations..."
sudo apt remove --purge -y neovim vim vim-runtime vim-common || error_exit "Failed to remove Neovim and Vim."
sudo apt autoremove -y || error_exit "Failed to run autoremove."
rm -rf ~/.vim ~/.vimrc ~/.config/nvim ~/.local/share/nvim || error_exit "Failed to remove Vim/Neovim config directories."
# Update package list
echo "Updating package list..."
sudo apt update || error_exit "Failed to update package list."
# Install required packages
echo "Installing required packages..."
sudo apt install -y git curl nodejs npm exuberant-ctags php-xdebug || error_exit "Failed to install required packages."
# Install libfuse2 alongside libfuse3
echo "Installing libfuse2 alongside libfuse3..."
sudo apt install -y libfuse2 || error_exit "Failed to install libfuse2."
# Download and Install Neovim from GitHub
echo "Downloading Neovim..."
curl -LO https://github.com/neovim/neovim/releases/download/stable/nvim.appimage || error_exit "Failed to download Neovim."
echo "Making Neovim executable..."
chmod u+x nvim.appimage || error_exit "Failed to make Neovim executable."
echo "Moving Neovim to /usr/bin/..."
sudo mv nvim.appimage /usr/bin/nvim || error_exit "Failed to move Neovim to /usr/bin/."
# Create Vim/Neovim configuration directories
echo "Creating configuration directories..."
mkdir -p ~/.vim/autoload ~/.vim/plugged ~/.config/nvim || error_exit "Failed to create configuration directories."
# Install vim-plug
echo "Installing vim-plug..."
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim || error_exit "Failed to install vim-plug."
# Create init.vim with plugins and quick help
echo "Creating init.vim configuration file..."
cat <<'EOL' > ~/.config/nvim/init.vim
set nocompatible
set number
syntax on
call plug#begin('~/.local/share/nvim/site/plugged')
Plug 'tpope/vim-sensible'
Plug 'scottmckendry/cyberdream.nvim'
Plug 'preservim/nerdtree'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'editorconfig/editorconfig-vim'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'sheerun/vim-polyglot'
Plug 'mattn/emmet-vim' " Emmet for HTML and CSS
Plug 'tpope/vim-surround' " Surroundings (quotes, brackets, etc.)
Plug 'tpope/vim-commentary' " Commenting plugin
Plug 'vim-airline/vim-airline' " Status bar
Plug 'pangloss/vim-javascript' " JavaScript syntax
Plug 'maxmellon/vim-jsx-pretty' " JSX syntax
Plug 'leafgarland/typescript-vim' " TypeScript syntax
Plug 'posva/vim-vue' " Vue.js syntax
Plug 'jwalton512/vim-blade' " Laravel Blade syntax
Plug 'puremourning/vimspector' " Debugger plugin
call plug#end()
colorscheme cyberdream
" NERDTree toggle
nnoremap <C-n> :NERDTreeToggle<cr>
" Fuzzy Finder
nnoremap <C-p> :Files<cr>
" Prompt for generating ctags and adding to .gitignore
autocmd VimEnter * call prompt_for_ctags()
" CoC Global Extensions
let g:coc_global_extensions = ['coc-tsserver', 'coc-json', 'coc-html', 'coc-css', 'coc-phpls', 'coc-python', 'coc-vetur', 'coc-prettier', 'coc-eslint', 'coc-diagnostic']
" Disable CoC startup warning
let g:coc_disable_startup_warning = 1
" Display quick help at startup
autocmd VimEnter * echo "Quick Tips: Ctrl+n (NERDTree), Ctrl+p (FZF), :VimspectorLaunch (Start Debugger), :VimspectorBreakpoints (Set Breakpoints), Intellisense with CoC"
EOL
# Install Plugins
echo "Installing plugins..."
nvim +PlugInstall +qall || error_exit "Failed to install Vim plugins."
# Configure PHP Xdebug
echo "Configuring PHP Xdebug..."
cat <<EOL > ~/.config/nvim/.vimspector.json
{
"configurations": {
"PHP XDebug": {
"adapter": "vscode-php-debug",
"filetypes": [ "php" ],
"configuration": {
"request": "launch",
"port": 9003,
"hostname": "127.0.0.1"
}
}
}
}
EOL
echo "Setup complete. Neovim is ready for Laravel development with debugging and quick help!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment