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?
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>