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

后端 未结 5 2032
北恋
北恋 2021-02-06 23:58

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 comma

相关标签:
5条回答
  • 2021-02-07 00:34

    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 replace python -m base64 -e with the encoding command and python -m base64 -d with the decoding command.

    function! Base64Encode() range
        " go to first line, last line, delete into @b, insert text
        " note the substitute() call to join the b64 into one line
        " this lets `:Base64Encode | Base64Decode` work without modifying the text
        " at all, regardless of line length -- although that particular command is
        " useless, lossless editing is a plus
        exe "normal! " . a:firstline . "GV" . a:lastline . "G"
        \ . "\"bdO0\<C-d>\<C-r>\<C-o>"
        \ . "=substitute(system('python -m base64 -e', @b), "
        \ . "'\\n', '', 'g')\<CR>\<ESC>"
    endfunction
    
    function! Base64Decode() range
        let l:join = "\"bc"
        if a:firstline != a:lastline
            " gJ exits vis mode so we need a cc to change two lines
            let l:join = "gJ" . l:join . "c"
        endif
        exe "normal! " . a:firstline . "GV" . a:lastline . "G" . l:join
        \ . "0\<C-d>\<C-r>\<C-o>"
        \ . "=system('python -m base64 -d', @b)\<CR>\<BS>\<ESC>"
    endfunction
    
    command! -nargs=0 -range -bar Base64Encode <line1>,<line2>call Base64Encode()
    command! -nargs=0 -range -bar Base64Decode <line1>,<line2>call Base64Decode()
    

    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

    0 讨论(0)
  • 2021-02-07 00:37

    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>
    
    0 讨论(0)
  • 2021-02-07 00:46

    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
    
    0 讨论(0)
  • 2021-02-07 00:48

    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
    
    0 讨论(0)
  • 2021-02-07 00:55

    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

    " 1. base64-encode(visual-selection) -> F2 -> encoded base64-string
    :vnoremap <F2> c<c-r>=system("base64 -w 0", @")<cr><esc>
    
    " 2. base64-decode(visual-selection) -> F3 -> decoded string
    :vnoremap <F3> c<c-r>=system("base64 -d", @")<cr> 
    
    0 讨论(0)
提交回复
热议问题