HomeUNIXCut and Paste Text in vi or vim

Cut and Paste Text in vi or vim

Cutting and pasting text in an editor is a basic requirement. This tech-recipe describes the cut and paste functions of vi and vim.


Cutting text in vi/vim is achieved with the various delete commands which are summarized below:

dd Delete current line
D Delete from cursor to end of line
d$ Delete from cursor to end of line
d0 Delete from cursor to beginning of line
dw Delete from cursor to end of current word
db Delete from cursor to beginning of current word

These commands must be given in command mode in vi/vim, not insert mode. All of these commands can be preceded by a number that is interpreted as the number of operations to perform. For example, 5dd is interpreted as delete 5 lines.

When a deletion occurs, the deleted text is stored temporarily in a buffer. This buffer can be pasted into the document relative to the current cursor position by using the p (lowercase p) paste after the current cursor position or the P (upper case P) paste before the current cursor position commands.

Beware that some commands in vi/vim will empty the buffer. Cursor movement is safe. However, since there is only one buffer that is used for both cut/copy/paste/undo/redo, any function that uses the buffer (insert, replace, etc.) will send your cut text to limbo.

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!