I sometimes have to cut a line (d) and paste in in the middle of another line. I'd like to remove the whitespaces that vim has copied (i.e. trim/strip the content). How can I do that in vim?

A common use case is that.

value := getValue()
if value == "test" {
    // Do something with value

In go, it is idiomatic to use if value := getValue(); value == "test" if value is only used within the if. However, sometimes I write the variable declaration outside of the if because I either used the variable after the if (and later moved that code inside the if), or because I simply forgot it. So, I'd like to cut the line value := getValue() and paste it after if. If I press p or P, it is inserted after (or before) the current line. Can I somehow insert that line onto the current line and remove the starting/trailing whitespaces (tabs, new lines, and )? I often have to do that in Latex documents, too.

1 Answer 1


You can go to the line with getValue() and do something like ^D to cut the contents, then go to the space after if and press p to paste it. This is not perfect because it leaves an empty line (more precisely a line with spaces) where getValue() has been, and you need to add a space after pasting. But it's still a lot better than pasting the entire line.

If you feel like learning something useful, run :h operator and read the entire chapter there. Pay particular attention to "text objects", those are one of the most powerful intermediate level concepts in Vim. When you feel comfortable with the built-in text objects you can install the textobj-user plugin, and add support for many other objects. In particular the vim-go plugin adds support for Go functions as objects (among many other things), and textobj-latex adds support for equation objects in LaTeX.

  • 1
    Actually instead of ^ d $ you can do _ D (underscore capital d) to cut to end (without newline) from the beginning of text. (this will ignore indenting whitespace at start of line). Might be a bit easier to type.
    – user10607
    Commented Jul 11, 2015 at 15:05

Your Answer

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

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