Skip to main content

Overview

The C++ Algorithm Snippets library provides automated snippet generation scripts that scan the repository and create code snippets for your editor. This enables instant access to over 100+ competitive programming algorithms and data structures.

Available Scripts

  • generate_vscode_snippets.py - Automatically generates VS Code snippets from all .cpp files
  • generate_snippets.sh - Creates UltiSnips format snippets for Vim/Neovim

VS Code Setup

1

Clone the Repository

First, clone the repository to your local machine:
git clone https://github.com/LuchoBazz/cpp-algorithm-snippets.git
cd cpp-algorithm-snippets
2

Run the Snippet Generator

Execute the Python script to generate VS Code snippets:
python3 generate_vscode_snippets.py
The script will:
  • Scan all subdirectories for .cpp files
  • Extract code from each file
  • Convert Vim date placeholders to VS Code variables
  • Generate snippets in your VS Code snippets directory
3

Snippet Location

Snippets are automatically placed in the correct location based on your OS:
%APPDATA%\Code\User\snippets\cpp.json
4

Verify Installation

Open VS Code and create a new .cpp file. Start typing a snippet prefix like graph_dijkstra_std and you should see autocomplete suggestions.
The snippet prefix is always the filename without the .cpp extension.

How the VS Code Generator Works

The generate_vscode_snippets.py script performs the following operations:
def add_snippets_to_vscode(root_path, filename, ext):
    # 1. Read the existing cpp.json file
    with open(PATH, 'r') as json_data:
        snippets = json.loads(json_data.read())
    
    # 2. Read the .cpp source file
    code = []
    with open(path, 'r') as reader:
        code = reader.read().split('\n')
    
    # 3. Convert Vim date placeholders to VS Code variables
    if 'template_' in filename:
        for i in range(len(code)):
            line = code[i].replace(
                '`!v strftime("%B %d, %Y")`',
                '${CURRENT_MONTH_NAME} ${CURRENT_DATE}, ${CURRENT_YEAR}'
            )
            code[i] = line
    
    # 4. Create snippet with filename as prefix
    data = {
        'prefix': filename,
        'body': code
    }
    snippets[filename] = data
The script automatically handles OS detection and places snippets in the correct platform-specific directory.

Vim/Neovim Setup

1

Prerequisites

Ensure you have UltiSnips plugin installed. If not, install it using your plugin manager:
" Using vim-plug
Plug 'SirVer/ultisnips'

" Using Vundle
Plugin 'SirVer/ultisnips'
2

Create UltiSnips Directory

Create the required directory structure:
mkdir -p ~/.vim/UltiSnips/cpp
3

Run the Snippet Generator

Execute the bash script from the repository root:
bash generate_snippets.sh
The script will recursively find all .cpp files and generate UltiSnips format snippets.
4

Verify Installation

Open Vim/Neovim and create a new .cpp file. Type a snippet prefix (e.g., graph_dijkstra_std) and press your UltiSnips trigger key (default: <Tab>).

How the Vim Generator Works

The generate_snippets.sh script uses a simple but effective approach:
#!/bin/bash

# Clear existing snippets
echo "" > ~/.vim/UltiSnips/cpp/cpp_generate.snippets

# Find all .cpp files and generate snippets
for i in $(find -name '*.cpp'); do
   [ -f "$i" ] || break
   echo "Snippet ${i##*/} was generated"
   
   # Write snippet header
   echo "" >> ~/.vim/UltiSnips/cpp/cpp_generate.snippets
   echo "snippet $(basename -- "$i" .cpp) \"$(basename -- "$i" .cpp) autocomplete\"" >> ~/.vim/UltiSnips/cpp/cpp_generate.snippets
   
   # Append file contents
   cat "$i" >> ~/.vim/UltiSnips/cpp/cpp_generate.snippets
   
   # Close snippet
   echo "" >> ~/.vim/UltiSnips/cpp/cpp_generate.snippets
   echo "endsnippet" >> ~/.vim/UltiSnips/cpp/cpp_generate.snippets
   echo "" >> ~/.vim/UltiSnips/cpp/cpp_generate.snippets
done
The script overwrites the existing cpp_generate.snippets file each time it runs. Any manual modifications will be lost.

Verification

Test Your Installation

Create a test file to verify snippets are working:
// Type: template_std
// Then trigger autocomplete
You should see the complete template expand:
/**
 * @created     : March 05, 2026
 * @handle      : @LuchoBazz
 */

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    return 0;
}

Common Issues

  1. Check that cpp.json exists in your snippets directory
  2. Restart VS Code after running the script
  3. Verify you’re editing a .cpp file
  4. Check VS Code’s snippet settings are enabled
Make the script executable:
chmod +x generate_snippets.sh
  1. Verify UltiSnips is installed: :echo exists('g:UltiSnipsExpandTrigger')
  2. Check your trigger key mapping in .vimrc
  3. Ensure the snippets file exists: ~/.vim/UltiSnips/cpp/cpp_generate.snippets

Updating Snippets

When you pull new changes from the repository or modify snippet files:
python3 generate_vscode_snippets.py
Consider adding these commands as shell aliases for quick access:
alias update-cpp-snippets-vscode='cd ~/cpp-algorithm-snippets && python3 generate_vscode_snippets.py'
alias update-cpp-snippets-vim='cd ~/cpp-algorithm-snippets && bash generate_snippets.sh'

Next Steps

Quick Start

Learn how to use snippets in competitive programming

Snippet Usage

Master snippet triggers and customization

Build docs developers (and LLMs) love