5

如何在 Vim 中使用外部命令的输出

 2 years ago
source link: https://hanleylee.com/articles/how-vim-use-output-of-external-command/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在 vim 中我们可以用添加前缀 ! 的方式执行外部命令, 例如 !ls, 其结果将被在底部输出

WaMa

那么我们如果想使用外部命令的结果, 该怎么做呢?

使用 :read

:read 可以读取命令执行结果到当前 buffer 中, 如果我们想插入外部命令的结果, 那么使用 :read !ls 即可

WaMa

使用 system()

如果我们不想将一个命令的执行结果插入当前 buffer 中, 有没有办法可以保存在变量中呢? 也是有的, vim 提供了函数 system() 用来获得输出外部命令的结果, 如果你使用过 shell 的话就会知道, 这种使用方式就像使用 $(command)`command` 一样. 在脚本中我们使用 let var = system("command") 的形式了获得输出结果

在终端中我们可以通过 $? 来得到上一个命令的执行结果, 根据此结果来判断是否继续执行还是终止程序. 在 vim 中我们也可以按照相同的思路去思考, 使用预设变量 v:shell_error 可以获得最近一个外部命令的执行结果

如下是一个综合使用 system()v:shell_error 的例子

" Swap between target path and source path
function! hl#chezmoi#swap_between_target_and_source()
    let current_path = expand('%:p')
    if current_path =~# '.local/share/chezmoi/' " current path is located in ~/.local/share/chezmoi/, now we are inside the source path
        let target_path = s:get_target_file(current_path)
        exec 'edit ' . target_path
    else " now we are in the target path, so we should check this file have corresponding source file or not
        let target_path = system('chezmoi source-path ' . current_path)
        if v:shell_error != 0
            echom 'Current file is not managed by chezmoi!!!'
        else
            exec 'edit ' . target_path
        endif
    endif
endfunction

Redir

我们可以定义一个方法用来捕获 vim 内部命令或外部命令的输出, 将捕获的结果输出到一个新窗口中

"`:Redir` followed by either shell or vim command
command! -nargs=+ -complete=command Redir silent call Redir(<q-args>)

" Redirect output to a single window
function! Redir(cmd)
    for win in range(1, winnr('$'))
        if getwinvar(win, 'scratch')
            execute win . 'windo close'
        endif
    endfor
    if a:cmd =~ '^!'
        let output = system(matchstr(a:cmd, '^!\zs.*'))
    else
        redir => output
        execute a:cmd
        redir END
    endif
    botright vnew
    let w:scratch = 1
    setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
    call setline(1, split(output, "\n"))
endfunction

GetOutput

同样, 我们可以仅仅将结果返回, 这在脚本中对变量赋值非常有用, 更加灵活, 使用方法为 let var1 = GetOutput("!python --version")

" Get output of a command
function! GetOutput(cmd)
    if a:cmd =~ '^!'
        let output = system(matchstr(a:cmd, '^!\zs.*'))
    else
        redir => output
        silent execute a:cmd
        redir END
    endif
    let output = substitute(output, '[\x0]', '', 'g')
    return output
endfunction

Reference


Recommend

  • 59
    • studygolang.com 6 years ago
    • Cache

    golang os/exec 执行外部命令

    exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (string, error) //LookPath在环境变量中查找科执行二进制文件,如果file中包含一个斜杠...

  • 23

    golang调用外部命令,并且通过stdin传数据的例子 使用场景: 当我们需要调用一个外部命令,然后给外部命令传参数,常用方便的做法是通过命令行传参数,但是有些时候数据太长,或者基于安全考虑,比如传密码,等不方便使...

  • 7
    • lindexi.gitee.io 4 years ago
    • Cache

    VisualStudio 自定义外部命令

    VisualStudio 自定义外部命令通过自定义命令,可以在 VisualStudio 加上一些自定义命令,可以快速启动 git 或者做其他的事情

  • 7
    • www.lujun9972.win 3 years ago
    • Cache

    如何在org2blog中插入外部URL视频

    如何在org2blog中插入外部URL视频 通过查看 WordPress 的HTML源码,可以看到视频是通过 video tag来实现的,它的格式差不多是这样的 <video class="wp-video-shortcode" id="video36-1" width="640" height="360" p...

  • 8
    • www.lujun9972.win 3 years ago
    • Cache

    使用logsave将命令输出保存起来

    使用logsave将命令输出保存起来 如果想要把命令的输出保存到文件中,你会怎么做? 一个常用的方法就是使用IO重定向吧 ls >/tmp/ls.txt 2>&1 如果需要在将命令输出保存到文件中的同时还需要将...

  • 5
    • darjun.github.io 2 years ago
    • Cache

    Go中调用外部命令的几种姿势

    Go中调用外部命令的几种姿势 2022-11-01 Go

  • 2

    如何根据内存使用情况对 top 命令的输出结果排序 作者:刘光录 2022-11-11 14:48:41 top 命令的输出是根据 CPU 的使用情况进行排序的。也就是说,消耗 CPU 越大的进程会被排在顶部。

  • 2
    • sunqi.site 2 years ago
    • Cache

    在Go语言中执行外部命令

    在Go语言中执行外部命令 2023-01-05  约 1753 字   预计阅读 4 分钟    次阅读  执行外部命令是开发中常见的需求,本节重点介绍如何在Go语言中执行外部命令,并且合理的进行控制。Go中执行命令主要是通过os/exec...

  • 4

    在本文中,我将向大家介绍如何在Windows平台上使用C++执行外部命令。我们将探讨两种不同的方法,并对它们进行比较和描述。当我们需要在程序中集成其他应用程序或运行脚本时,这两种方法都非常有用。 在详细讲解这两种方法之前,让我们先了解为什么我们需要在C...

  • 7
    • www.flydean.com 1 year ago
    • Cache

    如何在langchain中使用外部数据

    在大型语言模型(LLM)领域,越来越多的应用需要生成用户特定的数据,这些数据通常超出了模型的训练数据范围。 因为大模型在训练的过程中,因为受到训练数据的影响,可能并没没有用户所需要的数据,所以可能会导致大语言模型在某些特定数据上的行为缺失。...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK