简体   繁体   English

在 fish shell 中定义一个别名

[英]Define an alias in fish shell

I would like to define some aliases in fish.我想在 fish 中定义一些别名。 Apparently it should be possible to define them in显然应该可以在

~/.config/fish/functions

but they don't get auto loaded when I restart the shell.但是当我重新启动 shell 时它们不会自动加载。 Any ideas?有任何想法吗?

Just use alias .只需使用alias Here's a basic example:这是一个基本示例:

# Define alias in shell
alias rmi "rm -i"

# Define alias in config file ( `~/.config/fish/config.fish` )
alias rmi="rm -i"

# This is equivalent to entering the following function:
function rmi
    rm -i $argv
end

# Then, to save it across terminal sessions:
funcsave rmi

This last command creates the file ~/.config/fish/functions/rmi.fish .最后一条命令创建文件~/.config/fish/functions/rmi.fish

Interested people might like to find out more about fish aliases in the official manual .有兴趣的可以去官方手册中了解更多关于fish别名的知识。

This is how I define a new function foo , run it, and save it persistently.这就是我定义新函数foo 、运行它并持久保存它的方式。

sthorne@pearl~> function foo
                    echo 'foo was here'
                end
sthorne@pearl~> foo
foo was here
sthorne@pearl~> funcsave foo

For posterity, fish aliases are just functions:对于后代来说,fish 别名只是函数:

$ alias foo="echo bar"
$ type foo
foo is a function with definition
function foo
    echo bar $argv; 
end

To remove it删除它

$ unalias foo
/usr/bin/unalias: line 2: unalias: foo: not found
$ functions -e foo
$ type foo
type: Could not find “foo”

fish starts by executing commands in ~/.config/fish/config.fish. fish 首先执行 ~/.config/fish/config.fish 中的命令。 You can create it if it does not exist:如果它不存在,您可以创建它:

vim ~/.config/fish/config.fish vim ~/.config/fish/config.fish

and save it with :wq并用:wq保存

step1.步骤1。 make configuration file (like.bashrc)制作配置文件(如.bashrc)

config.fish配置.fish

step2.第2步。 just write your alias like this;像这样写你的别名;

alias rm="rm -i"别名 rm="rm -i"

If you add an abbr instead of an alias you'll get better auto-complete.如果您添加abbr而不是alias ,您将获得更好的自动完成功能。 In fish abbr more closely matches the behavior of a bash alias.在 fish 中, abbr更接近于 bash 别名的行为。

abbr -a gco "git checkout"

Will -a dd a new abbr eviation gco that expands to git checkout .-a gco一个扩展为git checkout的新abbr

Here's a video demo of the resulting auto-complete features这是生成的自动完成功能的视频演示

Save your files as ~/.config/fish/functions/{some_function_name}.fish and they should get autoloaded when you start fish.将您的文件保存为~/.config/fish/functions/{some_function_name}.fish并且它们应该在您开始钓鱼时自动加载。

  1. if there is not config.fish in ~/.config/fish/ , make it.如果~/.config/fish/中没有config.fish ,则创建它。
  2. there you can write your function.在那里你可以编写你的函数。 function name; command; end

To properly load functions from ~/.config/fish/functions~/.config/fish/functions 正确加载函数

You may set only ONE function inside file and name file the same as function name + add.fish extension.您只能在文件中设置一个函数,并将文件命名为与函数名称 + add.fish扩展名相同的名称。

This way changing file contents reload functions in opened terminals (note some delay may occur ~1-5s)这种方式在打开的终端中更改文件内容重新加载功能(注意可能会出现一些延迟~1-5s)

That way if you edit either by commandline这样,如果您通过命令行进行编辑

function name; function_content; end

then然后

funcsave name

you have user defined functions in console and custom made in the same order.您在控制台中有用户定义的功能,并以相同的顺序定制。

I know there are 11 answers already at this point, and they all work , but most are also suboptimal in 2022 (and for the past few years).我知道此时已经有 11 个答案,它们都有效,但大多数在 2022 年(以及过去几年)也是次优的。

