vim映射,用于使用可选输入文件运行c ++ [英] vim mapping for running c++ with optional input file

查看:54
本文介绍了vim映射,用于使用可选输入文件运行c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置vimrc文件,以便使用c ++进行竞争性编程.

I am configuring vimrc file for competitive programming in c++.

autocmd filetype cpp nnoremap <F5> :w <CR> 
                                   :!g++ -o %:r % <CR> :!./%:r < input.txt <CR>

下面的映射是当我按F5键时,它保存,编译(%:r是不带.cpp的文件名),并与input.txt文件一起运行.

The following mapping is when I press F5, it saves, compiles (%:r is filename without .cpp), and runs with input.txt file.

但是,并不是每个cpp文件都具有input.txt,因此,仅在当前目录中存在input.txt时,我才希望通过管道传输input.txt.

However not every cpp file has input.txt, so I want to pipe input.txt only when input.txt exists in the current directory.

总而言之,在映射过程中检查文件的最佳方法是什么?

To sum up, what is the best way of checking files during the mapping?

推荐答案

除非您正在无法使用gnu make或配置不当(mingw)的环境中工作,否则不需要任何 Makefile 用于单文件项目.在这种情况下,它们不是强制性的,并且IMO比其他任何事情都更加麻烦.

Unless you're working in an environment where gnu make is not available, or poorly configured (mingw), you won't need any Makefile for mono-file projects. They are not mandatory in that case, and IMO more of an hassle than anything else.

:make %<

这样,任何错误都会直接转到quickfix窗口.这样可以提高查找错误的效率(:h quickfix ).另外,无论您当前的文件是C,C ++,Fortran ...还是默认gnumake配置识别的任何语言,都不必根据当前文件类型指定要使用的编译器.例如,如果您确实想为C ++选择另一个编译器,则可以使用

This way any errors directly go to the quickfix windows. This will improve your productivity to locate errors (:h quickfix). Also, whether your current file is in C, C++, Fortran... or any language recognized by default gnumake configuration, you don't have to specify the compiler you wish to use depending on the current filetype. If you really want to select another compiler for C++ for instance, you can use

:let $CXX ='clang++'
" $CC for C, and so on

如果您想更改编译选项

:let $CXXFLAGS = '-std=c++17 -Wall -Werror'
" $CFLAGS for C, $LDLIBS, $LDFLAGS for the linker, and so on

还请注意,如果您有 Makefile ,则会自动使用它.

Note also that if you have a Makefile, it'll be automatically used.

:! make%<&&./%& lt; 确实很简单,可以链接两个步骤.las,我们没有直接与:make 等效的东西.我们必须分析快速修复列表,以查看是否存在任何问题

:!make %< && ./%< indeed is simple enough to chain both steps. Alas, we don't have the direct equivalent with :make. We have to analyse the quickfix list to see if there were any issue

如果 filter(getqflist(),'v:val.valid!= 0')不为空,我们可以知道是否已检测到问题.但这并不能说明它们是警告还是错误.我们可以提供以下完整的信息

If filter(getqflist(), 'v:val.valid != 0') is not empty we can know whether problems have been detected. But it doesn't tell whether they are warnings or errors. We can have the complete information with the following

" From my build-tools-wrapper plugin
function! lh#btw#build#_get_metrics() abort
  let qf = getqflist()
  let recognized = filter(qf, 'get(v:val, "valid", 1)')
  " TODO: support other locales, see lh#po#context().tranlate()
  let errors   = filter(copy(recognized), 'v:val.type == "E" || v:val.text =~ "\\v^ *(error|erreur)"')
  let warnings = filter(copy(recognized), 'v:val.type == "W" || v:val.text =~ "\\v^ *(warning|attention)"')
  let res = { 'all': len(qf), 'errors': len(errors), 'warnings': len(warnings) }
  return res
endfunction

由此,我们可以决定仅停止发生错误,或者停止发生错误和警告.

From this we can decide to stop just on errors, or on errors and warnings.

使用 fileizable(),我们可以知道输入文件是否在这里.

With filereadable() we can know whether the input file is here.

因此它变为:

let exec_line = '!./' . expand('%<') " we could also use the complete path instead
let input = expand('%:p:h')/.'input.txt'
if filereadable(input)
    let exec_line .= ' < ' . input
endif
exe exec_line

如果您想在:terminal中重定向结果,那么不幸的是,这次重定向不能与Vim一起使用(尽管它可以与nvim一起使用)

If you want to redirect the result in a :terminal, this time unfortunately, redirection cannot be used with Vim (it works with nvim though)

最终代码(赋予先前的功能以检测错误和警告)变成

The final code (given the previous function to detect errors & warnings) becomes.

function s:build_and_run(file) abort
  let tgt  = fnamemodify(a:file, ':r')
  " to make sure the buffer is saved
  exe 'update ' . a:file
  exe 'make ' . tgt
  if lh#btw#build#_get_metrics().errors
    echom "Error detected, execution aborted"
    copen
    return
  endif

  let path = fnamemodify(a:file, ':p:h')
  let exec_line = '!./' . tgt
  let input = path.'/input.txt'
  if filereadable(input)
    let exec_line .= ' < ' . input
  endif
  exe exec_line
endfunction

nnoremap µ :<C-U>call <sid>build_and_run(expand('%'))<cr>

这篇关于vim映射,用于使用可选输入文件运行c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