Zsh tries to correct a command to a file - zsh

Zsh has a feature that lets it prompt for corrections to files in the current directory. Eg, if I say cd bar when I mean to say cd baz, then zsh will say: zsh: correct 'bar' to 'baz' [nyae]?
Normally, this works fine. However, sudo seems to mess things up. Specifically, suppose I want to version control my apache2 directory with git. I would type something like sudo git add . This is the correct command to run. However, zsh would prompt me with zsh: correct 'git' to '.git' [nyae]? as if it didn't know that git was a command, so it thought I was trying to refer to the .git folder.
Why is this happening? How can I get it to stop prompting me in those situations?
Thanks!
EDIT: It seems like zsh, by default, will consider all arguments to a command to be files or directories. However, I know that there is some functionality to extend this. For instance, if I type git sttab, then zsh will complete it to stash, status, or stripspace (with documentation on each of those). I would, ideally, like zsh to keep providing tips like these even with something like sudo (so, I would rather not do a nocorrect). How do I customize that functionality in zsh?

Either use nocorrect before the command itself, or define an alias
alias sudo="nocorrect sudo"

Related

exec bash command in .profile file not letting Control-M job to run

I have an issue where my Control-M job is not able to execute anything on the unix box.
after investigation found out that .profile file in the unix server is the culprit.
content of the .profile file is
exec bash
I tried renaming the file and run the job in UAt and it did work where as I am not sure whats the implication of not having this file.
Can some one pls help me with explaining
what would be the overall impact if I rename the .profile file
how the content of .profile file being used in the server
I don't know what this "Control-M" thing is, but you should be able to safely remove that one-line .profile from your account with no problem. All it does is replace whatever command shell is assigned by default to your account with the command shell called bash. If you don't care if you use the default shell or bash, and especially if you are having problems with that .profile file, then just remove it.
If you really want to use bash then you might try changing your default shell with the chsh command. That may also cause problems for this "Control-M" thing, so you'll want to read the chsh manual page to be sure you know how to determine what your current shell is and how to change back to the original value if there are any problems.

Filter command history by folder they were executed in?

I know shell history doesn't keep track of the folder the commands were executed in but I think it would be really useful to be able to output the history for a particular folder by using a flag like history --local for example.
I often jump from project to project which use very similar commands but have different destination host for ssh or environment variable...
Is there any way to achieve that –preferably using zsh?
In bash, you can set PROMPT_COMMAND to something like the following:
PROMPT_COMMAND='history | tail -n1 >> .$USER.history'
It will save each command to a file in the current directory.
For an alternative approach (replacing cd with a command that changes where history is saved), see http://www.compbiome.com/2010/07/bash-per-directory-bash-history.html.

How to specify a custom path for my .zshrc file?

I'm trying to move .zshrc to a folder where I keep this kind of files synced with Github.
But now whenever I start a zsh session it doesn't use that config file.
Assuming I changed the file to ~/.dotfiles how can I add ~/.dotfiles/.zshrc to the PATH(?!) to make zsh start with that config?
Doing source ~./dotfiles/.zshrc only works for that session. Doesn't work anymore if I close the terminal.
You can symlink:
ln -s /path/to/original /path/to/symlink
For the zshrc you can do something like:
ln -s ~/.dotiles/.zshrc ~/.zshrc
One alternative to a symlink is to put this in ~/.zshenv:
ZDOTDIR=~/.dotfiles
If you want .zshenv in ~/.dotfiles as well, you can look into setting ZDOTDIR in one of the global configuration files (/etc/zshenv is a good choice).
Alternatively, you can do what I do and use GNU Stow. I've got my dotfiles in a repository, one subdirectory per category, like so:
dotfilerepo/zsh/.zshrc
dotfilerepo/zsh/.zlogin
dotfilerepo/git/.gitconfig
dotfilerepo/vim/.vimrc
then I can cd into repo and do stow zsh and it'll create a symlink from ~/.zshrc to repo/zsh/.zshrc, another from zsh/.zlogin to ~/.zlogin. stow vim to create symlinks from the vim subdirectory to ~, etc.
I've got a script, install-linkfarm, that does all the stow commands so when I move onto a new machine, I clone my repo, cd to it and run install-linkfarm and am good to go.
You can put this in ~/.zshrc, even as its entire contents:
if [ -r ~/.dotfiles/.zshrc ]; then
source ~/.dotfiles/.zshrc
fi
Please use the export command mentioned below to solve your problem.
export ZDOTDIR=$HOME/.dotfiles
In Linux, you can check if your zsh is loading /etc/zsh/zshrc, and edit it.
If that's the case, redirect this to your custom script by adding:
sh $HOME/.dotfiles/zshrc
Here is an interesting hack that doesn't require you to use sym-links.
In your .xsession, (or .*wmrc) have the following:
xterm -e 'zsh -c ". ~/.dotfiles/.zshrc; zsh"'.
instead of just:
xterm
Make sure to put the -e at the end after all of your other xterm options.

