How to execute ‘base64 --decode’ on selected text in Vim?
Solution 1
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>
Solution 2
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
Solution 3
If you want to replace the text with the output of base64
, use something like
:vnoremap <leader>64 y:let @"=system('base64 --decode', @")<cr>gvP
Related videos on Youtube
Jonatan
Embedded Linux software engineer. Open source fan, wannabe electronics hobbyist.
Updated on June 13, 2022Comments
-
Jonatan almost 2 years
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 thebase64
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?
-
daniel kullmann over 12 yearsDo you want to replace the selected text?
-
Jonatan over 12 yearsReplacing would be OK, plain output to the console would be OK as well.
-
-
shredding about 11 yearsIt is possible to echo the first example into a new vim tab?
-
shredding about 11 yearsOr how would I make a macro out if it?
-
ib. about 11 years@shredding: Just add a command opening an empty buffer in a new tab page between copying and calling
base64
::vnoremap <leader>64 y:tabe\|pu!=system('base64 -d', @@)<cr>
. -
Jonathan Dumaine over 10 yearsBonus points for a function that toggles between base64 and plaintext via the same keybind?
-
mosh about 6 yearsIf it was possible to toggle base64, there would be no need for base64 -d option. Example: Any 4 letter word can be base64 decoded or encoded.
-
Matthias Braun over 4 yearsThis works great! Would you mind explaining what
y:
does? -
ib. about 4 yearsThis suffers the same problem as the original command in the question. Namely, the Python process in this case is fed the whole lines that the current selection spans, rather than the exact character range selected.
-
Nathan Wallace over 3 yearsI added these two lines to my .vimrc for encoding and decoding:
vnoremap <leader>b y:let @"=system('base64 -w 0', @")<cr>gvP
andvnoremap <leader>B y:let @"=system('base64 --decode', @")<cr>gvP
-
daniel kullmann over 3 years@MatthiasBraun
y
is just the "yank" command, i.e. it copies the currently selected text into the clipboard. Then:let ...
assigns the register"
to the output of the command.