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

11,143

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
Share:
11,143

Related videos on Youtube

Jonatan
Author by

Jonatan

Embedded Linux software engineer. Open source fan, wannabe electronics hobbyist.

Updated on June 13, 2022

Comments

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

    • daniel kullmann
      daniel kullmann over 12 years
      Do you want to replace the selected text?
    • Jonatan
      Jonatan over 12 years
      Replacing would be OK, plain output to the console would be OK as well.
  • shredding
    shredding about 11 years
    It is possible to echo the first example into a new vim tab?
  • shredding
    shredding about 11 years
    Or how would I make a macro out if it?
  • ib.
    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
    Jonathan Dumaine over 10 years
    Bonus points for a function that toggles between base64 and plaintext via the same keybind?
  • mosh
    mosh about 6 years
    If 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
    Matthias Braun over 4 years
    This works great! Would you mind explaining what y: does?
  • ib.
    ib. about 4 years
    This 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
    Nathan Wallace over 3 years
    I added these two lines to my .vimrc for encoding and decoding: vnoremap <leader>b y:let @"=system('base64 -w 0', @")<cr>gvP and vnoremap <leader>B y:let @"=system('base64 --decode', @")<cr>gvP
  • daniel kullmann
    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.