Vim 新增程序执行窗口,类似于cwindow

发布于 2024-10-25 00:32:36 字数 344 浏览 4 评论 0原文

我正在使用 Vim 编辑/编译各个 cpp 文件 (makepgr=g++\ -g\ %),并希望最好使用 这样的快捷键来执行生成的二进制文件F6。然后,所需的行为是打开一个类似于 :cwindow 的新窗口,具有固定的最大高度(例如 20 行),其中包含程序输出。我还希望能够使用另一个快捷键(例如 F7)再次关闭此窗口。

作为奖励,如果程序的执行时间显示在窗口标题/状态中那就太好了。

编辑:我正在谈论的二进制文件是非交互式的!他们主要读取一些输入文件,进行数据操作,将结果写入其他文件,并在这样做时产生一些输出。

I am using Vim to edit/compile individual cpp files (makepgr=g++\ -g\ %) and want to execute the resulting binaries ideally using a shortkey like F6. The desired behavior is then, to open a new window similar to :cwindow with a fixed maximal height (say 20 lines), which contains the program output. I also want to be able to close this window again with another shortkey like F7.

As a bonus, it would be great to have the execution time of the program shown in the windows title/status.

EDIT: the binaries I am talking about are non-interactive! They mostly read some input files, do data manipulation, write their results to other files and have some output while doing so.

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

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

发布评论

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

评论(4

温柔一刀 2024-11-01 00:32:36

这个小脚本应该可以解决问题:

let s:title="ProgramOutput"

function! Open_output_window()
    let winnr = bufwinnr(s:title)
    if winnr != -1
        exe winnr . 'wincmd w'
        return
    endif

    let bufnr = bufnr(s:title)
    if(bufnr == -1)
        let wcmd = s:title
    else
        let wcmd = '+buffer' . bufnr
    endif

    exe 'silent! botright vertical 80 split ' .wcmd

    silent! setlocal buftype=nofile
    silent! setlocal noswapfile
    silent! setlocal nobuflisted
endfunction

function! Close_output_window()
    let winnr = bufwinnr(s:title)
    if winnr != -1
        exe winnr . 'wincmd w'
        close
    endif
endfunction

nmap <F6> <ESC>:call Open_output_window()<CR>:r ! %:r.exe<CR>
nmap <F5> <ESC>:call Close_output_window()<CR>

大部分代码来自 taglist.vim 插件 - 它相当容易理解。
您可以根据您的窗口大小首选项修改 split 命令。

This little script should do the trick:

let s:title="ProgramOutput"

function! Open_output_window()
    let winnr = bufwinnr(s:title)
    if winnr != -1
        exe winnr . 'wincmd w'
        return
    endif

    let bufnr = bufnr(s:title)
    if(bufnr == -1)
        let wcmd = s:title
    else
        let wcmd = '+buffer' . bufnr
    endif

    exe 'silent! botright vertical 80 split ' .wcmd

    silent! setlocal buftype=nofile
    silent! setlocal noswapfile
    silent! setlocal nobuflisted
endfunction

function! Close_output_window()
    let winnr = bufwinnr(s:title)
    if winnr != -1
        exe winnr . 'wincmd w'
        close
    endif
endfunction

nmap <F6> <ESC>:call Open_output_window()<CR>:r ! %:r.exe<CR>
nmap <F5> <ESC>:call Close_output_window()<CR>

Most of this code came from the taglist.vim plugin - it's reasonably simple to understand.
You can modify the split command for your window size preferences.

知足的幸福 2024-11-01 00:32:36

我不确定这是否有任何帮助,因为你问的是 vim:

我首先想到如何解决这个问题是使用 GNU screen。原因不是,vim 无法做到这一点,而是 vim 不能完全用作终端仿真器,因此运行一些交互式程序不起作用。 Plus 屏幕相对容易配置。

我当然会为该任务编写一个专门的屏幕配置。

I'm not sure if this is helpful in any way, since you're asking about vim:

My first thought about how to solve this problem would be usage of GNU screen. Reason is not, that vim isn't capable of doing this, but that vim isn't exactly usable as terminal emulator, so running some interactive programs doesn't work. Plus screen is relatively easy to configure.

I would of course write a specialized screen config for that task.

萌辣 2024-11-01 00:32:36

添加到 vimrc:

 map <F6> :tabnew<CR>:.!ls -la<CR>

打开 vim 并按 F6。您应该在新选项卡中看到命令 ls -la 的输出。现在,如果这对您有用,那么您可以更改它(将 myprog 替换为可执行文件的名称):

 map <F6> :tabnew<CR>:.!./myprog<CR>

您甚至可以为此选项卡命名:

 map <F6> :tabnew myprog<CR>:.!./myprog<CR>

不喜欢选项卡?将 tabnew 替换为 splitvsplit ,这样可以水平或垂直分割窗口。想要分割窗口小吗?将

 map <F6> :5split myprog<CR>:.!./myprog<CR>

5 替换为您喜欢的任何其他数字。还添加了

 map <F7> :tabclose<CR>

F7 关闭选项卡的功能。

PS 您可以将硬编码的 myprog 名称替换为 expand("%:r").".exe" (对于 Windows)或只是 expand("% :r") (对于 *nix)正如已经说过的。

Add to the vimrc:

 map <F6> :tabnew<CR>:.!ls -la<CR>

Open vim and press F6. You should see in the new tab with the output of the command ls -la. Now, if this works for you then you can change it (replace myprog with the name of your executable):

 map <F6> :tabnew<CR>:.!./myprog<CR>

You can even give name to this tab:

 map <F6> :tabnew myprog<CR>:.!./myprog<CR>

Do not like tabs? Replace tabnew with split or vsplit wich allows to split window horizontally or vertically. Want splitted window to be small? Use

 map <F6> :5split myprog<CR>:.!./myprog<CR>

Replace 5 with any other number you likes. Also add

 map <F7> :tabclose<CR>

to get ability to close the tab by pressing F7.

P.S. You can replace hard-coded myprog name with expand("%:r").".exe" (for Windows) or just expand("%:r") (for *nix) as already been said.

青衫儰鉨ミ守葔 2024-11-01 00:32:36

您可以将输入重定向到快速修复窗口(类似于 :make 的工作方式):
:cex system(expand("%:r").".exe")

您可以使用 :colder:cnewer 浏览快速修复历史记录>

我意识到这并不完全是您想要的,但希望它足够接近有用。

You could redirect the input into the quickfix window (similar to how :make works):
:cex system(expand("%:r").".exe")

You can navigate through quickfix history using :colder and :cnewer

I realise this isn't exactly what you were looking for, but hopefully it's near enough to be useful.

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