3

If I type a URL and I want to extract the parameters from the query string quickly in VIM how would I do that?

For instance if I have:

http://www.somesite.org/page2014.html?utm_source=facebook&utm_medium=eventlinkonwall&utm_term=trapshoot&utm_content=calendar_event_on_wall&utm_campaign=trapshoot2014

I want to end up with something close to:

utm_source=facebook
utm_medium=eventlinkonwall
utm_term=trapshoot
utm_content=calendar_event_on_wall
utm_campaign=trapshoot2014
1
  • 2
    Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. Mar 12 '15 at 18:36
4

What about:

df?:s/&/\r/g
  • df? to delete everything up to & including the first ?
  • :s/&/\r/g to replace every & with a newline

You could prefix this with either ^ to go the start of the line, or B to go to the start of the word; use whatever is appropriate in the scenario. Be careful with automatically adding B in a keybind or command though, because if you're on the first character of the URL it will go to the word before the URL!

This will only work on well-formed URLs, the web would not be the web if there weren't a bunch of malformed URLs floating around ;-) Specifically, URL's which omit the ? and just use &key=value to start the first parameter seem to be common.
To fix this, you could search for either the first ?, OR the first &, which is unfortunately a bit more typing:

d/[?&]<CR>x:s/&/\r/g

The <CR> is the enter key.

If you do this sort of thing often, you could map it to a key:

:nnoremap <Leader>p df?:s/&/\r/g<CR>
:nnoremap <Leader>p d/[?&]<CR>x:s/&/\r/<CR>

Or make a command out of it:

:command! SplitURL exe 'normal df?'|:s/&/\r/g
2
  • Instead of ^ to go to the beginning of the line, you could use B to go to the beginning of the WORD.
    – aharris88
    Mar 12 '15 at 18:09
  • Not all URL all perfectly formed. A LOT don't have the ? character and just use the &. Browser automatically compensate (so you don't notice it much) but there are a significant number out there. So I would replace the df? with d/[?&]<enter>x Mar 12 '15 at 18:30
0

Try:

:s/[?&]\ze.\{-}=[^&\r[:blank:]]\+/\r/g

It replaces all ? and & ([?&]) with new line (\r) which are followed by a=b patterns ending with &, new line or space characters.

Where:

  • \zs and \ze - Is the start and end of a pattern (these can be used independently).
  • [?&] - Match either ? or & character.
  • .\{-} - Non-greedy quantifier which match as few as possible characters.
  • [^&\r[:blank:]] - Match all characters which are NOT &, new line, space and tab characters.
1
  • 2
    Minor note: You can use \zs and \ze independently. In this case, the \zs can be omitted since it's at the start of the pattern already.
    – tommcdo
    Mar 13 '15 at 18:23
0

If you're not doing this a lot of times, it's pretty quick to do it manually:

  1. df?: Delete to the start of the parameters,
  2. f&: Jump to the next parameter
  3. rEnter: Replace the & with a newline,
  4. Then just mash ;. repeatedly until you've separated all the parameters. (; jumps forward to the next occurrence of the last f command, and . repeats the last edit operation.)

Depending on how many parameters there are, it won't be many more keypresses that the answers already posted using :substitute, but, for me at least, it requires even less brainpower.

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.