How can I set up Vim to open with a function when no file is specified?

0
2
Asked By GeekyGiraffe93 On

I'm trying to customize my Vim command in PowerShell so that when I type `vim` without any file, it automatically runs as `vim $(...)`. However, if I specify a file like `vim somedir`, I want it to just open that file normally without any extra arguments. Essentially, I want some fzf functionality to kick in only when I haven't provided a file to edit. Any insights on how to make this happen?

2 Answers

Answered By CodingDude42 On

In PowerShell, you can't create aliases with default arguments, but you can use a function to achieve what you're looking for. Here's a sample function you could use:

```powershell
function vim {
$ArgsToUse = if ($args.Count -eq 0) {
"arg1", "arg2", "arg3" # You can replace these with your fzf arguments
} else {
$args
}

$CommandToRun = Get-Command -Name vim -CommandType Application -ErrorAction Stop | Select-Object -First 1
& $CommandToRun @ArgsToUse
}
```

This checks if any arguments were passed and sets defaults if none were. If you use a different name for your function, you could skip the `Get-Command` line and just call `vim.exe` directly. Also, remember to handle your directory checks if needed!

Answered By TechieTom On

Just a quick question—can you clarify what tools you're referring to? It seems like you're mixing up command line tools here. Make sure to check the help documentation for each of the tools you're trying to use to see if there's something specific you're missing!

GeekyGiraffe93 -

I was specifically asking about creating an alias in PowerShell for Vim—it’s a bit frustrating to explain!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.