Executing 'base64 --decode' on a selection in vim

牧云@^-^@ 提交于 2019-12-20 17:05:44

问题


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 selection I made.

I'm selecting the text in visual mode, then entering normal mode so that my command line looks like this:

:'<,'>!base64 --decode

How should I pass only the selected piece of the line to base64 --decode?

Thanks in advance


回答1:


If the text to pass to the shell command is yanked to a register (for example, to the unnamed register), one can use the following command.

:echo system('base64 --decode', @")

It is possible to combine copying of the selected text and running the command in one step using a Visual mode mapping.

:vnoremap <leader>64 y:echo system('base64 --decode', @")<cr>

The mapping can be modified to replace selected text with the output of the shell command using expression register.

:vnoremap <leader>64 c<c-r>=system('base64 --decode', @")<cr><esc>



回答2:


You can use Python instead which should work.

Select lines which you want to decode in visual mode (V), then execute the following command:

:'<,'>!python -m base64 -d



回答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



回答4:


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> 



回答5:


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



来源:https://stackoverflow.com/questions/7845671/executing-base64-decode-on-a-selection-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!