Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - yank from current cursor position till beginning of line

Tags:

vim

in VIM, I understand that we can do a yank till the end of line using y$ but if e.g. my text is abcdefg and my cursor is at 'g' and I enter y^ the line will be copy without the g. My idea is to copy the whole line without the line break, any similar action will do.

like image 850
drhanlau Avatar asked Dec 18 '12 13:12

drhanlau


People also ask

How to yank a line in Vim?

To yank one line, position the cursor anywhere on the line and type yy . Now move the cursor to the line above where you want the yanked line to be put (copied), and type p . A copy of the yanked line will appear in a new line below the cursor.

How can you yank 3 lines in Vim?

To copy text, place the cursor in the desired location and press the y key followed by the movement command. Below are some helpful yanking commands: yy - Yank (copy) the current line, including the newline character. 3yy - Yank (copy) three lines, starting from the line where the cursor is positioned.

How do you copy a range of a line in vi?

The vi yy command "yanks" the current line into the vi general buffer. That is, it copies the line for you to put into the file immediately. The vi p and P commands "put" the line back into the file just after (p) or just before (P) the line on which the cursor is resting.

How to select a whole line in Vim?

If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.


3 Answers

0y$$ - copy the line without line break and move cursor back to the end

like image 68
Mihkel Avatar answered Oct 24 '22 19:10

Mihkel


Making it a visual selection and then yanking that includes the character under the cursor:

v0y
like image 44
Matthew Strawbridge Avatar answered Oct 24 '22 19:10

Matthew Strawbridge


If all the characters are indeed together and conform to a "vim sentence", you could use visual selection for the sentence object. A sentence in this case would match abcdefg even if that is not starting at the beginning of a line and it will not include the line ending:

visy

If you want to include trailing whitespace you would use a instead of i (mnemonic for "inside"):

vasy

The only problem with this approach (which may be what you do not want) is that it will not include leading whitespace. So if you have something like:

    abcdefg

The selection will not include the leading chunk of whitespace, just abcdefg.

like image 40
alfredodeza Avatar answered Oct 24 '22 17:10

alfredodeza