How do I install GHC 7.8.1 and assign it a different command?

I would like to install GHC 7.8.1, but would like to assign it different commands, so as not to clash with 7.6.3. For example:
runghc with runghc7.8.1
ghci with ghci7.8.1
etc...
Or similar. (ghci would be most important, for typed holes.)
Basically, I want to be able to use GHC 7.8 and 7.6, so if there is a more direct way to do this tell me (A-B problem.)
Note: Ubuntu 13.10
Because you are on a unix-like system (Ubuntu) you can do the following:
Choose a folder you like for installing ghc (e.g. in a subfolder of your home directory like $HOME/ghc7.8.1 or in a subfolder of /opt like /opt/ghc7.8.1 – I would prefer the later one if you are the only user of your computer and the first one if this isn't the case). See this wikipedia article for explanations about the unix directory structure.
Download the source code into that folder and follow the installation instructions:
See also https://ghc.haskell.org/trac/ghc/wiki/Building/Using#Runtheconfigurescript
In configure setp its important, that you set the --prefix to the folder you have chosen above (if you don't do this, ghc will be installed in /usr/local/ which you do not want)! For example:
./configure --prefix=/opt/ghc7.8.1
After the installations look for the folder with the created binaries (it will be called bin if you did not use another name for bindir). Lets imagine this folder is /opt/ghc7.8.1/bin.
Now you have two possibilities:
Solution with creating symlinks: Create symlinks in a folder which is in your $PATH pointing to the created binaries (for example /usr/local/bin or $HOME/bin – I would use the first one, if you are the only user on your computer and the second if, if you are not). Therefore you have to use the command line tool ln. For example:
sudo ln -s -T /opt/ghc7.8.1/bin/runghc /usr/local/bin/runghc7.8.1
After this command there is a file /usr/local/bin/runghc7.8.1 pointing to the binary /opt/ghc7.8.1/bin/runghc. Executing /usr/local/bin/runghc7.8.1 via typing runghc7.8.1 will now execute the runghc binary created in /opt (Note: sudo is not necessary if you create your symlink in $HOME/bin – it is just needed because root can create files under /usr)
Solution with bash aliases: Write in your $HOME/.bash_aliases (#Others: you can alternatively choose $HOME/.bashrc or $HOME/.profile depending of your system/preference) the following line:
alias runghc7.8.1='/opt/ghc7.8.1/bin/runghc'
Now typing runghc7.8.1 in your terminal is an shortcut (alias) for typing /opt/ghc7.8.1/bin/runghc and will execute this binary.
Note, that with this solution typing runghc7.8.1 will just work, when you typed it into your terminal. There are cases, when it does not work (for example calling runghc7.8.1 in a script).

How do I get vim's :sh command to source my bashrc?

Whenever I start a shell in vim using :sh, it doesn't source my ~/.bashrc file. How can I get it to do this automatically?
See :help 'shell'. You can set this string to include -l or --login, which will source your .bashrc file. So, you might have a line like this in your .vimrc:
set shell=bash\ --login
Note that this will alter everything that invokes the shell, including :!. This shouldn't be much of a problem, but you should be aware of it.
The value of this command can also be changed by setting the $SHELL environment variable.
If it doesn't source your .bashrc file, it may still source your .bash_profile file. I usually make one of them a symlink to the other. If your .bashrc performs some particularly odd one-time operations, you may have to edit it to only perform those operations with a login shell, but I've never had problems with it.
~/.vimrc
cmap sh<CR> !bash --login<CR>
If you quickly enter "sh<Enter>" in command-line, you can start bash with sourcing ~/.bashrc. So dirty.

Resources