2011-10-21 36 views
13

我試圖在可視模式中選中的一段文本上執行'base64 --decode',但base64命令似乎是通過了整行,而不是隻是我做的選擇。對vim中的選擇執行'base64 --decode'

我選擇在視覺模式下的文本,然後進入正常模式,這樣我的命令行看起來是這樣的:

:'<,'>!base64 --decode 

我應該如何只對選定的一塊線路的傳遞爲base64 --decode ?

預先感謝

+0

你想替換選定的文本? –

+0

更換將是美好的,純輸出到控制檯會確定爲好。 – Jonatan

回答

15

如果文本傳遞給外殼命令猛拉到寄存器(例如, 到無名寄存器),可以使用下面的命令。

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

這是可能選擇的文本並使用可視模式映射在一個步驟中運行命令 的拷貝結合。

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

的映射可以被修改,以替換使用表達式寄存器的 外殼命令的輸出選定的文本。

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

是可能的第一示例回聲到一個新的VIM標籤? – shredding

+0

或者我會如何製作一個宏呢? – shredding

+1

@shredding:只需添加一個命令複製之間的新標籤頁打開一個空的緩衝區,並呼籲'base64':':vnoremap 64歲:多部未華\ | PU =系統( '的base64 -d',@@)! '。 –

4

如果要替換的base64輸出文本,使用類似

:vnoremap <leader>64 y:let @"=system('base64 --decode', @")<cr>gvP 
5

您可以使用Python,而不是它應該工作。

要在視覺模式(V)解碼選擇線,然後執行下面的命令:

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

的Base64編碼/在緩衝液和剪貼板解碼視覺選擇的區域, 把這個在的〜/ .vimrc,並用F2來編碼選擇和F3解碼選擇

" 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

下面是一個使用Python和base64模塊提供的base64解碼和ENC腳本ode命令。支持其他base64程序也很簡單,只要它從標準輸入讀取 - 只需用編碼命令替換python -m base64 -e,然後用解碼命令替換python -m base64 -d

  • 支持範圍,只轉換在默認情況下(使用:%Base64Encode編碼整個文件,例如當前行,並從內預計它會工作:

    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() 
    

    這提供了一些功能可視模式,儘管它只能轉換整行)

  • 不保留輸出縮進 - 所有縮進(製表符/空格)都編碼到base64中,然後在解碼時保留。

  • 支持正在結合其他命令與|

相關:help標籤:user-functionsfunc-rangei_0_CTRL-Di_CTRL-R_CTRL-Oexpr-registersystem()user-commandscommand-nargscommand-range:normal