Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute ‘base64 --decode’ on selected text in Vim?

Tags:

vim

base64

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?

like image 366
Jonatan Avatar asked Oct 21 '11 06:10

Jonatan


People also ask

How do I decode base64 text in Linux?

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.

Is it possible to decode base64?

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.

What is == in base64?

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.


2 Answers

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> 
like image 151
ib. Avatar answered Sep 22 '22 05:09

ib.


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 
like image 31
kenorb Avatar answered Sep 20 '22 05:09

kenorb