5

There are times I know I just want to paste the content of the register inline where my cursor is, so the initial and trailing whitespace (newline, tab, etc) is not wanted.

Because selecting contents on a web browser does not always reflect what's being copied, I am forced to just clean up after pasting or paste in another line then copy the contents without whitespace to a proper place.

So how can I paste without the whitespace?

2

2 Answers 2

2

I needed to "cast" register contents into a certain (characterwise / linewise / blockwise) mode so often, I wrote the UnconditionalPaste plugin for it. It provides gcp, glp, etc. alternatives to the built-in paste commands that force a certain mode (and by now several more variations on this theme, like pasting with joined by commas or queried characters).

The gcp mapping pastes characterwise (inner newline characters and indent are flattened to a single space, leading and trailing removed).

If you just want to remove leading and trailing whitespace, you can use g=p with this expression: ingo#str#Trim(v:val).

2

If you want to paste where your cursor is, you could try pasting in insert mode with:

<C-R>"

The command above (- ctrl-r + " - ) will not add a new line.

If you have more than one line of text to paste, and want to get rid any empty space that might come along, you could create a macro:

qq
:e testtext
^d0
dG
:bd!
pq

This macro ('@q') will open a new buffer called testtext (':e testtext'), go to the first non-empty character on the line ('^') and delete until the beginning of the line ('d0'), close the buffer without saving (':bd!'), and paste the text on the original file without the initial blank spaces.

Just out of curiosity, could you give an example of a real situation? All situation I could think of, could be solved with a macro and/or a mapping. But I'm not sure what you are looking for.

Additional information about the topic:

Many tips on how to remove unwanted whitespaces

A script that deletes white spaces at the end of lines.

1
  • @Gregory-Nisbet 's comment seems to be way more interesting (he commented while I was writing, so I'll keep the answer above until the author decides which one helps).
    – lsrdg
    Dec 16, 2016 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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