0

With other words: How to copy selection without its root-level indentation?

Example:

How to copy this:

a
  b
    c
  d

instead of:

    a
      b
        c
      d

I am using Neovim inside VS Code but I think this is not relevant since it loads the bindings from the Neovim configuration file.

4
  • Thank you! In the meantime I found the solution with the help of a colleague. I am posting an answer in a few minutes. – silviubogan Oct 6 '20 at 9:47
  • Oh, wait. Silly me! Block selection with $ will work. – B Layer Oct 6 '20 at 9:51
  • My answer works only in VS Code which is enough for me, but if you have another solution, please post it. Thank you. – silviubogan Oct 6 '20 at 9:58
  • Done. You'll have to try it in VS Code...I know it works in Vim. – B Layer Oct 6 '20 at 9:59
3

Here's a pure Vim solution:

  1. Put the cursor on the first line on the leftmost column you want to yank
  2. Hit Ctrl+V
  3. Move the cursor down to the last line
  4. Hit $

Yank away.

3

In Vim there are :h [p and :h ]p key bindings which do both "put" and "indent adjust" with respect to the current line. Normally that's exactly what one wants.

3
  • Cool. Don't recall seeing this one before. However, I notice some cases where it loses indentation in unexpected (to me, at least) ways. A paragraph with first line indent, for instance. Meanwhile, it doesn't happen with ctrl-v + $. ;) (I suppose there could be some fo-options combo that avoids it but it kinda threw me.) Not your fault, though, so +1. – B Layer Oct 6 '20 at 18:57
  • 1
    @BLayer The logic is hardcoded in src/register.c:do_put(). Basically, it's new_indent = max(old_indent + indent_diff, 0), where indent_diff is a difference between reference line and the first inserted line. Quite primitive solution, but works mostly well for code. – Matt Oct 7 '20 at 5:14
  • Interesting. And good to know when using it. – B Layer Oct 7 '20 at 18:35
-2

This works only in VS Code:

  1. Put the cursor on the first character of the contents that you want to copy, at the inner indentation level you want.
  2. Keep Shift and Alt key pressed and click on the first character (from step 1) and drag until everything you want is selected.
  3. Use "+y to copy that to the clipboard.

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.