Get AI Code and Text Completion in Vim with Llama.vim
Job to be done: Integrate a local LLM for code and text completion and editing within Vim
🇳🇬 Ways to use this in Nigeria
Ideas to get you started, adapt to your situation.
- Student
A computer science student uses Vim's local AI to complete Python function definitions and debug code for their CSC301 assignment, speeding up homework completion.
- Entrepreneur
A technical founder developing their startup's MVP uses Vim's integrated AI to quickly draft and refactor backend API code, accelerating product development offline.
- 9-5 employee
A software engineer uses Vim's local AI to complete complex code blocks and refactor existing functions during development sprints, improving efficiency without sending proprietary code to external services.
What you’ll get
You will set up your Vim text editor to use a local AI (Large Language Model) for smart code and text suggestions, completions, and editing. This means the AI runs directly on your computer, offering privacy and speed without needing an internet connection for every suggestion. This approach is useful for developers who want AI assistance without sending their code to external services.
Tools you need
- Vim (free): A powerful text editor often used by programmers.
- Git (free): A tool for managing code versions, used here to download software.
- Terminal (free): A program on your computer where you type commands (like Command Prompt on Windows or Terminal on macOS/Linux).
- vim-plug (free): A popular tool that helps you install and manage plugins (add-ons) for Vim.
- llama.vim (free): The Vim plugin that connects Vim to a local AI model.
- llama.cpp (free): The software that runs the local AI model on your computer.
- Qwen2.5-Coder (free): An example AI model (Large Language Model) designed for coding tasks, which you will download and run locally.
Steps
-
Install Vim and Git: If you don’t have Vim (or Neovim) and Git installed, you’ll need to install them first.
- On Windows: Download the installer for Vim from
https://www.vim.org/download.php#pc(choosegvim_9.x.exe). Download the installer for Git fromhttps://git-scm.com/download/win. Follow the installation prompts. - On macOS: Open your Terminal app (search for “Terminal” in Spotlight) and run:
If you don’t have Homebrew, install it first by following instructions on# macOS brew install vim githttps://brew.sh. - On Linux: Open your terminal and run:
# Linux (Debian/Ubuntu) sudo apt update && sudo apt install vim git # Linux (Fedora) sudo dnf install vim git
You should now have Vim and Git available from your terminal. Type
vim --versionandgit --versionto check. - On Windows: Download the installer for Vim from
-
Install a Vim Plugin Manager (vim-plug): This makes installing
llama.vimmuch easier.- Open your Terminal (Command Prompt on Windows, Terminal on macOS/Linux).
- Install vim-plug:
# macOS or Linux curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim# Windows (PowerShell) New-Item -ItemType Directory -Force "$HOME\.vim\autoload" Invoke-WebRequest -Uri https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim -OutFile "$HOME\.vim\autoload\plug.vim"
You should see a
plug.vimfile created in the specified directory. -
Configure Vim to use llama.vim: You need to edit your Vim configuration file (
.vimrc) to tell Vim about the plugin.- Open your
.vimrcfile:- On macOS/Linux: Open Terminal and type
vim ~/.vimrc. If the file doesn’t exist, Vim will create it. - On Windows: Open Command Prompt and type
notepad $HOME\_vimrc. If the file doesn’t exist, Notepad will ask if you want to create it.
- On macOS/Linux: Open Terminal and type
- Add the following lines to your
.vimrcfile. If you already have acall plug#begin()andcall plug#end()section, add'ggml-org/llama.vim'inside it." Add these lines to your .vimrc file call plug#begin('~/.vim/plugged') Plug 'ggml-org/llama.vim' call plug#end() " Optional: Configure llama.vim (example: disable auto Fill-In-the-Middle completion) " let g:llama_config = { 'auto_fim': v:false } - Save and close the
.vimrcfile. In Vim, type:wqand press Enter. In Notepad, save the file. You have now told Vim to load thellama.vimplugin.
- Open your
-
Install llama.vim: Now, open Vim and tell the plugin manager to install
llama.vim.- Open Vim: In your Terminal, type
vimand press Enter. - Install plugins: In Vim, type
:PlugInstalland press Enter. You should see a window pop up showingllama.vimbeing downloaded and installed. Once it’s done, pressqto close the window.
- Open Vim: In your Terminal, type
-
Install llama.cpp: This is the software that will run the AI model locally.
- Open your Terminal.
- On macOS:
# macOS brew install llama.cpp - On Windows:
If# Windows (PowerShell) winget install llama.cppwingetis not available, you might need to install it from the Microsoft Store or buildllama.cppfrom source (more advanced). - For other OS or if
brew/wingetfails: You can buildllama.cppfrom its source code. This is more technical.# macOS or Linux (build from source) git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp make# Windows (PowerShell, build from source - requires Visual Studio Build Tools) git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build --config Release
You should now have the
llama.cpptools, includingllama-server, installed or built. -
Download an AI Model: You need a “Fill-in-the-Middle (FIM)” compatible AI model. The source recommends Qwen2.5-Coder.
- Go to the Hugging Face collection: Visit
https://huggingface.co/collections/Qwen/qwen2-5-coder-6693d2543232015243292497. - Choose a model: Look for a model with a
.ggufextension. The source mentions “Qwen2.5-Coder 1.5B Q8_0”. This means a 1.5 billion parameter model, quantized to 8-bit, which is good for less powerful hardware.- Find
Qwen2.5-Coder-1.5B-Q8_0.ggufor a similar small, quantized version. - Click on the model file, then click the “Download” button.
- Find
- Save the model: Save the
.gguffile to a memorable location on your computer, for example,C:\llama_models\on Windows or~/llama_models/on macOS/Linux. You should have a.gguffile (e.g.,Qwen2.5-Coder-1.5B-Q8_0.gguf) saved on your computer.
- Go to the Hugging Face collection: Visit
-
Start the llama.cpp Server: This runs the AI model in the background, ready for Vim to connect to it.
- Open a new Terminal window (keep this window open as long as you want AI suggestions in Vim).
- Navigate to your
llama.cppdirectory if you built it from source, otherwise, ensurellama-serveris in your system’s PATH. - Run the server command: Replace
[PATH_TO_YOUR_MODEL]with the actual path to the.gguffile you downloaded. The source recommends settings based on VRAM (Video RAM, memory on your graphics card). For less than 8GB VRAM, use the 1.5B model.# macOS or Linux llama-server -m [PATH_TO_YOUR_MODEL]/Qwen2.5-Coder-1.5B-Q8_0.gguf --fim-qwen-1.5b-default# Windows (PowerShell) llama-server -m "[PATH_TO_YOUR_MODEL]\Qwen2.5-Coder-1.5B-Q8_0.gguf" --fim-qwen-1.5b-default- Example for 16GB+ VRAM: If you have more VRAM, you could use a larger model and its corresponding flag, e.g.,
--fim-qwen-7b-defaultwith a 7B model. You should see the server start up, showing messages about loading the model and listening for connections. It will stay running in this terminal window.
- Example for 16GB+ VRAM: If you have more VRAM, you could use a larger model and its corresponding flag, e.g.,
-
Use AI Completion in Vim: Now, switch back to Vim and try it out.
- Open Vim (or a file in Vim) in a separate Terminal window.
- Start typing code or text in Insert mode (press
i). - Wait for suggestions: The source says “Auto-suggest on cursor movement in Insert mode”. You should see AI suggestions appear (often in a different color, like orange as shown in the source’s example).
- Accept suggestions:
- Press
Tabto accept the full suggestion. - Press
Shift+Tabto accept only the first line of the suggestion.
- Press
- Instruction-based editing: The source mentions
leader lli. Theleaderkey is usually\(backslash) by default in Vim. So, in Normal mode, type\llito trigger instruction-based editing. You would then type your instruction (e.g., “refactor this function to be more concise”) and press Enter. You should see AI suggestions appearing as you type, and you can accept them to quickly complete code or text.
Original source
This workflow is based on llama.vim, a Vim plugin developed by kgwgk and hosted on GitHub. It enables local Large Language Model (LLM) assistance for text completion and editing directly within the Vim editor.
Notes & variations
- Free-tier alternatives: All tools in this workflow are free and open-source, so there are no paid tiers to worry about. The main cost is your computer’s hardware, especially VRAM for larger models.
- Common mistake: Not starting the
llama.cppserver before opening Vim, or closing the server’s terminal window while Vim is still running. The server must be active in the background forllama.vimto work. Also, ensure the model path in thellama-servercommand is correct. - Tip for better results: Experiment with different FIM-compatible models from Hugging Face. Larger models (e.g., 7B or 30B parameters) generally provide better quality suggestions but require more VRAM. Always check the
llama.cppdocumentation or the model card on Hugging Face for recommendedllama-serverflags and model types. If you have a powerful GPU, consider buildingllama.cppwith GPU support for faster inference.