How to bookmark most using linux command line - linux

How i can bookmark my custom command line in linux (centos 6.2)?
I'm using history | grep keyword and then !command number now.
But is there any faster solution?

just use alias:
alias lala="ls -lrt|grep a"

Yes. You can define an alias, a function, or write a script in your bin folder.

Most shell's provides more complex history expansion. For example you can use
!?str
in zsh, bash etc. to execute the most recent command that contains str.
Another possibility to save keystrokes would be to use the incremental search of the history if you want a broader control over what you want to select.
To search the history backwards this feature is usually bound to <ctrl>-r, for forward search it's <ctrl>-s (at least in bash and zsh).

Just put a comment tag on it and you can search for the tag latter.
$ my-command # tag-bookmark-1
Then later you can grep or search history for tag-bookmark-1.
Use something shorter if you would like.
(I can't say about !, because I always use export histchars="")

Related

is there a special search command for options and commands in man-pages (in less)?

Question: using the less command in any linux shell (i'm using bash as probably most people do), is there a way to search a file only for it's commands or options?
So, to be more precise:
if i want to quickly find the description for one special option in a man-page,
is there a special search syntax to quickly jump to the corresponding line explaining that specific command?
example:
if i type:
man less
and i want to quickly find the description for the "-q" command,
is there a search syntax to directly jump to that line?
If I type /-q, it finds all occurences of "-q" everywhere in the file, so I get around 10-20 hits, of which only one is the one i was looking for..
So I'm just hoping there is a better/quicker way to do this..
(not to important though :D)
In man, options are generally described with the option name in bold at the start of the line.
So, if you are looking for the option -q, then the search command would be /^\s*-q\>
The regex ^\s*-q\> reads as follow:
^ start of a line
\s* any number of spaces (including none)
-q the option name you are looking for
\> the end of the word

How to add a custom command/alias like hello in fish shell? [duplicate]

I would like to define some aliases in 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. Any ideas?
Just use 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.
Interested people might like to find out more about fish aliases in the official manual.
This is how I define a new function foo, run it, and save it persistently.
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:
$ 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”
If you add an abbr instead of an alias you'll get better auto-complete. In fish abbr more closely matches the behavior of a bash alias.
abbr -a gco "git checkout"
Will -add a new abbreviation gco that expands to git checkout.
Here's a video demo of the resulting auto-complete features
fish starts by executing commands in ~/.config/fish/config.fish.
You can create it if it does not exist:
vim ~/.config/fish/config.fish
and save it with :wq
step1. make configuration file (like .bashrc)
config.fish
step2. just write your alias like this;
alias rm="rm -i"
Save your files as ~/.config/fish/functions/{some_function_name}.fish and they should get autoloaded when you start fish.
if there is not config.fish in ~/.config/fish/, make it.
there you can write your function .function name; command; end
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).
Short, updated, current answer for all releases since 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):
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):
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:
functions -e <aliasname> # or --erase
rm ~/.config/fish/functions/<aliasname>.fish
Ironically, you may even want to alias this into unalias. You'll need to first alias -s unalias the functions -e ... part, then funced -s unalias again to add the rm ....
Note that #nemanja's answer does include the alias -s command, but doesn't go into much detail. 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:
Go into more detail on why ('alias -s`) is the best solution.
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.
However, when you define an alias this way, it is defined only in memory. It is not persisted. That ability was added shortly after that answer was written.
Outdated method #1
With that in mind, the currently accepted answer from 2010 has a few issues nowadays.
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. Defining functions/aliases in your startup config is the "bash/zsh way". Fish is better than that ...
One of the (many) features that sets fish apart from bash and zsh is lazy-loading. 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. Note that this will be $XDG_CONFIG_HOME/fish/functions if that variable is defined.
Functions in this directory are lazy-loaded.
Lazy loading means:
Fish does not load any part of your alias/function when it starts. 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.
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>.
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?
This lazy-loading might sound complicated, but it's almost exactly what the alias -s command does for us without any additional effort.
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. It also adds a --description, which is used to describe the function as an "alias". 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.
As with the original answer, this is the suboptimal, bash/zsh way of doing things. This means that your aliases/functions will be processed and loaded every time you start a new shell.
I recommend that you take advantage of lazy-loading instead.
mkalias function
This answer by #Mike defines a mkalias function that creates and saves the alias. 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.
abbr command
#TobiasMühl's answer recommends using the abbr command, which is a reasonable alternative. However, note that alias does handle completions, and in pretty much the same manner as the abbr example given in that answer.
alias -s gco "git checkout"
And completions will be based on git checkout, just as if it were an expanded abbreviation.
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). 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. 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.
To properly load functions from ~/.config/fish/functions
You may set only ONE function inside file and name file the same as function name + add .fish extension.
This way changing file contents reload functions in opened terminals (note some delay may occur ~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.
#bozhidar-batsov gave an absolutely complete answer that helps one understand the inner workings of the alias/function in 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.
-s or --save Automatically save the function created by the alias into your fish configuration directory using funcsave.
One-line solution for defining and saving an alias (for example):
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.
# 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
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:
Create ~/.config/fish/config.fish. This file can optionally be a softlink.
Add to it the line alias myalias echo foo bar.
Restart fish. To confirm the definition, try type myalias. Try the alias.

substituting multiple possible patterns using OR in vim search

I often find myself using something like:
sed -ri 's/<\/(abc|def ghi|j klm)>//g' someFile.html
to perform substitutions on multiple possible patterns, in this case, a closing html tag to be deleted, saving me the time and effort to do this three separate times for three closing tags I want deleted.
Is there a way to do this using substitute on vim's cli? I haven't yet found a way to do it, but it would be more efficient than going to a terminal cli or running sed from within vim if it could be done natively instead.
Yes, you don't need to use an external program at all:
:%s#</\(foo\|bar\|baz\)>##g
You can use the silent ! command to silently execute shell commands from inside vim:
:silent !sed -ri 's/<\/(abc|def ghi|j klm)>//g' %
This will execute the command in the shell silently (it won't take you away from vim to see any shell output). The % means the current buffer name. Vim will then notify you that the file you are editing has been changed and will ask you if you want to load the changes, press l for load and the new changes from the sed shell command will appear.

Dynamic abbrev expand for the shell

Is there a function on one of the linux shells like the emacs dabbrev-expand?
First to give a definition:
M-xdescribe-functionEnterdabbrev-expandEnter
...
Expands to the most recent, preceding word for which this is a prefix.
Given that bash seems to be most heavily influenced by Emacs, looking there first reveals a few possibilities:
man bash(1), readline section
dynamic-complete-history (M-TAB)
Attempt completion on the text before point, comparing the text
against lines from the history list for possible completion matches.
dabbrev-expand
Attempt menu completion on the text before point, comparing the text
against lines from the history list for possible completion matches.
By default (or my system at least), M-/ is already bound to complete-filename:
$ bind -l | grep /
"\e/": complete-filename
You could re-bind it by putting
"\e/": dabbrev-expand
in your ~/.inputrc or /etc/inputrc.
Note that it only seems to complete the first word (the command), and only from history, not from the current command line as far as I can tell.
In zsh, I can't see anything in the man page that does this, but it should be possible to make it happen by figuring out the appropriate compctl command (Google mirror).

Define an alias in fish shell

I would like to define some aliases in 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. Any ideas?
Just use 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.
Interested people might like to find out more about fish aliases in the official manual.
This is how I define a new function foo, run it, and save it persistently.
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:
$ 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”
If you add an abbr instead of an alias you'll get better auto-complete. In fish abbr more closely matches the behavior of a bash alias.
abbr -a gco "git checkout"
Will -add a new abbreviation gco that expands to git checkout.
Here's a video demo of the resulting auto-complete features
fish starts by executing commands in ~/.config/fish/config.fish.
You can create it if it does not exist:
vim ~/.config/fish/config.fish
and save it with :wq
step1. make configuration file (like .bashrc)
config.fish
step2. just write your alias like this;
alias rm="rm -i"
Save your files as ~/.config/fish/functions/{some_function_name}.fish and they should get autoloaded when you start fish.
if there is not config.fish in ~/.config/fish/, make it.
there you can write your function .function name; command; end
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).
Short, updated, current answer for all releases since 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):
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):
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:
functions -e <aliasname> # or --erase
rm ~/.config/fish/functions/<aliasname>.fish
Ironically, you may even want to alias this into unalias. You'll need to first alias -s unalias the functions -e ... part, then funced -s unalias again to add the rm ....
Note that #nemanja's answer does include the alias -s command, but doesn't go into much detail. 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:
Go into more detail on why ('alias -s`) is the best solution.
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.
However, when you define an alias this way, it is defined only in memory. It is not persisted. That ability was added shortly after that answer was written.
Outdated method #1
With that in mind, the currently accepted answer from 2010 has a few issues nowadays.
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. Defining functions/aliases in your startup config is the "bash/zsh way". Fish is better than that ...
One of the (many) features that sets fish apart from bash and zsh is lazy-loading. 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. Note that this will be $XDG_CONFIG_HOME/fish/functions if that variable is defined.
Functions in this directory are lazy-loaded.
Lazy loading means:
Fish does not load any part of your alias/function when it starts. 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.
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>.
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?
This lazy-loading might sound complicated, but it's almost exactly what the alias -s command does for us without any additional effort.
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. It also adds a --description, which is used to describe the function as an "alias". 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.
As with the original answer, this is the suboptimal, bash/zsh way of doing things. This means that your aliases/functions will be processed and loaded every time you start a new shell.
I recommend that you take advantage of lazy-loading instead.
mkalias function
This answer by #Mike defines a mkalias function that creates and saves the alias. 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.
abbr command
#TobiasMühl's answer recommends using the abbr command, which is a reasonable alternative. However, note that alias does handle completions, and in pretty much the same manner as the abbr example given in that answer.
alias -s gco "git checkout"
And completions will be based on git checkout, just as if it were an expanded abbreviation.
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). 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. 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.
To properly load functions from ~/.config/fish/functions
You may set only ONE function inside file and name file the same as function name + add .fish extension.
This way changing file contents reload functions in opened terminals (note some delay may occur ~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.
#bozhidar-batsov gave an absolutely complete answer that helps one understand the inner workings of the alias/function in 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.
-s or --save Automatically save the function created by the alias into your fish configuration directory using funcsave.
One-line solution for defining and saving an alias (for example):
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.
# 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
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:
Create ~/.config/fish/config.fish. This file can optionally be a softlink.
Add to it the line alias myalias echo foo bar.
Restart fish. To confirm the definition, try type myalias. Try the alias.

Resources