I’m trying to execute base64 --decode
on a piece of text selected in Visual mode, but it is the entire line that seems to be passed to the base64
command, not just the current selection.
I’m selecting the text in Visual mode, then entering Normal mode, so that my command line looks like this:
:'<,'>!base64 --decode
How can I pass only the selected piece of the line to a shell-command invocation in Vim?
To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.
Base64 is an encoding, the strings you've posted are encoded. You can DECODE the base64 values into bytes (so just a sequence of bits). And from there, you need to know what these bytes represent and what original encoding they were represented in, if you wish to convert them again to a legible format.
The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.
If the text to be passed to the shell command is first yanked to a register, say, the unnamed one, one can use the following command:
:echo system('base64 --decode', @")
It is possible to combine copying the selected text and running the command into a single Visual-mode key mapping:
:vnoremap <leader>64 y:echo system('base64 --decode', @")<cr>
The mapping can further be modified to replace the selected text with the output of the shell command via the expression register:
:vnoremap <leader>64 c<c-r>=system('base64 --decode', @")<cr><esc>
You can use Python instead, which should work.
Select lines that you want to decode in Visual mode (via V), then execute the following command:
:'<,'>!python -m base64 -d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With