0

I am trying to write a command that yanks the whitespace (indentation) at the start of a line as well as yanking some other object.

I have several ideas of how this could be done.

Consider the following example, where we want to make a new command similar to "yiw" which yanks the variable name hello_world but also yanks the whitespace before double.

// no indentation on this line
    double hello_world = 10.0; // there is some indentation on this line
  • We could either store the current cursor position, go to the start of the line, yank the whitespace, go back to where we were, execute the yiw command and somehow get these 2 yanks to "concatinate".

  • Or we could execute the yiw command, go to the end of the line, add a new line, put the yank buffer, go back up to the start of the line, yank the whitespace, go down again, put the whitespace at the start of the line, delete-yank the whole line and put it again. (So that the yank buffer contains what we want.)

  • Or some combination of the above.

Firstly, while not essential, I don't know how to manipulate the yank buffer, or how to "concatinate" to it.

Secondly I do not know how to save the cursor position and then restore it. This is also not essential but the "work around" as described above is messy.

What is the best way of accomplishing a yank and retaining the indentation whitespace?

This is my attempt so far. It works but doesn't seem like a particularly good solution. Also "o" seems like an inefficient combination to put in a command since everything else is done without entering insert mode.

nnoremap <S-y>iw yiwo<ESC>p-0y^<DOWN>PddP
  • 1
    As an aside, are you already aware of :help ]p? – Rich Jan 24 '18 at 13:31
  • @Rich No what does that do? – user3728501 Jan 24 '18 at 13:48
  • It's an auto-indent version of "put"? – user3728501 Jan 24 '18 at 13:48
  • 1
    Yes: I don't know why you want to yank the indent, but if it's just so you can paste with indent, that's what ]p etc are for. – Rich Jan 24 '18 at 14:03
  • May also want to look at unimpaired.vim which "improves" ]p and friends. It also provides =p which puts/pastes a block then re-indents with = command. – Peter Rincker Jan 24 '18 at 15:05
2

You can concatenate to some registers using capital letters. "Ay concatenates to instead of replaces register a. However, there is no way to concatenate yank to the unnamed register.

Moving the cursor and restoring it can be done with marks and/or <c-o>. For example you could m' to save the cursor position, then `' to restore it, or simply use <c-o>. Alternatively, you can use named marks like ma and `a. Both of these have side-effects.

I recommend using command syntax instead. In this case, it is easier to write and understand, and has fewer side-effects than your proposed methods.

nnoremap <silent> <leader>yiw :<c-u>let @"=repeat(' ', indent('.')).expand('<cword>')<cr>

A few of the features need explaining:

  1. let @"= places the expression in the unnamed register.
  2. repeat(' ', N) is a series of N spaces and indent('.') gets the number of spaces at the beginning of the line.
  3. expand('<cword>') retrieves the current word, similar to iw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.