Remove a section from ch19

pull/62/head
Igor Irianto 3 years ago
parent 6ef201b4e3
commit 2440c510d9

@ -1,6 +1,6 @@
# Ch19. Compile
Compiling is an important subject for many languages. In this chapter, you will learn how to compile from Vim. In addition, you will look at ways to take advantage of Vim's `:make` command.
Compiling is an important subject for many languages. In this chapter, you will learn how to compile from Vim. You will also look at ways to take advantage of Vim's `:make` command.
## Compile From the Command Line
@ -12,63 +12,11 @@ You can use the bang operator (`!`) to compile. If you need to compile your `.cp
However, having to manually type the filename and the output filename each time is error-prone and tedious. A makefile is the way to go.
## Makefile
In this section, I will briefly go over makefile basics. If you already know how to use a makefile, feel free to jump to the next section. In the current directory, create a file named `makefile`. Put this inside:
```
all:
echo "Hello all"
```
Run the `make` command from the terminal:
```
make
```
You will see:
```
echo "Hello all"
Hello all
```
The terminal outputs the echo command itself and its output. You can have multiple "targets" in a makefile. Let's add a few:
```
all:
echo "Hello all"
foo:
echo "Hello foo"
list_pls:
ls
```
Now you can run the `make` command with different targets:
```
make foo
## returns "Hello foo"
make list_pls
## returns the ls command
```
The `make` command outputs the actual command in addition to the output. To suppress the output of the actual command, append `@` before that command, for example:
```
all:
@echo "Hello all"
```
Now when you run `make`, you only see "Hello all" and not `echo "Hello all"`.
## `:make`
## The Make Command
Vim has a `:make` command to run a makefile. When you run it, Vim looks for a makefile in the current directory to execute.
If you haven't already, create a file named `makefile` in the current directory and put these inside:
Create a file named `makefile` in the current directory and put these inside:
```
all:
@ -85,27 +33,29 @@ Run this from Vim:
:make
```
Vim executes it the same way as when you're running it from the terminal. The `:make` command accepts parameter just like the terminal `make` command. Run:
Vim executes it the same way as when you're running it from the terminal. The `:make` command accepts parameter just like the terminal make command. Run:
```
:make foo
" Outputs "Hello foo"
:make list_pls
" Outputs the ls command result
```
The `:make` command uses Vim's `quickfix` to store any error if you run a bad command. Let's run a nonexisting target:
The `:make` command uses Vim's quickfix to store any error if you run a bad command. Let's run a nonexisting target:
```
:make dontexist
```
You should see an error running that command. To view that error, run the `quickfix` command `:copen` to view the ``quickfix`` window:
You should see an error running that command. To view that error, run the quickfix command `:copen` to view the quickfix window:
```
|| make: *** No rule to make target `dontexist'. Stop.
```
## Compile With `make`
## Compiling With Make
Let's use the makefile to compile a basic `.cpp` program. First, let's create a `hello.cpp` file:
@ -118,7 +68,7 @@ int main() {
}
```
Update your `makefile` to build and run a `.cpp` file:
Update your makefile to build and run a `.cpp` file:
```
all:
@ -143,7 +93,7 @@ The `g++` compiles `./hello.cpp` and creates `./hello`. Then run:
You should see `"Hello!"` printed on the terminal.
## `makeprg`
## Different Make Program
When you run `:make`, Vim actually runs whatever command that is set under the `makeprg` option. If you run `:set makeprg?`, you'll see:
@ -151,7 +101,7 @@ When you run `:make`, Vim actually runs whatever command that is set under the `
makeprg=make
```
The default `:make` command is the `make` external command. To change the `:make` command to execute `g++ <your-file-name>` each time you run it, run:
The default `:make` command is the `make` external command. To change the `:make` command to execute `g++ {your-file-name}` each time you run it, run:
```
:set makeprg=g++\\ %
@ -159,28 +109,27 @@ The default `:make` command is the `make` external command. To change the `:make
The `\\` is to escape the space after `g++` (you need to escape the escape). The `%` symbol in Vim represents the current file. The command `g++\\ %` is equivalent to running `g++ hello.cpp`.
Go to `./hello.cpp` then run `:make`. Vim compiles `hello.cpp` and creates `a.out` because you didn't specify the output. Let's refactor it so it will name the compiled output with the name of the original file minus the extension. Run:
Go to `./hello.cpp` then run `:make`. Vim compiles `hello.cpp` and creates `a.out` because you didn't specify the output. Let's refactor it so it will name the compiled output with the name of the original file minus the extension. Run or add this to vimrc:
```
:set makeprg=g++\\ %\\ -o\\ %<
set makeprg=g++\\ %\\ -o\\ %<
```
The breakdown:
- `g++\\ %` is the same as above. It is equivalent to running `g++ <your-file>`.
- `-o` is the output option.
- `%<` in Vim represents the current file name without an extension (`hello.cpp` becomes `hello`).
When you run `:make` from inside `./hello.cpp`, it is compiled into `./hello`. To quickly execute `./hello` from inside `./hello.cpp`, run `:!./%<`. Again, this is the same as running `:!./<current-file-name-minus-the-extension>`.
When you run `:make` from inside `./hello.cpp`, it is compiled into `./hello`. To quickly execute `./hello` from inside `./hello.cpp`, run `:!./%<`. Again, this is the same as running `:!./{current-file-name-minus-the-extension}`.
For more, check out `:h :compiler` and `:h write-compiler-plugin`.
## Auto-Compile On Save
## Auto-compile On Save
You can further make life easier by automating compilation. Recall that you can use Vim's `autocommand` to automate actions based on certain events. To automatically compile `.cpp` files on each save:
You can make life even easier by automating compilation. Recall that you can use Vim's `autocmd` to trigger automatic actions based on certain events. To automatically compile `.cpp` files on each save add this on your vimrc:
```
:autocmd BufWritePost *.cpp make
autocmd BufWritePost *.cpp make
```
Each time you save inside a `.cpp` file, Vim executes the `make` command.
@ -190,12 +139,12 @@ Each time you save inside a `.cpp` file, Vim executes the `make` command.
Vim has a `:compiler` command to quickly switch compilers. Your Vim build probably comes with several pre-built compiler configurations. To check what compilers you have, run:
```
:e $VIMRUNTIME/compilers/<tab>
:e $VIMRUNTIME/compilers/<Tab>
```
You should see a list of compilers for different programming languages.
To use the `:compiler` command, suppose you have a ruby file, `hello.rb`, and inside it has:
To use the `:compiler` command, suppose you have a ruby file, `hello.rb` and inside it has:
```
puts "Hello ruby"
@ -207,7 +156,7 @@ Recall that if you run `:make`, Vim executes whatever command is assigned to `ma
:compiler ruby
```
Vim runs the `$VIMRUNTIME/compiler/ruby.vim` script and changes the `makeprg` to use the `ruby` command. Now if you run `:set makeprg?`, it should say `makeprg=ruby` (this depends on what is inside your `$VIMRUNTIME/compiler/ruby.vim` file or if you have another custom ruby compilers. Yours might be different). The `:compiler <your-lang>` command allows you to switch to different compilers quickly. This is useful if your project uses multiple languages.
Vim runs the `$VIMRUNTIME/compiler/ruby.vim` script and changes the `makeprg` to use the `ruby` command. Now if you run `:set makeprg?`, it should say `makeprg=ruby` (this depends on what is inside your `$VIMRUNTIME/compiler/ruby.vim` file or if you have another custom ruby compilers. Yours might be different). The `:compiler {your-lang}` command allows you to switch to different compilers quickly. This is useful if your project uses multiple languages.
You don't have to use the `:compiler` and `makeprg` to compile a program. You can run a test script, lint a file, send a signal, or anything you want.
@ -286,7 +235,7 @@ Luckily there are plugins to run async processes. The two big ones are:
- [vim-dispatch](https://github.com/tpope/vim-dispatch)
- [asyncrun.vim](https://github.com/skywind3000/asyncrun.vim)
In this chapter, I will go over vim-dispatch, but I would strongly encourage you to try all of them out there.
In the remaining of this chapter, I will go over vim-dispatch, but I would strongly encourage you to try all of them out there.
*Vim and NeoVim actually supports async jobs, but they are beyond the scope of this chapter. If you're curious, check out `:h job-channel-overview.txt`.*
@ -294,7 +243,7 @@ In this chapter, I will go over vim-dispatch, but I would strongly encourage you
Vim-dispatch has several commands, but the two main ones are `:Make` and `:Dispatch` commands.
## `:Make`
### Async Make
Vim-dispatch's `:Make` command is similar to Vim's `:make`, but it runs asynchronously. If you are in a Javascript project and you need to run `npm t`, you might attempt to set your makeprg to be:
@ -308,35 +257,35 @@ If you run:
:make
```
Vim will execute `npm t`. Meanwhile, you are just staring at the frozen screen while your Javascript test runs. With vim-dispatch, you can just run:
Vim will execute `npm t`, but you will be staring at the frozen screen while your JavaScript test runs. With vim-dispatch, you can just run:
```
:Make
```
Vim will run `npm t` asynchronously. This way, while `npm t` is running on a background process, you can edit your text with Vim. Neat!
Vim will run `npm t` asynchronously. This way, while `npm t` is running on a background process, you can continue doing whatever you were doing. Awesome!
## `:Dispatch`
### Async Dispatch
The `:Dispatch` command works like the `:compiler` and the `:!` command.
The `:Dispatch` command is like the `:compiler` and the `:!` command. It can run any external command asynchronously in Vim.
Assume that you are inside a ruby spec file and you need to run a test. Run:
```
:Dispatch rspec %
:Dispatch bundle exec rspec %
```
Vim will asynchronously run the `rspec` command against the current file (`%`).
## Automating Dispatch
### Automating Dispatch
Vim-dispatch has `b:dispatch` buffer variable that you can configure to evaluate specific command. You can leverage it with `autocmd`. If you add this in your vimrc:
Vim-dispatch has `b:dispatch` buffer variable that you can configure to evaluate specific command automatically. You can leverage it with `autocmd`. If you add this in your vimrc:
```
autocmd BufEnter *_spec.rb let b:dispatch = 'bundle exec rspec %'
```
Now each time you enter a file (`BufEnter`) that ends with `_spec.rb`, running `:Dispatch` automatically executes `bundle exec rspec <your-current-ruby-spec-file>`.
Now each time you enter a file (`BufEnter`) that ends with `_spec.rb`, running `:Dispatch` automatically executes `bundle exec rspec {your-current-ruby-spec-file}`.
## Learn Compile The Smart Way

Loading…
Cancel
Save