Skip to content

Commit 3eeb5f1

Browse files
committed
Add Check For An Executable as a vim til
1 parent 94d23f8 commit 3eeb5f1

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_547 TILs and counting..._
13+
_548 TILs and counting..._
1414

1515
---
1616

@@ -544,6 +544,7 @@ _547 TILs and counting..._
544544
- [Case-Aware Substitution With vim-abolish](vim/case-aware-substitution-with-vim-abolish.md)
545545
- [Case-Insensitive Substitution](vim/case-insensitive-substitution.md)
546546
- [Center The Cursor](vim/center-the-cursor.md)
547+
- [Check For An Executable](vim/check-for-an-executable.md)
547548
- [Check Your Current Color Scheme](vim/check-your-current-color-scheme.md)
548549
- [Close All Other Splits](vim/close-all-other-splits.md)
549550
- [Close All Other Windows](vim/close-all-other-windows.md)

vim/check-for-an-executable.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Check For An Executable
2+
3+
Sometimes Vim needs to reach outside of itself to use an existing program.
4+
For example, wiring up [auto-formatting of JavaScript
5+
code](https://hashrocket.com/blog/posts/writing-prettier-javascript-in-vim)
6+
requires Vim to call out to the
7+
[`prettier`](https://github.com/prettier/prettier) binary.
8+
9+
We want our `.vimrc` files and plugins to, generally, be as portable as
10+
possible. What happens if you haven't yet installed a particular program?
11+
Vim will likely experience a runtime exception. One way to get around this
12+
is to check for the presence of that program on the path. If it isn't there,
13+
don't do the thing. We can use the `executable()` function for this.
14+
15+
```vimscript
16+
if executable('prettier')
17+
...
18+
endif
19+
```
20+
21+
It will return `1` (true) if `prettier` is an executable on the path,
22+
otherwise it will return `0` (false).
23+
24+
See `:help executable()` for more details.

0 commit comments

Comments
 (0)