How do I paste register contents at the beginning of a file without moving the cursor there first?
Answer
:0put
Explanation
The :put Ex command inserts register contents as a new line below the specified line number. Using :0put — line zero — places the content before line 1, prepending it at the very top of the file without requiring any cursor movement.
How it works
:put {reg}— paste register{reg}below the current line (linewise):{N}put {reg}— paste below line N regardless of cursor position:0put {reg}— paste before line 1 (top of file):$put {reg}— paste after the last line (end of file)
The register defaults to the unnamed register " if omitted. Unlike p or P, :put always pastes linewise regardless of the register's stored type.
Example
Given a JavaScript file:
function greet() { return 'hello'; }
module.exports = { greet };
Yank 'use strict'; into the unnamed register, then run :0put:
'use strict';
function greet() { return 'hello'; }
module.exports = { greet };
Tips
:0put =system('date')— insert shell command output at the top using the expression register:$put a— append registeracontents at the end of the file- Works safely inside macros — no cursor repositioning needed before or after
- To move a block to the top without copying: yank it with
yap, delete it, then:0put