vim yanked the entire line, but paste the text part later between texts?

84 Views Asked by At

Understand this sounds vary unsmart, but still ask here to learn vim way to do it.

let's say, we have your yanked the entire line in vim.( but the entire line only has few text ). now we would like to paste the text/entire line into some texts. where there are some other texts. ex:

hello world!
yanked text 

we can yanked the entire line of "yanked text" by shift + v, then y. now we would like to paste the yanked text between hello world. as:

hello yanked text  world!

Understand we could yanked the text only at the beginning instead of the entire line.

Q: after we yanked the entire line, then can we paste the yanked text between texts only? (instead of pasting the entire line) since yanking entire line seems much faster.

3

There are 3 best solutions below

2
mattb On BEST ANSWER

One option is to define new text objects:

" inner/around line text objects
" visual mode
xnoremap <silent> il <ESC>^vg_
xnoremap <silent> al <ESC>0v$
" operator pending mode
onoremap <silent> il :<C-u>normal! ^vg_<CR>
onoremap <silent> al :<C-u>normal! 0v$<CR>

The il text object will capture text on the line, minus any whitespace, whereas the al text object will capture the whole line, including whitespace (but not the newline as yy does:

                      'il'
    |------------------------------------------|
    here is some text with whitespace at the end      
|----------------------------------------------------|
                      'al'

Then you can use yil and p to insert it in the middle of some other line. doing yil or dil etc. on the above, then moving the cursor (indicated in the example below by a ^) to the middle of a different line of text:

    a new line
         ^

and pressing p gives:

    a new here is some text with whitespace at the endline
                                                     ^

and pressing p gives:

1
Andreas Louv On

Just yank the line with 0y$ or simply y$, which will yank from the cursor to the end of the line. This will allow you to paste the line as you would like.

Neovims default mappings maps Y to y$ as of this commit: 5a111c1b0 you could consider adding the same mapping if you like.

If you really want to the register to be character-wise, then you can use something like:

:call setreg('0', getreg('0'), 'c')

Which would allow you to paste yanked text\n between hello and world, resulting in:

hello yanked text 
world!
yanked text

Please just use y$.

0
romainl On

When you put from a register, the content of the register determines how it is put.

If the content of the register ends with an "EOL" character, the put is done "linewise", meaning that it happens on a new line, below or above the current one.

If the content of the register doesn't end with an "EOL" character, the put is done "characterwise", meaning that it happens in the current line, before or after the cursor.

When you do Vy or yy, it is the whole line, including the "EOL", that is yanked, so when you do p, the put is done "linewise" and you get your yanked line below the current line. It makes a lot of sense: you yank a line, you put a line.

If you want to yank the content of a line and thus ensure that it will be put "characterwise", which is relatively less common than what Vim is optimised for, you must omit the "EOL". For this you need to know these four motions:

  • 0 moves the cursor to the first character of the line,
  • ^ moves the cursor to the first non-whitespace character of the line,
  • $ moves the cursor to the last character of the line,
  • g_ moves the cursor to the last non-whitespace character of the line,

which you can combine with y to yank exactly what you want from the current line: ​

  • 0y$ yanks everything from the first character up to and excluding the "EOL",
  • ^y$ yanks everything from the first non-whitespace character up to and excluding the "EOL",
  • 0yg_ yanks everything from the first character up to the last non-whitespace character,
  • ^yg_ yanks everything from the first non-whitespace character up to the last non-whitespace character.

In my opinion, the commands above are intuitive and expressive enough to be used when needed but, if you really want a single character command, then you can simply make a custom mapping:

nnoremap <key> ^yg_

Reference:

:help linewise
:help 0
:help ^
:help $
:help g_