Neovim for 42 Students
So you are a 42 student and you are as geeky as it gets. And when it comes to coding efficiency, you’re probably no stranger to Vim — the powerhouse text editor that lets you work at the speed of thought, no mouse required. However, setting up Neovim in your workspace might not be as straightforward as a simple brew install
. With restricted permissions and absence of Neovim on the school's Linux machines, getting AstroNvim up and running can seem like a daunting task. But fear not, I've got you covered with this hassle-free walkthrough on setting up Neovim and AstroNvim for optimal coding bliss.
Clone Neovim Repository
First things first, let’s get Neovim on your system. Clone the Neovim repository from GitHub into your home directory, since that’s where you have permission.
git clone https://github.com/neovim/neovim.git ~/neovim
Build Neovim Locally
Now that you have the Neovim source code, let’s build it. Navigate into the Neovim directory:
cd ~/neovim
Configure the Build for a Local Installation
Before we build Neovim, we need to configure it for a local installation. This command tells Neovim’s build system to install to ~/neovim
:
make CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_INSTALL_PREFIX=$HOME/neovim
To ensure a successful configuration of the Neovim build environment, it is imperative to execute the above command, while ensuring that all necessary dependencies are in place.
This command serves as a pivotal step in configuring the Neovim build system. It is essential to have key dependencies such as ‘make’ and ‘cmake’ installed beforehand. If these dependencies are not already present, they can typically be installed using the following command:
sudo yum groupinstall "Development Tools"
export CC=/usr/bin/gcc
Add the above export at the bottom of your .zshrc or .bashrc file.
It is worth noting that in a typical 42 workspace, many of these dependencies are readily available, simplifying the process significantly.
Upon successful execution of this command, the Neovim build system will be configured as intended, paving the way for subsequent steps in the installation process.
Build Neovim
It’s time to build Neovim:
make -j4
The -j4
flag specifies the number of parallel jobs to run during compilation. You can adjust this number according to the number of cores your CPU has (e.g., -j8
for 8 cores).
Install Neovim Locally
Once the build process is complete, install Neovim locally:
make install
Add Neovim to PATH
To make it easier to run Neovim from anywhere in the terminal, let’s add its bin directory to your PATH. Add the following line to your ~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
file:
export PATH=$HOME/neovim/bin:$PATH
Then, apply the changes:
source ~/.bashrc # or ~/.bash_profile or ~/.zshrc depending on your shell
Verify Installation
You can now verify that Neovim is installed and accessible:
nvim --version
This command should display the Neovim version, confirming that the installation was successful.
Install AstroNvim
Now that Neovim is set up, let’s supercharge it with AstroNvim:
git clone --depth 1 https://github.com/AstroNvim/template ~/.config/nvim
rm -rf ~/.config/nvim/.git
Install AstroNvim Plugins
Launch Neovim:
nvim
Now, the plugins for AstroNvim plugins should install by itself. Press enter as much as it prompts you to. If it doesn’t type this into the normal mode of
:PlugInstall
Symlink your vim to nvim (Optional)
Deceptive heading, yeah I know. You can’t symlink since you guessed it — don’t have a sudo permission. The workaround is to add it’s alias to your .zshrc/.bashrc or equivalent.
alias vim="nvim"
Setting Up MYVIMRC
Now, to ensure your Neovim always loads the correct configuration, let’s set the MYVIMRC
environment variable. Open Neovim and check which configuration file Neovim is using:
vim
:echo $MYVIMRC
If it’s empty, let’s set it up. Add the following line to your ~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
file:
export MYVIMRC="$HOME/.config/nvim/init.lua"
Now that you understand the nitty-gritty of setting it up, I will do you one better. Here is a script that can automate and spin up the whole set-up process without much input from you. In your terminal, create a script_name.sh file (touch script_name.sh) and make it executable (chmod +x script_name.sh). Then, copy and paste these lines of code into the created file, and run it (bash ./script_name.sh).
#! /bin/bash
function print_color()
{
NC='\033[0m' # No Color
case $1 in
"deepgreen") COLOR='\033[0;38;5;22m' ;;
"orange") COLOR='\033[0;33m' ;;
"green") COLOR='\033[0;32m' ;;
"red") COLOR='\033[0;31m' ;;
"*") COLOR='\033[0m' ;;
esac
echo
echo -e "${COLOR} $2 ${NC}"
}
function clone_repo() {
local repo_url=$1
local repo_dir=$2
output=$(git clone $repo_url $repo_dir 2>&1) || \
{ print_color "red" "Failed to clone repository: $output"; exit 3; }
}
function dir_exist() {
if [ -d "$1" ]; then
print_color "" "Entering into $1 directory"
cd "$1" || { print_color "red" "Unable to change directory!!!"; exit 2; }
print_color "deepgreen" "Successfully changed directory"
else
print_color "red" "$1 directory doesn't exist!"
exit 2
fi
}
function update_progress() {
local percentage=$1
local bar_length=50
local progress=$((bar_length * percentage / 100))
local bar=$(printf "%${progress}s" | tr ' ' '#')
printf "\rProgress: [%-${bar_length}s] %d%%\n" "$bar" "$percentage"
}
# Detect user's default shell and set shell configuration file
if [[ -n "$BASH" ]]; then
SHELL_RC_FILE="$HOME/.bashrc"
elif [[ -n "$ZSH_NAME" ]]; then
SHELL_RC_FILE="$HOME/.zshrc"
else
# Default to .bashrc if neither Bash nor Zsh is detected
SHELL_RC_FILE="$HOME/.bashrc"
fi
# Install Neovim
print_color "orange" "---------------- Installing Neovim ------------------"
update_progress 0
clone_repo "https://github.com/neovim/neovim.git" "$HOME/neovim"
# Update progress after cloning Neovim
update_progress 20
# cd into neovim directory
dir_path="$HOME/neovim"
if [ -d "$dir_path" ]; then
cd "$dir_path" || { print_color "red" "Unable to change directory!!!"; exit 2; }
else
print_color "red" "$dir_path directory doesn't exist!"
exit 2
fi
update_progress 25
# Execute install commands and check for failure
make CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_INSTALL_PREFIX=$HOME/neovim > /dev/null 2>&1 && \
make -j4 > /dev/null 2>&1 && \
make install > /dev/null 2>&1 || \
{ print_color "red" "One of the commands failed"; exit 3; }
update_progress 60
# Append the export PATH command with a newline
echo -e "\nexport PATH=\$HOME/neovim/bin:\$PATH" >> "$SHELL_RC_FILE" || \
{ print_color "red" "Failed to write into $SHELL_RC_FILE"; exit 5; }
source "$SHELL_RC_FILE" || { print_color "red" "Failed to reload $SHELL_RC_FILE"; exit 6; }
update_progress 65
# Check if Neovim was successfully installed
nvim --version > /dev/null 2>&1 || { print_color "red" "Installation failed"; exit 4; }
# Clone AstoNvim
clone_repo "--depth 1 https://github.com/AstroNvim/template" "$HOME/.config/nvim"
dir_path="$HOME/.config/nvim"
if [ -d "$dir_path" ]; then
cd "$dir_path" || { print_color "red" "Unable to change directory!!!"; exit 2; }
else
print_color "red" "$dir_path directory doesn't exist!"
exit 2
fi
update_progress 80
# Install the AstroNvim plugins
nvim || { print_color "red" "Something went wrong..."; exit 7;}
update_progress 95
# Append the export PATH command with a newline
echo -e "\nexport MYVIMRC=\"$HOME/.config/nvim/init.lua\"" >> "$SHELL_RC_FILE" || \
{ print_color "red" "Failed to write into $SHELL_RC_FILE"; exit 8; }
update_progress 100
print_color "green" "---------------- AstroNvim successfully installed ------------------"
With Neovim and AstroNvim set up, you’re now equipped with a powerful coding environment tailored to your needs. Say goodbye to mouse dependence and hello to lightning-fast coding sessions. Happy coding!
OTHER ARTICLES
42 Push Swap Explained With Psuedocodes
Compilation Process in C
Heredoc: A Deep Dive
Neovim For 42 Students