运行 来自 vi 的命令 / vim
Run command from within vi / vim
作为学习 Haskell 的一部分,为了好玩,我正在尝试使用 Raspberry PI。在 PI 上安装 ghci 时遇到无数问题,我决定只使用 ghc。
因此要创建、编译 & 运行 一个新的 Haskell 文件:
vi first.hs
i
main = putStrLn "First"
Esc
:w
:q
ghc -o first first.hs
./first
输出为:"First"
我想自动执行命令:
Esc
:w
:q
ghc -o first first.hs
./first
能否将这些添加为 vi / vim 中的新命令,例如:
:我的自定义命令
和 运行 来自 vi / vim 编辑器 ?
绝对在 vim 中,但不一定在其他 vi 风格中。请参阅 this tutorial on defining custom commands. Put the custom command in your vimrc
and it will always be available as :Customcmd
or whatever you call it. For one-button access, you can use :remap
to assign a hotkey to your custom command or the sequence of built-in commands you want to run. This 键映射教程,它将为您提供更多信息。
我将@statox 推荐给 https://vi.stackexchange.com :)
或许您可以尝试将类似这样的内容添加到您的 vimrc
:
function! ExecuteHS()
w
!ghc -o first %
!./first
endfunction
要使用此功能,您只需这样调用它 :call ExecuteHS()
。 Vim 将在您的文件执行期间置于后台,然后在执行结束时返回前台。
作为奖励,您可以将以下行添加到您的 vimrc
nnoremap <key> :call ExecuteHS()<CR>
例如,将 <key>
替换为您喜欢的组合键 <Leader>e
。这样你只需要在正常模式下点击 ,e (如果你没有改变你的主键)来调用函数。
这可能不是最简洁的方法,但它应该可以满足您的需求。
我使用 vim-haskell,其中包含一些不错的东西。特别是,它包含一个用于将 cabal-install 设置为编译器的文件,这是一种非常好的工作方式。将其转储到 ~/.vim/compiler/cabal-build.vim
:
CompilerSet makeprg=cabal\ build
CompilerSet errorformat=
\%W%f:%l:%c:\ Warning:%m,
\%W%f:%l:%c:\ Warning:,
\%E%f:%l:%c:%m,
\%E%f:%l:%c:,
\%C\ \ %#%m,
\%-G%.%#,
\%-G%.%#
这在 ~/.vim/ftplugin/haskell.vim
中:
compiler cabal-build
(compiler
的参数应该与你在~/.vim/compiler
中输入的文件名匹配。)然后你可以在vim中运行 :make
它将保存所有更改的缓冲区(假设设置了 autowrite
)并构建您的项目。当出现错误时,它会填充快速修复列表,让您可以一键跳转到每个错误或警告的特定文件和行号。使用 :help quickfix
阅读有关此功能的更多信息。一旦一切正常,您就可以 :!cabal run
到 运行 它了。
作为学习 Haskell 的一部分,为了好玩,我正在尝试使用 Raspberry PI。在 PI 上安装 ghci 时遇到无数问题,我决定只使用 ghc。
因此要创建、编译 & 运行 一个新的 Haskell 文件:
vi first.hs
i
main = putStrLn "First"
Esc
:w
:q
ghc -o first first.hs
./first
输出为:"First"
我想自动执行命令:
Esc
:w
:q
ghc -o first first.hs
./first
能否将这些添加为 vi / vim 中的新命令,例如:
:我的自定义命令
和 运行 来自 vi / vim 编辑器 ?
绝对在 vim 中,但不一定在其他 vi 风格中。请参阅 this tutorial on defining custom commands. Put the custom command in your vimrc
and it will always be available as :Customcmd
or whatever you call it. For one-button access, you can use :remap
to assign a hotkey to your custom command or the sequence of built-in commands you want to run. This 键映射教程,它将为您提供更多信息。
我将@statox 推荐给 https://vi.stackexchange.com :)
或许您可以尝试将类似这样的内容添加到您的 vimrc
:
function! ExecuteHS()
w
!ghc -o first %
!./first
endfunction
要使用此功能,您只需这样调用它 :call ExecuteHS()
。 Vim 将在您的文件执行期间置于后台,然后在执行结束时返回前台。
作为奖励,您可以将以下行添加到您的 vimrc
nnoremap <key> :call ExecuteHS()<CR>
例如,将 <key>
替换为您喜欢的组合键 <Leader>e
。这样你只需要在正常模式下点击 ,e (如果你没有改变你的主键)来调用函数。
这可能不是最简洁的方法,但它应该可以满足您的需求。
我使用 vim-haskell,其中包含一些不错的东西。特别是,它包含一个用于将 cabal-install 设置为编译器的文件,这是一种非常好的工作方式。将其转储到 ~/.vim/compiler/cabal-build.vim
:
CompilerSet makeprg=cabal\ build
CompilerSet errorformat=
\%W%f:%l:%c:\ Warning:%m,
\%W%f:%l:%c:\ Warning:,
\%E%f:%l:%c:%m,
\%E%f:%l:%c:,
\%C\ \ %#%m,
\%-G%.%#,
\%-G%.%#
这在 ~/.vim/ftplugin/haskell.vim
中:
compiler cabal-build
(compiler
的参数应该与你在~/.vim/compiler
中输入的文件名匹配。)然后你可以在vim中运行 :make
它将保存所有更改的缓冲区(假设设置了 autowrite
)并构建您的项目。当出现错误时,它会填充快速修复列表,让您可以一键跳转到每个错误或警告的特定文件和行号。使用 :help quickfix
阅读有关此功能的更多信息。一旦一切正常,您就可以 :!cabal run
到 运行 它了。