How do I autocomplete an entire line based on existing lines in the file?
Answer
<C-x><C-l>
Explanation
The <C-x><C-l> command triggers whole-line completion in insert mode. Vim scans the current buffer (and other sources) for lines that start with the same text you've typed so far, and offers them as completions. This is incredibly useful for duplicating similar lines — function calls, import statements, struct fields — without leaving insert mode or recording a macro.
How it works
- While in insert mode, type the beginning of a line
- Press
<C-x><C-l>to trigger line completion - Vim searches for lines in the current buffer (and included files, depending on
completeoption) that start with the same prefix - A popup menu appears with matching lines
- Press
<C-n>or<C-p>to navigate through matches, or<CR>/<C-y>to accept - Press
<C-e>to dismiss the menu and keep what you originally typed
Example
Given the text already in the buffer:
import os
import sys
import json
import logging
Start a new line and type import then press <C-x><C-l>. A popup appears showing all four import lines. Select the one you want with <C-n>/<C-p> and press <C-y> to insert it.
Or in a configuration file:
server.host = "localhost"
server.port = 8080
server.timeout = 30
Type server. and press <C-x><C-l> to see all lines starting with server. as completions.
Tips
- Press
<C-x><C-l>again after accepting a match to complete the next line as well — Vim chains sequential line completions, which is perfect for duplicating multi-line blocks - Other useful insert-mode completions in the
<C-x>family:<C-x><C-f>— filename completion<C-x><C-n>— keyword completion from current file<C-x><C-k>— dictionary completion<C-x><C-o>— omni completion (language-aware)
<C-n>and<C-p>on their own (without<C-x>) do generic keyword completion, scanning multiple sources- Adjust which sources Vim searches with
:set complete=.,w,b,u,t— the default scans the current buffer, other windows, other buffers, unloaded buffers, and tags - Line completion is smarter than copy-paste because it matches by prefix and lets you pick from multiple candidates interactively