Recently I decided to learn to use Erlang, but I’m a little uncomfortable switching back and forth between the Erlang shell and Vim. Come to think of it, optimize this repetitive process. Just as if I had configured the execution shortcuts for Python, each time I pressed F6, I would execute the Python script I was currently editing and open a new Terminal, printing out messages or errors that the program normally prints

Write a Hello World script

-module(hello).
-export([say/0, main/0, hi/1]).


hi(Name) ->
    io:format("hi ~s~n",Name).

say() ->
    io:format("hello world~n").

main()->
    io:format("main~n"),
    say().

Erlang’s method of executing the script

  1. inerlang shellPerformed in theerlEnter the Erlang shell

    $ erl

    Erlang/OTP 22 [ERTS-10.5] [Source] [64-bit] [SMP :4:4] [DS :4:4:10] [Async-Thread :1] [HIPE]

    Eshell V10.5 (abort with ^G)

    1> c(hello).

    {ok,hello}

    2> hello:say().

    hello world

    ok
  2. $erlc hello.erl $erl-noshell -s hello say -s init stop erlc from the command line and start the Erlang compiler, which compiles hello.erl Generate an object code file called hello.beam erl-noshell… The hello module is loaded and the hello:say() function is executed. Finally, init:stop() is executed, terminating the Erlang session

Both of these are troublesome. Is there a simpler way? As a lazy programmer, I certainly don’t like this repetition

My solution

  1. in/usr/local/bin/Next create an executable bash script, assuming the name isexec_erl, the contents are as follows
#! /bin/bash erl_file_name=$1 shift fun=$1 shift if [ -z "$fun" ]; then fun=main fi erl_mode="${erl_file_name%.*}" erlc ${erl_file_name} erl -noshell -s $erl_mode $fun $@ -s init stop
  1. Modify the Vim configuration file
augroup filetype_erlang
    autocmd!
    autocmd Filetype erlang nnoremap <buffer> <F6> :w<CR>:ter   exec_erl %  <CR>
augroup END

summary

After writing an Erlang script, for quick verification, just write another main function and export it, then call your test method in main and press F6 to run it.

You can happily debug the script. Other languages that need to compile can be handled in a similar way, but should be simpler than Erlang’s

And there’s another benefit, If you want to validate a method, you can just execute exec_erl filename.erl functionName param1 param2 for example, I want to execute hello.erl say() exec_erl hello.erl say if you want to execute hi exec_erl hello.erl hi zhangsan

Finally, welcome everyone to pay attention to my WeChat public number: anonymous program socialite, we dig pit fill pit together.