如何执行“base64”--解码“在 Vim 中选择文本?

发布于 2024-12-11 09:38:51 字数 295 浏览 0 评论 0原文

我试图对在可视模式下选择的一段文本执行 base64 --decode ,但它似乎是传递给 base64 命令的整行,不仅仅是当前的选择。

我在可视模式下选择文本,然后进入普通模式,这样我的命令行看起来像这样:

:'<,'>!base64 --decode

How can I pass only the selected block of the line to a shell-command incalling in Vim?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无声情话 2024-12-18 09:38:51

如果要传递给 shell 命令的文本首先被拉到
一个寄存器,例如未命名的寄存器,可以使用以下命令:

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

可以将复制所选文本和运行
命令转换为单个可视模式键映射:

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

可以进一步修改映射以将所选文本替换为
通过表达式寄存器输出 shell 命令:

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

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>
安稳善良 2024-12-18 09:38:51

您可以使用 Python 来代替,它应该可以工作。

选择要在可视模式下解码的行(通过V),然后执行以下命令:

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

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
嘿嘿嘿 2024-12-18 09:38:51

如果您想将文本替换为 的输出base64,使用类似

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

解释:

  • y → 提取当前选定的文本以注册 "。这会取消选择文本。
  • :let @"=system('base64 --decode', @") → 将"寄存器的内容传递给base64并将结果写入相同的内容注册 "
  • gv → 再次选择之前选择的文本。
  • P → 粘贴寄存器 " 中的文本,将其替换当前选定的文本。

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

Explanation:

  • y → Yank the currently selected text to register ". This deselects the text.
  • :let @"=system('base64 --decode', @") → Pass the contents of the " register to base64 and write the results into the same register ".
  • gv → Select the previously selected text again.
  • P → Paste the text from register " which replaces the currently selected text.
栀子花开つ 2024-12-18 09:38:51

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> 

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> 
昇り龍 2024-12-18 09:38:51

下面是一个使用 Python 和 base64 模块提供 base64 解码和编码命令的脚本。支持任何其他 base64 程序也非常简单,只要它从 stdin 读取即可 - 只需将 python -m base64 -e 替换为编码命令和 python -m base64 -d 带有解码命令。

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标签:<代码>用户函数、func-rangei_0_CTRL-Di_CTRL-R_CTRL-Oexpr-注册, system()用户命令command-nargs命令范围:正常< /代码>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文