# Short Names for Keyword Arguments

**URL:** <https://discourse.julialang.org/t/short-names-for-keyword-arguments/103407>\
**Category:** General Usage\
**Tags:** keyword-arguments\
**Created:** [August 31, 2023, 2:40pm UTC](https://discourse.julialang.org/t/short-names-for-keyword-arguments/103407 "2023-08-31T14:40:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)\
**Post date:** [August 31, 2023, 2:40pm UTC](https://discourse.julialang.org/t/short-names-for-keyword-arguments/103407/1 "2023-08-31T14:40:55Z")

</div>

Can I alias keyword arguments for brevity?

For example, in MATLAB I can do the following

```matlab
x = linspace(0, 1);
y = x.^2;
lw = "linewidth"

close all
plot(x, y, lw=2)

```

There is a similar thing in python’s matplotlib where keyword arguments have short names.

Is this possible in Julia?

I tried both of the following in CairoMakie but neither worked.

```julia
using LinearAlgebra, CairoMakie

x = LinRange(0, 1, 100)
y = x.^2

# Neither of the following work
#lw = "linewidth" 
#lw = :linewidth

fig, ax, ln = lines([x y])
# fig, ax, ln = lines([x y]; lw=3)

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [August 31, 2023, 3:10pm UTC](https://discourse.julialang.org/t/short-names-for-keyword-arguments/103407/2 "2023-08-31T15:10:16Z")

</div>

I don’t believe there is anything like this, no.

If it helps at all, you can make a `NamedTuple` or `Dict` to splat for keywords. I often find this nice for plotting and other functions that use a large number of keywords, especially when I need to pass the same options to multiple function calls.

```julia
plotkws = (;linewidth=3, color="red") # NamedTuple for keyword arguments
# plotkws = Dict(:linewidth=>3, :color=>"red") # Dict for keyword arguments

fig, ax, ln = lines([x y]; plotkws...)

# NOTE: duplicate keywords are overwritten with the right-most value
fig, ax, ln = lines([x y]; plotkws..., color="blue") # same as above but set new color

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 31, 2023, 3:16pm UTC](https://discourse.julialang.org/t/short-names-for-keyword-arguments/103407/3 "2023-08-31T15:16:54Z")

</div>

You _can_ implement it yourself.  
Plots.jl does this.  
I assume it does it through some normalization function that maps all the different keywords a common name and choses the last of them to use the value from.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 31, 2023, 3:31pm UTC](https://discourse.julialang.org/t/short-names-for-keyword-arguments/103407/4 "2023-08-31T15:31:11Z")

</div>

That’s on the implementation side. But on the caller-side you **can do** exactly what @TI36XPro asks, even if the given function hasn’t implemented short aliases itself. You just use a `Pair` (with `=>`) instead of a kwarg literal:

```julia
x = LinRange(0, 1, 100)
y = x.^2
lw = :linewidth
fig, ax, ln = lines([x y]; lw=>3)

```
