Skip to content

Instantly share code, notes, and snippets.

@atripes
Created May 19, 2017 16:35
Show Gist options
  • Save atripes/15372281209daf5678cded1d410e6c16 to your computer and use it in GitHub Desktop.
Save atripes/15372281209daf5678cded1d410e6c16 to your computer and use it in GitHub Desktop.
URL encode/decode vim selection.
" URL encode/decode selection
vnoremap <leader>en :!python -c 'import sys,urllib;print urllib.quote(sys.stdin.read().strip())'<cr>
vnoremap <leader>de :!python -c 'import sys,urllib;print urllib.unquote(sys.stdin.read().strip())'<cr>
@Bhupesh-V
Copy link

Oi vim friends!

I have stopped using vim for some time now. Having said that @unphased here's a sample xnoremap usage for a function that uses UrlEncode: https://github.com/Bhupesh-V/.Varshney/blob/master/init.vim#L148

@unphased
Copy link

unphased commented Oct 2, 2024

Thanks! What do you use instead now?

The solution I use now is:

function! EncodeURIComponent() range
  let saved_reg = @"
  normal! gvy
  let selected_text = @"
  let encoded = system('node -e "process.stdout.write(encodeURIComponent(process.argv[1]))" -- ' . shellescape(selected_text))
  let @" = encoded
  normal! gv"_d"0P
  let @" = saved_reg
endfunction
vnoremap <leader>ue :call EncodeURIComponent()<CR>

Interesting use case you got there of internet searching something from a buffer. That is pretty neat.

I just wanted to be able to write text fragment links which need to be url-encoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment