Basically what I want is a keyboard shortcut in vim that lets me [compile and] run the currently being edited C, C++ or Python program. In psuedocode:
when a shortcut key is pressed:
if current_extension == 'c' then
shell: gcc this_filename.c -o this_filename_without_extension
if retcode == 0 then shell: ./this_filename_without_extension
else if current_extension == 'cpp' then
shell: g++ this_filename.cpp -o this_filename_without_extension
if retcode == 0 then shell: ./this_filename_without_extension
else if current_extension == 'py' then
shell: python this_filename.py
end if
end key
I realise I might be asking a bit much but would love it if this was possible!
Something like this would work. Just create filetype autocmd that map
<F4>
or whatever you want to save and compile and run the program. It uses exec to build the string and uses shellescape to escape the file name.%
is the current buffer filename.%:r
is the buffer filename without extensionthis is my map it really work you can update it to any language:
now if you are ine one of those file in vim just do in normal mode:
http://singlecompile.topbug.net seems to do more than what you want. For a simpler solution you could also just add the following to your vimrc
HTH
It's 2018 now, vim 8 has released for 2 years and shipped with all the mean stream Linux distributions and Mac OS X. But a lot of vim tutorials are still teaching people something ten years ago.
You can compile your C++/Java programs in vim as convenience as Sublime Text or NotePad++ with some dedicated plugins for Vim 8 or NeoVim.
For example, the AsyncRun plugin will allow you run shell commands in background and read output from quickfix window in realtime. See the screen capture.
Just like compiling programs in IDEs, the compilation errors will be matched by errorformat and be highlighted and become selectable. You can navigate errors in the quickfix window or continue editing while compiling.
Quick setup
Copy & paste the lines below to your vimrc:
When you input “:AsyncRun echo hello ” in the command line:
see the capture here
You will see the realtime command output in the open quickfix window.
Compile and run a single file
Compiling a single file with AsyncRun is much simpler than Sublime Text’s build system. We can setup F9 for this:
The macros in
$(..)
form will be expanded as the real file name or directory, and then we will have F5 to run the executable:The double quotation mark is used to handle path names containing spaces. The option
-cwd=$(VIM_FILEDIR)
means running the file in the file's directory. The absolute path name$(VIM_FILEDIR)/$(VIM_FILENOEXT)
is used because linux needs a./
prefix to running executables in current directory, but windows doesn't . Using the absolute path name of the binary file can handle this crossing platform issue.Another option
-raw
means the output will not be matched by vim's errorformat, and will be displayed in quickfix as what it is. Now you can compile your file with F9, check the compilation errors in quickfix window and press F5 to run the binary.Build C/C++ Projects
No matter what build tool you are using, make or cmake, project building means acting to a group of files. It requires locating the project root directory. AsyncRun uses a simple method called root markers to identify the project root. The Project Root is identified as the nearest ancestor directory of the current file which contains one of these directories or files:
If none of the parent directories contains these root markers, the directory of the current file is used as the project root. This enables us to use either
<root>
or$(VIM_ROOT)
to represent the project root. and F7 can be setup to build the current project:What if your current project is not in any git or subversion repository ? How to find out where is my project root ? The solution is very simple, just put an empty .root file in your project root, it will be located easily.
Let’s move on, setup F8 to run the current project:
The project will run in its root directory. Of course, you need define the run rule in your own makefile. then remap F6 to test:
If you are using cmake, F4 can be map to update your Makefile:
Due to the implementation of c runtime, if the process is running is a non-tty environment, all the data in stdout will be buffered until process exits. So, there must be a
fflush(stdout)
after yourprintf
statement if you want to see the real-time output. or you can close the stdout buffer at the beginning byAt the mean time, if you are writing C++ code, a std::endl can be appended to the end of std::cout. It can force flush the stdout buffer. If you are developing on windows, AsyncRun can open a new cmd window for the child process:
Using the option
-mode=4
on windows will open a new prompt window to run the command, just like running command line programs in Visual Studio. Finally, we have these key mappings below:It is more like build system in NotePad++ and GEdit. If you are using cmake heavily, you can write a simple shell script located in
~/.vim/script/build.sh
to combine F4 and F7 together: it will update Makefile if CMakeList.txt has been changed, then exectute make.Advanced usage
You can also define shell scripts in your dotfiles repository and execute the script with F3:
The following shell environment variables are defined by AsyncRun:
All the above environment variables can be used in your
build_advanced.sh
. Using the external shell script file can do more complex work then a single command.Grep symbols
Sometimes, If you don't have a well setup environment in you remote linux box, grep is the most cheap way to search symbol definition and references among sources. Now we will have F2 to search keyword under cursor:
The above script will run grep or findstr in your project root directory, and find symbols in only
.c
,.cpp
and.h
files. Now we move around the cursor and press F2, the symbol references in current project will be displayed in the quickfix window immediately.This simple keymap is enough for most time. And you can improve this script to support more file types or other grep tools in your vimrc .
That’s the practical way to build/run C/C++ projects in Vim 8 or NeoVim. Just like Sublime Text’s build system and NotePad++’s NppExec.
No more outdated vim tutorials again, try something new.