How to yy macro on console
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 4, 2026
Key Facts
- yy command copies entire lines in vim/vi editors without selection
- Works in normal mode by pressing 'y' twice consecutively
- Can be combined with numbers like 5yy to copy 5 lines at once
- Copied content stays in vim buffer until overwritten by new copy operation
- Paste operation uses 'p' for after cursor or 'P' for before cursor placement
What It Is
The 'yy' macro is a keyboard command used in vim and vi text editors to copy or 'yank' an entire line of text into the editor's buffer. This command is fundamental to vim's modal editing system, where different modes perform different functions. The 'yy' command specifically operates in normal mode, allowing users to quickly duplicate or move lines without using a mouse. Understanding this command is essential for efficient text editing in vim and related command-line environments.
The vim text editor was created in 1991 by Bram Moolenaar as an improved version of the earlier vi editor, which originated at UC Berkeley in 1976. The 'yy' command is part of vim's comprehensive keyboard shortcut system that evolved from vi's original design. Over the past three decades, vim has become the standard text editor for system administrators, programmers, and developers working in Unix-like environments. The command structure and efficiency of vim have made it one of the most enduring text editors in computing history.
There are three main variations of the yank command in vim: 'yy' for entire lines, 'y$' for from cursor to end of line, and 'yw' for copying individual words. Users can also combine yank with motion commands like 'y2w' to copy two words or 'yap' to copy entire paragraphs. The yank command can be prefixed with numbers to repeat the action multiple times, such as '3yy' to copy three consecutive lines. Understanding these variations allows users to optimize their editing workflow based on the specific text they need to copy.
How It Works
To execute the 'yy' command, you must first ensure you are in normal mode by pressing the Escape key if needed, then position your cursor anywhere on the line you wish to copy. Press the 'y' key twice in quick succession without releasing the first 'y', and the entire line will be copied to vim's unnamed buffer. The cursor remains in the same position after the copy operation, and vim typically displays a confirmation message at the bottom of the screen. The copied content remains in the buffer until you perform another copy operation or until you exit vim.
In a practical example, consider editing a configuration file using vim on a Linux server where you need to duplicate a line with server settings. You position your cursor on the line containing 'server_address=192.168.1.100', press 'yy' to copy it, move your cursor to a new location in the file using arrow keys or vim motions, and then press 'p' to paste the line below the current line or 'P' to paste above it. This process can be repeated multiple times to create variations of configuration entries. Tools like tmux sessions and ssh connections often display vim as the default editor, making this skill invaluable for remote system administration.
For advanced usage, you can combine 'yy' with other vim features to create powerful editing workflows with minimal keystrokes. Typing '5yy' will copy five consecutive lines starting from the cursor position, allowing you to duplicate large blocks of code or configuration sections. You can also use ':%y' to copy the entire file to the buffer, or 'yab' to copy content within specific scope markers. Understanding these combinations helps speed up editing tasks significantly in large files with hundreds or thousands of lines.
Why It Matters
Learning the 'yy' command is crucial for anyone working in terminal-based environments where graphical text editors may not be available or practical. System administrators managing multiple servers often need to quickly duplicate configuration lines or code snippets across different files and systems. The ability to efficiently copy and paste text in vim directly impacts productivity when working on remote servers where mouse interactions are limited or unavailable. Studies show that developers proficient in vim shortcuts can edit code 30-40% faster than those relying on mouse-based selection and copying.
The 'yy' command is used extensively in DevOps workflows where engineers manage infrastructure-as-code across platforms like Terraform, Ansible, and Kubernetes manifests. Software developers working with languages like Python, Go, and Rust often duplicate functions or modules to create new implementations, making quick line copying essential. System configuration tasks on Linux, BSD, and Unix systems frequently require duplicating entire configuration blocks from files like /etc/nginx/nginx.conf, /etc/apache2/apache2.conf, or /etc/ssh/sshd_config. The efficiency gained from mastering this command multiplies significantly when working with large codebases or configuration management systems.
Future trends in text editing increasingly focus on keyboard-driven interfaces and terminal-based tools as containerization and cloud computing make remote access more common. The rise of tools like Neovim, an advanced fork of vim, continues to improve and extend vim's capabilities while maintaining compatibility with the 'yy' command. Educational institutions are beginning to teach vim proficiency as part of computer science curricula, recognizing its importance in professional development work. As artificial intelligence and machine learning tools integrate more deeply into development workflows, the ability to quickly manipulate and duplicate code snippets using vim commands remains a valuable skill.
Common Misconceptions
One common misconception is that 'yy' requires the line to be selected or highlighted before copying, similar to graphical editors like Microsoft Word or Google Docs. In reality, vim's 'yy' command operates on the line where the cursor is positioned without any need for visual selection or highlighting. Many users coming from graphical editing backgrounds assume they need to see selected text to confirm the copy operation, but vim's design eliminates this requirement. This misunderstanding often leads new vim users to spend extra time trying to select text when they could simply position the cursor and press 'yy'.
Another myth is that the 'yy' command only works with complete lines and cannot be used for partial line copying or manipulation. While 'yy' specifically copies entire lines, vim provides numerous alternatives like 'y$' to copy from cursor to end of line, 'y^' to copy from cursor to line beginning, and 'y%' for copying matched block content. Users can also use 'y(' or 'y)' to copy sentence content, and 'y{' or 'y}' for paragraph content depending on vim plugins installed. The variety of yank options makes vim far more flexible than many users initially realize.
A third misconception is that using 'yy' and 'p' commands is inefficient compared to modern graphical copy-paste methods, leading some developers to avoid learning vim entirely. Research and real-world usage demonstrate that keyboard-based copying is actually faster than reaching for a mouse, moving to the correct position, selecting text, and copying, especially over remote connections with latency. Professional vim users can copy and paste content in a matter of seconds using muscle memory and key combinations, while graphical methods require visual targeting and multiple steps. The learning curve for vim is steep initially, but proficiency reaches much higher efficiency levels than graphical alternatives within weeks of regular practice.