The "c" letter was already used
for the change operator, and "y" was still available. Calling this
operator "yank" made it easier to remember to use the "y" key.
Since "y" is an operator, you use "yw" to yank a word. A count is possible as
usual. To yank two words use "y2w". Example:
let sqr = LongVariable * ~
-------------->
y2w
let sqr = LongVariable * ~
p
let sqr = LongVariable * LongVariable ~
Notice that "yw" includes the white space after a word. If you don't want
this, use "ye".
The "yy" command yanks a whole line, just like "dd" deletes a whole line.
a text line yy a text line a text line
line 2 line 2 p line 2
last line last line a text line
last line
"Y" was originally equivalent to "yank the entire line", as opposed to "D"
which is "delete to end of the line". "Y" has thus been remapped to mean
"yank to end of the line" to make it consistent with the behavior of "D".
Mappings will be covered in later chapters.
==============================================================================
*04.7* Using the clipboard
If you are using the GUI version of Vim (gvim), you can find the "Copy" item
in the "Edit" menu. First select some text with Visual mode, then use the
Edit/Copy menu item. The selected text is now copied to the clipboard. You
can paste the text in other programs. In Vim itself too.
If you have copied text to the clipboard in another application, you can paste
it in Vim with the Edit/Paste menu item. This works in Normal mode and Insert
mode. In Visual mode the selected text is replaced with the pasted text.
The "Cut" menu item deletes the text before it's put on the clipboard. The
"Copy", "Cut" and "Paste" items are also available in the popup menu (only
when there is a popup menu, of course). If your Vim has a toolbar, you can
also find these items there.
If you are not using the GUI, or if you don't like using a menu, you have to
use another way. You use the normal "y" (yank) and "p" (put) commands, but
prepend "* (double-quote star) before it. To copy a line to the clipboard: >
"*yy
To put text from the clipboard back into the text: >
"*p
This only works on versions of Vim that include clipboard support. More about
the clipboard can be found in section |09.3| and here: |clipboard|.
==============================================================================
*04.8* Text objects
If the cursor is in the middle of a word and you want to delete that word, you
need to move back to its start before you can do "dw". There is a simpler way
to do this: "daw".
this is some example text. ~
daw
this is some text. ~
The "d" of "daw" is the delete operator. "aw" is a text object. Hint: "aw"
stands for "A Word". Thus "daw" is "Delete A Word". To be precise, the white
space after the word is also deleted (or the white space before the word if at
the end of the line).
Using text objects is the third way to make changes in Vim. We already had
operator-motion and Visual mode. Now we add operator-text object.
It is very similar to operator-motion, but instead of operating on the text
between the cursor position before and after a movement command, the text
object is used as a whole. It doesn't matter where in the object the cursor
was.
To change a whole sentence use "cis". Take this text:
Hello there. This ~
is an example. Just ~
some text. ~
Move to the start of the second line, on "is an". Now use "cis":
Hello there. Just ~
some text. ~
The cursor is in between