4

Let's consider that I have the following bash script called time.bash:

#!/bin/bash
date -d @$1 

This script converts the epoch timestamp to its corresponding date as the following:

$ ./time.bash 1267619929
qua mar  3 09:38:49 -03 2010

Now let's suppose I'm editing the following file using Vim:

1267619929 word word 
1267619935 word word 
1267619931 word word 
1267619946 word word

Considering that I'm working just on a single line, I'd like to save the timestamp 1267619929 on the register a with "aye, execute the script time.bash with the register a content as input, and save its output on another register (let's say register b). Is it possible to do this kind of stuff on Vim?

1 Answer 1

7

You could do this:

let @b=system("time.bash " . @a)

The @a is register a and @b is register b. The system(...) runs a command and returns the output. The result is a line, so when you paste it, you get a new line.

See :help system().

Another way is:

call setreg("b", system("time.bash " . @a), "v")

Here you can set the mode for the register. The lower case v sets it to characterwise. With it you can paste the result within a line.

See :help setreg()

You could also do it without first copying the value to register a. Put the cursor on the numeric value and execute:

call setreg("b", system("time.bash " . expand("<cword>")), "v")

The expand("<cword>") is expanded to the word under the cursor.

See :help <cword> and :help expand().

3
  • Thanks!! That was exactly what I needed... Now that I've played a little with these commands I've realized that without the . on let @b=system("time.bash " . @a) the command doesn't work. Just as a curiosity, what is the . purpose inside the system function? Jun 21, 2019 at 6:11
  • 2
    @RafaelMuynarsk The dot is the operator for string concatenation.
    – Ralf
    Jun 21, 2019 at 6:47
  • Or (untested) :s/<cword>/\=system("time.bash" . submatch(0)/
    – D. Ben Knoble
    Jun 21, 2019 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.