Short, updated, current answer for all releases since 3.0b1 :3.0b1以来所有版本的简短、更新、当前答案:

  • The quickest and best way to do what is requested in this question is:执行此问题中要求的最快和最好的方法是:

     alias -s <aliasname> "command(s)" # Or --save

    Important : Simply do this one time per alias at the command-line.重要提示只需在命令行中为每个别名执行一次此操作。 Do not add it to your startup config.不要将它添加到您的启动配置中。

  • To list existing aliases which have been defined this way (since fish 2.5b1 ):列出以这种方式定义的现有别名(自fish 2.5b1 起):

     alias
  • To edit an alias created this way:要编辑以这种方式创建的别名:

     funced -s <aliasname> # or --save
  • To remove an alias defined this way (since fish 3.4.0 ):要删除以这种方式定义的别名(自fish 3.4.0起):

     functions -e <aliasname> # or --erase funcsave <aliasname>

    Note that since 3.4.0 was only released a few weeks ago, I'll include the commands that work in previous versions as well:请注意,由于 3.4.0 仅在几周前发布,因此我将包括在以前版本中也适用的命令:

     functions -e <aliasname> # or --erase rm ~/.config/fish/functions/<aliasname>.fish

    Ironically, you may even want to alias this into unalias .具有讽刺意味的是,您甚至可能想将其别名为unalias You'll need to first alias -s unalias the functions -e... part, then funced -s unalias again to add the rm... .您需要首先alias -s unalias functions -e...部分,然后funced -s unalias再次添加rm...


Note that @nemanja's answer does include the alias -s command, but doesn't go into much detail.请注意, @nemanja 的回答确实包含alias -s命令,但没有详细说明。 Regardless, since it predates mine, I wouldn't mind at all if it was the accepted answer.无论如何,因为它早于我的,所以如果它是公认的答案,我根本不会介意。 However, the currently accepted answer is a bit outdated.但是,当前接受的答案有点过时了。 While it could, in theory, be edited, the necessary changes, IMHO, would create a different answer, which we aren't supposed to do.虽然理论上可以对其进行编辑,但恕我直言,必要的更改会产生不同的答案,而我们不应该这样做。

