GAVD
GAVD

Reputation: 2134

Open multi files by vim

I know how to open multi modified files in vim with git.

vim -O $(git status -uno -s | awk '{print $2}')

But when I add this command in .bashrc file

export vim_open="vim -O $(git status -uno -s | awk '{print $2}')"

It does not work as I expect. I try to echo it but it print only vim -O.

EDIT: my bad: git status -uno -s

How can I make alias for the command?

Upvotes: 2

Views: 182

Answers (2)

sensorario
sensorario

Reputation: 21600

May I suggest also a similar alias that opens in tab all modified files

alias vmod='vim -p `git diff --name-only `'

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172540

it print only vim -O.

The whole right-hand side is inside double quotes; that means that the command substitution ($(...)) is evaluated immediately, while you're defining the variable. If (at that point) you're not in a Git working copy, you will get an error; if there are just no modified files, the result will be empty, and that is persisted in your variable.

A proper alias

The export command is for environment variables; in Bash, there's a separate alias command:

alias vim_open='vim -O $(git stat -uno -s | awk '\''{print $2}'\'')'

Note how I used single quotes to avoid the immediate execution of the embedded command substitution (and then had to escape the other embedded single quotes). Alternatively, I could have kept double quotes and escaped just the $ characters for command substitution and variable reference:

alias vim_open="vim -O \$(git stat -uno -s | awk '{print \$2}')"

Super powers with a function

Finally, to retain the ability to pass command-line arguments to Vim (these would have to come before the file list, but aliases can only append them at the end), you would have to define a shell function instead:

vim_open()
{
    #     v--- arguments passed to vim_open are inserted here
    vim "$@" -O $(git stat -uno -s | awk '{print $2}')
}

Upvotes: 8

Related Questions