2011-10-21 16 views
13

私はvisualモードで選択されたテキストに 'base64 --decode'を実行しようとしていますが、base64コマンドは行全体を渡すように見えます私が作った選択だけ。vimの選択で 'base64 --decode'を実行

私は私のコマンドラインは次のようになりますように、そして、通常モードに入る、ビジュアルモードでテキストを選択しています:

:'<,'>!base64 --decode 

私はBASE64にラインの唯一の選択された曲を渡す必要がありますどのように--decode ?予め

+0

あなたがしたいですか選択したテキストを置き換えますか? –

+0

置き換えても問題ありません。コンソールへの普通の出力もOKです。 – Jonatan

答えて

15

おかげシェルコマンドに渡すテキストは(無名レジスタに例えば、 )レジスタにヤンクされている場合、一方が次のコマンドを使用することができます。

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

ビジュアルモードのマッピングを使用して1つのステップで選択したテキストやコマンド を実行しているのコピーを組み合わせることが可能です。

: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 y:tabe \ | pu!= system( 'base64 -d'、@@) '。 –

4

あなたはあなたが働くべき代わりのPythonを使用することができます

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

のようなものを使用し、base64の出力でテキストを置換したい場合。

次のコマンドを実行し、ビジュアルモード(V)でデコードしたいセレクトライン:

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

のBase64エンコードは/バッファとクリップボードにビジュアル選択した領域を復号化し、 でこれを置きます〜/ .vimrcと、選択をエンコードするためにF2を使用して、選択

" 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

をデコードするためのF3は、ここでbase64でデコードおよびENCを提供するために、Pythonのスクリプトを使用してbase64モジュールですodeコマンド。 stdinから読み込むのであれば、他のbase64プログラムもサポートするのは簡単でしょう。python -m base64 -eをencodingコマンドで置き換え、python -m base64 -dをdecodingコマンドで置き換えてください。

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

一部の機能これが用意されています

  • が内から予想されるように、たとえば、ファイル全体をエンコードするために:%Base64Encodeを使用し、それがうまくいく(デフォルトでは現在の行だけを変換し、範囲をサポートすべてのインデント(タブ/スペース)は、base64にエンコードされ、デコード時に保存されます。

  • |

関連:helpタグを有する他のコマンドと組み合わせをサポートしている:user-functionsfunc-rangei_0_CTRL-Di_CTRL-R_CTRL-Oexpr-registersystem()user-commandscommand-nargscommand-range:normal

関連する問題