While @nemanja's answer is the best solution for current fish releases, I'm writing this as a separate answer to:虽然@nemanja 的回答是当前鱼类发布的最佳解决方案,但我将其作为一个单独的答案来写:

  • Go into more detail on why ('alias -s`) is the best solution.详细了解为什么('alias -s`) 是最佳解决方案。
  • Go into more detail on why the other answers are suboptimal with current fish releases.更详细地了解为什么其他答案对于当前的鱼放生不是最佳的。
  • Provide the additional information on editing and removing aliases which are defined this way.提供有关编辑和删除以这种方式定义的别名的附加信息。

Much more detail on why the above is preferred over the other answers关于为什么上述答案优于其他答案的更多详细信息

First, it is important to understand that, as Glenn Jackman (a former fish shell contributor) mentioned in this answer , the alias command in fish is just syntactic sugar for creating a function.首先,重要的是要理解,正如 Glenn Jackman(前 fish shell 贡献者)在这个答案中提到的,fish 中的alias命令只是用于创建函数的语法糖。

However, when you define an alias this way, it is defined only in memory .但是,当您以这种方式定义别名时,它仅在 memory中定义。 It is not persisted.它没有持久化。 That ability was added shortly after that answer was written.在写完该答案后不久就添加了该功能。

Outdated method #1过时的方法 #1

With that in mind, the currently accepted answer from 2010 has a few issues nowadays.考虑到这一点,目前从 2010 年开始接受的答案现在有一些问题。

First, you really shouldn't define your aliases in your config.首先,你真的不应该在你的配置中定义你的别名。 That may have been correct in 2010, but even then I think fish supported lazy-loading of functions/aliases, which is a better option.这在 2010 年可能是正确的,但即便如此,我认为 fish 支持函数/别名的延迟加载,这是一个更好的选择。 Defining functions/aliases in your startup config is the "bash/zsh way".在启动配置中定义函数/别名是“bash/zsh 方式”。 Fish is better than that...鱼比那个好。。。

One of the (many) features that sets fish apart from bash and zsh is lazy-loading.将 fish 与 bash 和 zsh 区分开来的(许多)特性之一是延迟加载。 Lazy is good in this case.在这种情况下,懒惰是好的 You want lazy-loading.想要延迟加载。 You need lazy-loading (ok, well maybe not need ), but anyway...需要延迟加载(好吧,也许不需要),但无论如何......

As the original question mentioned, it is possible to define your functions in ~/.config/fish/functions/ , although it is a directory rather than a file .正如最初提到的问题,可以在~/.config/fish/functions/中定义你的函数,尽管它一个目录而不是一个文件 Note that this will be $XDG_CONFIG_HOME/fish/functions if that variable is defined.请注意,如果定义了该变量,这将是$XDG_CONFIG_HOME/fish/functions

Functions in this directory are lazy-loaded.此目录中的函数是延迟加载的。

Lazy loading means:延迟加载意味着:

  • Fish does not load any part of your alias/function when it starts. Fish 在启动时不会加载别名/函数的任何部分。 This can speed up launch times significantly, especially if you have many aliases and/or complex functions, or perhaps are running on a slower system/VM/shared CPU host.这可以显着加快启动时间,特别是如果您有许多别名和/或复杂的功能,或者可能在较慢的系统/VM/共享 CPU 主机上运行。

  • No part of the function other than the name (for lookup purposes) is loaded into memory until it is used.除了名称(用于查找目的)之外,函数的任何部分在使用之前都不会加载到内存中。

  • The first time you call a function with functionname , then and only then will fish lazy-load the function from ~/.config/fish/functions/<functionname.fish> .第一次使用 functionname 调用functionname时,fish 将延迟加载来自~/.config/fish/functions/<functionname.fish>的函数。

How much of a difference this will make will depend on a lot of factors, but personally, I keep a lookout for simple ways to optimize my configuration.这将产生多大的不同取决于很多因素,但就个人而言,我一直在寻找优化配置的简单方法。 One of the main factors that drove me from Zsh to fish was the increasingly slow startup of my Zsh config as I added features, functions, etc. We've made the switch to a better shell (in our opinion, I assume) -- Why not take advantage of its improved features?促使我从 Zsh 转向钓鱼的主要因素之一是,随着我添加特性、功能等,我的 Zsh 配置启动越来越慢。我们已经切换到更好的 shell(我认为,在我们看来)——为什么不利用其改进的功能呢?

This lazy-loading might sound complicated, but it's almost exactly what the alias -s command does for us without any additional effort .这种延迟加载听起来可能很复杂,但它几乎正是alias -s命令为我们所做的,无需任何额外的努力

In addition, the alias command, goes several steps further and automatically adds a --wraps <original_command> argument to your function so that you get the added benefit of completions.此外, alias命令更进一步,并自动将--wraps <original_command>参数添加到您的函数中,以便您获得完成的额外好处。 It also adds a --description , which is used to describe the function as an "alias".它还添加了一个--description ,用于将函数描述为“别名”。 As a result, running just:结果,只运行:

alias

... by itself will give you a list of all functions/aliases defined this way. ... 本身会给你一个以这种方式定义的所有函数/别名的列表。

Other answers其他答案

Three separate answers also all mention using ~/.config.fish/config.fish , either with function declarations or alias commands.三个单独的答案也都提到使用~/.config.fish/config.fish ,使用function声明或alias命令。

As with the original answer, this is the suboptimal, bash/zsh way of doing things.与原始答案一样,这是次优的 bash/zsh 做事方式。 This means that your aliases/functions will be processed and loaded every time you start a new shell.这意味着每次启动新 shell 时都会处理和加载别名/函数。

I recommend that you take advantage of lazy-loading instead.我建议您改用延迟加载。

mkalias function mkalias函数

This answer by @Mike defines a mkalias function that creates and saves the alias. @Mike 的这个答案定义了一个创建保存别名的mkalias函数。 A very good solution at the time (and IMHO should have had more upvotes), but it predated fish release 3.0 which added alias --save/-s , which now does the same thing.当时是一个非常好的解决方案(恕我直言,应该有更多的赞成票),但它早于 fish 版本 3.0,它添加了alias --save/-s ,现在做同样的事情。

abbr command abbr命令

@TobiasMühl's answer recommends using the abbr command, which is a reasonable alternative. @TobiasMühl 的回答建议使用abbr命令,这是一个合理的选择。 However, note that alias does handle completions, and in pretty much the same manner as the abbr example given in that answer.但是,请注意alias确实处理完成,并且与该答案中给出的abbr示例几乎相同。

alias -s gco "git checkout"

And completions will be based on git checkout , just as if it were an expanded abbreviation.完成将基于git checkout ,就像它是一个扩展的缩写一样。

There may be some cases where the completions will be more accurate because abbreviations are expanded as soon as the Space is pressed after typing the abbreviation name.某些情况下,完成会更准确,因为在键入缩写名称后按下空格键会立即展开缩写。

That's one of the fundamental differences between abbreviations and aliases in fish.这是鱼的缩写和别名之间的根本区别之一。 Abbreviations are expanded at the prompt;缩写在提示符下展开; aliases are not.别名不是。

Another difference is that abbreviations are stored in variables, which are processed/loaded at shell startup (whether universal or global/config).另一个区别是缩写存储在变量中,这些变量在 shell 启动处理/加载(无论是 universal 还是 global/config)。 As mentioned above, aliases are lazy-loaded.如上所述,别名是延迟加载的。

And yet another difference is that aliases, since they are functions, can be much more complex.另一个区别是别名,因为它们是函数,所以可能要复杂得多。 For instance, I have my ls set to be exa with the output piped to bat .例如,我将ls设置为exa ,输出通过管道传输到bat It's just not possible define that in an abbreviation.只是不可能用缩写来定义它。

That said, again, abbreviations are a feature to consider using in fish.也就是说,缩写是考虑在鱼类中使用的一个特征。 I do plan to shift a few of my aliases to abbreviations, since I have some where I want to change the arguments after expansion;我确实计划将我的一些别名转换为缩写,因为我有一些我想在扩展后更改参数的地方; something that's not possible with the unexpanded aliases.未扩展的别名不可能做到的事情。

@bozhidar-batsov gave an absolutely complete answer that helps one understand the inner workings of the alias/function in fish. @bozhidar-batsov 给出了一个绝对完整的答案,可以帮助人们理解 fish 中别名/函数的内部工作原理。 Reading fish documentation for an alias, there is also a -s flag that is really convenient to use, but I didn't see anyone mention it.阅读别名的 fish 文档,还有一个-s标志,使用起来非常方便,但我没有看到有人提到它。

-s or --save Automatically save the function created by the alias into your fish configuration directory using funcsave. -s 或 --save 使用 funcsave 将别名创建的函数自动保存到您的 fish 配置目录中。

One-line solution for defining and saving an alias (for example): alias cl 'clear' -s .定义和保存别名的单行解决方案(例如): alias cl 'clear' -s Instantly works across all sessions and is persisted.立即在所有会话中工作并持续存在。

Navigate to the ~/.config/fish/functions/ and you'll see cl.fish file.导航到~/.config/fish/functions/ ,您将看到cl.fish文件。

# Defined via `source`
function cl --wraps=clear --description 'alias cl clear'
  clear $argv;
end

make a function in ~/.config/fish/functions called mkalias.fish and put this in在 ~/.config/fish/functions 中创建一个名为 mkalias.fish 的函数并将其放入

function mkalias --argument key value
  echo alias $key=$value
  alias $key=$value
  funcsave $key
end

and this will create aliases automatically.这将自动创建别名。

I found the prior answers and comments to be needlessly incomplete and/or confusing.我发现之前的答案和评论不必要地不完整和/或令人困惑。 The minimum that I needed to do was:我需要做的最低限度是:

  1. Create ~/.config/fish/config.fish .创建~/.config/fish/config.fish This file can optionally be a softlink.该文件可以选择是一个软链接。
  2. Add to it the line alias myalias echo foo bar .添加行alias myalias echo foo bar
  3. Restart fish .重启fish To confirm the definition, try type myalias .要确认定义,请尝试type myalias Try the alias.试试别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM