3

I have a line of text that looks like this

foo bar http://www.example.com -> baz

I want yank the url part using ex. Anyone have any ideas on how to do this?

To clarify. I want to do something like this :y/http:.*\.com/ from the command line. But that doesn't work.

Matt Peck
  • 43
  • 4

4 Answers4

6

While on the beginning of a line,

wwviWy

will yank the URL part while not changing the cursor position.


If you want to do it as a command

:execute "normal! wwviWy0"

does the same thing.

Rook
  • 54,867
  • 44
  • 156
  • 233
  • Perfect, thats exactly what I need. – Matt Peck Nov 22 '11 at 16:50
  • 1
    Why `:execute` instead of `:normal!` directly? With @Benoit suggestion, it can be as short as `:norm! 2WyE` – sidyll Nov 22 '11 at 18:06
  • @sidyll - I'm sure it can be shortened/improved in many ways. This one, however is the first that came to mind, and I rarely use pre-numbers, so ... and the OP got his solution. Moving on ... – Rook Nov 22 '11 at 18:28
  • @sidyll - Also, I consider it bad practice using abbreviations in .vim files (for when typing, ok, but not in scripts). They make an already hard to read language completely unreadable. Prefer the full form wherever possible. – Rook Nov 22 '11 at 18:29
  • 1
    @ldigas I understand. However the real point of my comment was intended to be the (useless) use of `:execute` as a "wrapper". You can call `:normal` directly. :-) – sidyll Nov 22 '11 at 18:33
  • 1
    @sidyll - Oh, yes, you're right about that. I prefer this way, because I don't have to think two case scenarious; with and without escape characters. This way "it just works". – Rook Nov 22 '11 at 18:49
2

The URL fits the definition of a WORD, so you can do it with yW when the cursor is at the beginning.

Cat Plus Plus
  • 113,388
  • 26
  • 185
  • 215
  • Yeah, but how can it be done from the command line. I want to be able to do something like `:y/http:.*\.com/` but that doesn't work. – Matt Peck Nov 22 '11 at 16:44
0

I would use /http to get at the begining of the URL if I were on a different line, or fh if I were on the same line and then yt (last key is space).

deviousdodo
  • 9,023
  • 2
  • 27
  • 33
  • The "`t `" part is generally better expressed as `E`. – bitmask Nov 22 '11 at 17:21
  • @bitmask I generally prefer `t ` because it's generic (ie. it's not that I always look for a space), not only word-related. This way I don't have to use 2 different shortcuts or care about word-separators. – deviousdodo Nov 22 '11 at 17:30
0
normal /http^v^m^v^mv2fm <Enter>

Where ^v = Ctrl + v and ^m = Enter

Brian Deragon
  • 2,885
  • 22
  • 44
SergioAraujo
  • 8,735
  • 1
  • 46
  • 37