showkey
showkey

Reputation: 328

How to run the shell command in the appointed line in vim?

I have a file edited in Vim with many lines. There is a specific line that contains a shell command, which I want to run.
How can I do this through Vim?

enter image description here

Upvotes: 0

Views: 184

Answers (2)

Mark Reed
Mark Reed

Reputation: 95385

You can use this map:

:nmap ^ GI:!^V^[yy@"Xx

(Pick your favorite key command you don't use in place of ^ for the mapping;I like ^ because I always use 0 for its default function. Enter the ^V^[ with control-V control-V control-V Esc)

Then you can type 4^ to execute line 4, or just ^ to execute the last line in the file.

Upvotes: 2

Alon
Alon

Reputation: 2929

try Use

 :exec '!'.getline('.')

This is like to copy the current line and run it.

You can also map this command to

 map <F12> :exec '!'.getline('.')

getline receives the number of the line. if you will write 4 it will the line 4. The "." it run the current line.

so for run the command in line 4 you can write.

:exec '!'.getline(4)

Upvotes: 1

Related Questions