Mercurial alias doesn't output to editor - vim

I often end up needing to see the difference between my current commit + changes and the previous one. I do this by hg diff --rev .^
Since I do this a lot, I made an alias for it, which I called pdiff. Howver, when I run this, if the differences takes more space than the terminal, I can't navigate using vim bindings, it puts all the output in terminal.
Is there a way to get it to act the same as typing out the full command?

You have (for Mercurial 3, I suppose for earlier versions - too) wrong syntax for revset "N-th parent of". From hg help revsets
"x^n"
The nth parent of x, n == 0, 1, or 2.
If you have to use OS-level features, you have to read about shell aliases
An alias can start with an exclamation point (!) to make it a shell alias. A shell alias is executed with the shell and will let you run arbitrary commands.
and your pdiff will be something like
pdiff = !$HG diff -r .^$1 | less
and used as pdiff (0|1|2) for getting changes from second|first parent of working dir or working dir directly

Related

What does this alias do in Red Hat Linux?

cd = 'cd !* ;set prompt="! $host `dirs` % "'
This alias is on a Red Hat Enterprise linux server I use commonly, and I can't figure out what it does for me. Any ideas?
I think it is meant to change the directory and display the previous directories in the prompt. !* is a bash history expansion meaning the previous command without the first word. E.g. if the previous command was cd project, then !* is project. See the History Expansion section in man bash, more specifically here.
dirs simply displays the stack of directories, and is a bash builtin, see the link for details.
However, in standard bash, you'd use export PS1=... instead of set prompt=, so perhaps the author of the alias meant it to be used in csh rather than bash, e.g. see here. The history expansion !* has the same semantics, e.g. see here for tcsh.
In csh aliasing, !* is the input the command received, e.g.
alias + emacs \!* & and running + somefile will open somefile using emacs and move it to the background.
Now to your line - it changes directory to whatever you give it (!*) and sets the prompt (the line written in your terminal before your writing zone) to the host name, path (as pwd) and %
Just a quick note - pay attention to the \ in my alias line - you must escape special characters in your alias and pay attention to this

Setting a part of a PWD as a prompt and keeping a variable updated

I'm using tcsh, and I'm trying to set a part of the PWD to appear always in the prompt (so I will always know in which "parent" directory I am).
I managed to extract the needed part of the prompt in the following way, and it works fine (I call it MyTreePath):
set MyTreePath=`echo $PWD | awk '{... print whichTree}'`
I've added the code above to my .tcshrc and I've added %$MyTreePath to my set prompt line in .tcshrc.
The problem is that once the shell is opened, the MyTreePath doesn't change, even if I'm going to a totally different path.
How to keep a variable that appears in the prompt updated?
Use the magical cwdcmd alias! It is used for defining a command which executes everytime the cwd changes. In your case, you need to updated your variable.
From the manpage:
The beepcmd, cwdcmd, periodic, precmd, postcmd, and jobcmd Special
aliases can be set, respectively, to execute commands when the
shell wants to ring the bell, when the working directory changes,
every tperiod minutes, before each prompt, before each command gets
executed, after each command gets executed, and when a job is started
or is brought into the foreground.
Here's a quick example:
alias cwdcmd 'set FOO=`pwd`'
set prompt='%$FOO >>> '
field testing:
cd /
/ >>> cd dev
/dev >>>
So all that's left is to replace pwd in the alias above with your own command.
For more info and other magic aliases, see here.

How to add something at the beginning of every Bash command?

I'd like to change my Bash configuration, so when I type something (e.g. foo bar) in the command prompt, it really executes h foo bar.
I want to do it because I often use hilite (aliased as h) to color stderr in red, and I would like to make this behaviour permanent.
Other use I see would be interacting with Git, as I write lots of commands like:
git status
git add ...
git commit ...
I guess I could use preexec_invoke_exec to execute something before
the command is being run, but I don't know how can I change the
command or prevent it from executing.
Any ideas?
You can achieve this by binding the return key to insert the h for you. You can do this by adding this to your .input.rc:
Return: "\C-ah\ \n"
or put this bind in your .bashrc:
bind 'RETURN: "\C-ah \n"'
(Kudos to these guys).
There are a few catches: obviously, it's bash-only, and this can give some pretty strange behavior in places (I can't think of a decent example right now), so I wouldn't say this is 'good' bashing in any way.
I would personally skip hilite and keep it all pure bash. So instead, try to look for a way to append something to each command so as to redirect the stderr stream to a colorized echo/printf...but that's a matter of preference I guess :)
To run a different command from the one you've specified, your best bet may be to maks a bash function of the commands to be caught.
I don't know anything about "hilite", but if it installs a binary, at, say /usr/bin/hilite, you could use:
git () {
/usr/bin/hilite /usr/bin/git "$#"
}
So ... when you run git at your bash prompt, bash will actually run hilite, using /usr/bin/git and the rest of your command line arguments as the arguments to h.
Note that this should work in traditional Bourne shell as well as bash.

