branquito
branquito

Reputation: 4044

send code segment from vim to shell for temporary testing (bash)

Having this typed in VIM, would it be possible to send it to shell, as if it were typed there, so as a result I would have that function defined in shell, and I would run it by calling it there?

# declare helper functions
function gen_credentials {
    cat > .credentials <<-EOM
    password: my_secret_password
    user: my_pretty_name
    db: script_db5
    dbprefix: son_45678
    live_site: sonikete3.com
    sqlfile: test-import.sql
    log_path: $(pwd)
    tmp_path: $(pwd)
EOM
}

The code snippet above, is a small segment of a whole file, so I would like to send piece of code as visual selection to shell, instead of passing whole script via % substitution.

Upvotes: 0

Views: 56

Answers (2)

romainl
romainl

Reputation: 196596

  1. Select the lines.

  2. Type : to enter command-line mode, the visual range is inserted automatically for you: :'<,'>.

  3. Type :'<,'>w !sh and press enter.

See :help :w_c.

You can replace sh with whatever interpreter you need.

Upvotes: 2

Degustaf
Degustaf

Reputation: 2670

The command is

:! ./%

Now, breaking it down, :! tells vim that you want it to pass the rest of the command to the shell. % is the symbol to reference the current file.

Note that this only works if you have saved the file.

Upvotes: 0

Related Questions