I'm trying to execute base64 --decode
on a piece of text selected in visual mode, but the base64
command seems to be passed the entire line, 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?
Thanks in advance.
Base64 encode/decode visual-selected region in buffer and clipboard, put this in ~/.vimrc, and use F2 to encode selection, and F3 to decode selection
You can use Python instead which should work.
Select lines which you want to decode in visual mode (V), then execute the following command:
If you want to replace the text with the output of
base64
, use something likeIf 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:
It is possible to combine copying the selected text and running the command into a single Visual-mode key mapping:
The mapping can further be modified to replace the selected text with the output of the shell command via the expression register:
Here’s a script that uses Python and the
base64
module to provide base64 decode and encode commands. It’d be pretty simple to support any other base64 program as well, as long as it reads from stdin — just replacepython -m base64 -e
with the encoding command andpython -m base64 -d
with the decoding command.Some features this provides:
Supports ranges, converts only the current line by default (use
:%Base64Encode
to encode the whole file, for example, and it’ll work as expected from within visual mode, although it only converts whole lines)Doesn’t leave output indented — all indents (tabs/spaces) is encoded into base64, and then preserved when decoding.
Supports being combined with other commands with
|
Relevant
:help
tags:user-functions
,func-range
,i_0_CTRL-D
,i_CTRL-R_CTRL-O
,expr-register
,system()
,user-commands
,command-nargs
,command-range
,:normal