[vim/vim] Ability to chain global commands (Issue #9063)

44 views
Skip to first unread message

Rasmus Lindahl

unread,
Oct 29, 2021, 3:04:38 AM10/29/21
to vim/vim, Subscribed

It would be useful to be able to chain global commands. In that way, you could quickly execute multiple commands on multiple lines. Perhaps it could be accessed in the following fashion:

:g/pattern/cmd1;cmd2;cmd3

This command would find each line matching to "pattern" and sequentially execute cmd1, cmd2, and cm3. Should be relatively easy to implement.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

Jürgen Krämer

unread,
Oct 29, 2021, 3:18:13 AM10/29/21
to vim/vim, Subscribed

Vim's command separator is the pipe symbol |. Have you tried

:g/pattern/ cmd1 | cmd2 | cmd3

?

Rasmus Lindahl

unread,
Oct 29, 2021, 4:48:14 AM10/29/21
to vim/vim, Subscribed

I've tried that and it, unfortunately, doesn't work. I was browsing the source code and I think this is the function for global commands:

image

and this is the end of that function:

image

I guess you could do something in the following fashion if you assume cmd is the g/pattern/cmd:

if (*cmd* contains ";") 
{
    var cmds = cmd.split(";");
    foreach (var cmd in cmds) 
    {
        global_exe(cmd);
    }
}
else
{
    global_exe(cmd);
}

Do you think that would be sufficient?

Rasmus Lindahl

unread,
Oct 29, 2021, 5:07:36 AM10/29/21
to vim/vim, Subscribed

I guess it would make the most sense to use a pipe symbol. Replace "global_exe(cmd);" in original code with something like this?

const char delimeter[2] = "|";

// Multiple commands
if (strstr((char *)cmd, delimeter) != NULL)
{
    char *token;
    token = strtok(cmd, delimiter);
    // Split and execute each command
    while(token != NULL ) 
    {
        global_exe(cmd);
        token = strtok(NULL, s);
    }
}
else
{
    global_exe(cmd);
}

Jürgen Krämer

unread,
Oct 29, 2021, 5:14:59 AM10/29/21
to vim/vim, Subscribed

I've tried that and it, unfortunately, doesn't work. I was browsing the source code and I think this is the function for global commands:

What was the command you actually tried? Did you get an error message? If yes, what did it say?

Rasmus Lindahl

unread,
Oct 29, 2021, 5:26:23 AM10/29/21
to vim/vim, Subscribed

Actually, it seems to work. I don't know what I did to fuck it up. This can be closed. You can achieve this with:

:g/pattern/ cmd1 | cmd2 | cmd3

As you wrote.

Bram Moolenaar

unread,
Oct 29, 2021, 6:37:01 PM10/29/21
to vim/vim, Subscribed

Closed #9063.

Reply all
Reply to author
Forward
0 new messages