Bash: call script with customized keyboard shortcuts?

Lets say I have a script, "myscript.sh", with contents being simply echo $PWD. I'd like to bind somehow this script to a key combo in bash (gnome-terminal) - so that when I press this key combination, the output of "myscript.sh" is inserted ("pasted") at the cursor position in the terminal.
Apparently, bash history and line manipulation is handled by readline - and the references I got for bash keyboard shortcuts, do reference readline:
bash keyboard shortcuts
Bash Reference Manual: Bindable Readline Commands
I've also seen in Bash Reference Manual: Readline Init File Syntax that the key bindings for bash can be listed by using bind -p (see help bind [not 'man bind'] for more). So maybe this question would better be titled as "_binding macros to custom keyboard shortcuts in readline" :) But in any case, is what I want possible to do?
I guess an alternative would be to have the script be something like "pwd | xsel -b", and then I call it on terminal - and I can paste afterwards; but I'd still like a single keyboard shortcut instead, say like Ctrl-Alt-H (which seems to be not used for anything), which will immediately insert/paste script output when pressed.
Thanks in advance,
Cheers!
EDIT: Just to clarify - here is my use case where I'd like this facility. I'm usually cd'd in a project folder, usually named something like myproject-folder-0012a, which is under revision control by svn. And there is a bunch of these folders. So quite often, I do commits where the first word of the message is the directory name, as in:
svn ci -m "myproject-folder-0012a: here a commit message"
But that is what I don't like - first I type 11 characters, which go rather fast:
svn ci -m "
And then, I cannot use autocompletion to get the name (i'm inside the folder) - which means I either have to fully type it (no way :)), or I copy paste it from the prompt (which requires selection - press mouse, drag, release mouse; then Ctrl+Shift+C, and then Ctrl+Shift+V, plus any left/right keys if I miss allignment - plus deletions and such if I make the copy wrong).
Meaning - so much work, just to get the bloody folder name for a bloody commit message :( I'd MUCH rather press something like (say) Ctrl-Alt-H, and have the folder name automatically inserted at cursor position, and be done with it :)
My suggestion for xsel is only because I could put it into a "global" script - say symlink it as /usr/bin/myscript (and obviously, the contents of the script are echo $(basename $PWD) rather than just pwd for my needs), and then I could do:
$ myscript # this puts directory name in clipboard
$ svn ci -m "[CTRL+SHIFT+V TO PASTE HERE]myproject-folder-0012a[NOW TYPE]: here a commit message"
... which sort of makes the workload less, but still - then I have to remember what the script name is, and call it, before I type the svn command (and I don't always remember that)... And still - I have to call a command, and then press a key combo; why shouldn't I just press a key combo once, and be done with it ??! :)
Well, hope this clarifies my problem a bit better ....
EDIT2: However, another reason why a bash keyboard shortcut would be useful, is that then I could also "paste/insert current directory name" not only in shell commands - but also in terminal programs, say like nano (where it would, arguably, be more difficult to use bash script or function expansion directly).
Simple version:
This command at a shell prompt:
bind '"\ee": "${PWD##*/}\e\C-e"'
or this line added to your ~/.inputrc:
"\ee": "${PWD##*/}\e\C-e"
will cause Alt-e to insert the basename of the current directory on the command line. It requires that the default binding of the readline function shell-expand-line which is \e\C-e be present (this could be adapted if it's different). I'm also making the assumption that you're using Bash's emacs mode.
Unfortunately, it causes things that have already been typed to be expanded as well. One of the affects of this is that after having typed:
svn ci -m "
and pressing Alt-e, the quotation mark will have disappeared. There are a couple of ways to deal with this.
One, assume that all you'll lose is the quote and either manually add it back or have the readline macro add it for you:
bind '"\ee": "${PWD##*/}\e\C-e\eb\"\C-e"'
which just isn't very satisfactory.
Advanced version:
Or, two, kill the line, do the insertion, then yank the line back:
bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b"'
or
bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b\ef\C-f"'
This leaves the rest of the line intact (nothing else is expanded or deleted), but it uses the kill ring, so it may leave it in a state that's different than you expect (if you're using it). It also inserts a space after the inserted directory name (the spaces in the macro are used to ensure that older kill-ring contents are not regurgitated if the macro is executed at the beginning or end of the line). The macro should work regardless of the position of the cursor in the line. The insertion will be made at the cursor's position, leaving the cursor in the same position [in the first version].
Edit: The second version leaves the cursor after the dirname and space that are inserted.
Edit 2:
The readline function shell-forward-word (unbound) does a better job than forward-word (\ef) for this. You can make use of that like this:
bind '"\ew":shell-forward-word'
bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b\ew\C-f"'
By the way, you should know that Bash keyboard shortcuts are not active in other programs such as nano.
Ok, not really an answer, but I'd just like to summarize the comments I got so far, which are useful for my problem. However, the question as it stands - in respect to bash keyboard shortcuts running arbitrary scripts - is still not answered (I'd still prefer doing all this with a single key combo :))
First, I can use a 'global' script like:
$ sudo bash -c 'cat > /usr/bin/bpwd <<EOF
#!/bin/bash
basepwd=\$(basename \$(pwd))
echo -n \$basepwd # suppress line ending
# exec 1>/dev/null # debug: redir stdout to null
echo -n \$basepwd | xsel -i -b # suppress LF, and make xsel read from stdin
# exec 1>/dev/tty # debug: restore stdout
EOF
chmod +x /usr/bin/bpwd'
Or, I can add bash functions to my .bashrc (note: make sure you reload bash after you add these lines to .bashrc - for example, simply by typing bash in your current terminal):
$ echo '
bpwd2() { basepwd=${PWD##*/} ; echo -n $basepwd | xsel -i -b ; echo -n $basepwd ; }
svnci-test() { echo -n "$(bpwd2): $*" ; }
svnci-m() { svn ci -m "$(bpwd2): $*" ; }' >> ~/.bashrc
Basically, I misunderstood Reese Moore's suggestion originally - you can indeed use backticks - consider this command session (after the above commands have been ran):
$ bpwd
Desktop\
$ bpwd2
Desktop\
$ echo `bpwd`
Desktop
$ echo "`bpwd2` 2"
Desktop 2
This is what I needed to understand Moore's "the output from the backticked commands will be used as input on the executed command" (however, one also needs to take care to clean the line endings from the output); or, in my case, I can call
svn ci -m "`bpwd`: my message here"
# svn ci -m "${PWD##*/}: my message here" # alternatively
... or, I could follow camh's suggestion, and use svnci-m as a function (in my case, I almost never use additional arguments to svn ci, and so my version is slightly different). And to test whether arguments are passed correctly, I can use the svnci-test function:
$ svnci-test "my message"
Desktop: my message\
Thanks for the comments so far,
Cheers!
One way to do what you want with a single key press is to take advantage of programmable completion in bash. You possibly have some programmable completion set up with the bash_completion tool/package. If not, look into that to see the specifics of how it is done.
The idea is to have the programmable completion recognise when you have hit at the start of a svn commit message and then have it return a single completion which is the text you want to insert (the basename of the current directory).
I've only dabbled with programmable completion so I can't give you the details, but the above-mentioned bash_completion package or the subversion completion script may be a good start.

Creating a shorter version of a bash command

i am novice to the Linux shell and had to recently start using it for work...i have now got used to the basic commands in bash to find my way around...however there are a lot of commands i find myself typing over and over again and its kind of a hassle to type them every time...so can anyone tell me how can i shorten the command syntax for ones i use frequently.
A very simple example, i use the ls -lh command often, though this is quite short but im just giving an example. Can I have something (a shell script may be) so that I can run it by typing just say lh.
I want to do it for more complex commands.
alias lh='ls -lh'
If you want to make this persistent across sessions, put it in your .bashrc file. Don't forget to run source .bashrc afterwards to make bash aware of the changes.
If you want to pass variables, an alias just isn't enough. You can make a function. As an example, consider the command lsall to list everything in a given directory (note this is just an example and thus very error prone):
function lsall
{
ls $1/*
}
$Ngets replaced with the Nth argument.
You would place the following alias in your .bashrc file:
alias lh='ls -lh'
Now lh is shorthand for ls -lh.
For more complicated tasks you could use a bash function. For example, on one of my machines I have a function which causes 'ls' to run after every successful 'cd':
cdls() {
builtin cd "$*" && ls
}
alias cd='cdls'
you can define aliases. For longer commands, use a function, put it into a library file and source it whenever you want to use your functions.
Just for the sake of completeness, since you want to learn bash: you could also write a function
lh() {
ls -lh "$#"
}
although I would never write that when a simple alias would do ;-)
;) Heh, I remember one problem when I was starting out on Linux, which is that I would ask questions like these, and people would diligently answer them, but no one would explain how to make such changes permanent, and so I found myself typing in a bunch of commands every time I opened a terminal.
So, even though others have accurately answered this question... if you want to make the change permanent, put the alias-line into your ~/.profile or ~/.bashrc file (~ = your home directory). It depends a bit on your distribution on which is run when, but I always try adding my aliases to ~/.profile first and if that doesn't work, then ~/.bashrc. One of them should work for sure.

Resources