Tag Info

Hot answers tagged

25

You didn't specify which shell you are using, so let this be excuse to advertise zsh. Yes, we do have more history for cd, namely cd -2, cd -4 etc. Very convenient is cd -TAB, especially with completion system and colors enabled: This is what I have in .zshrc: setopt AUTO_PUSHD setopt PUSHD_MINUS setopt CDABLE_VARS zstyle ':completion:*:directory-stack' ...


24

The command you are looking for is pushd and popd. You could view a practical working example of pushd and popd from here. mkdir /tmp/dir1 mkdir /tmp/dir2 mkdir /tmp/dir3 mkdir /tmp/dir4 cd /tmp/dir1 pushd . cd /tmp/dir2 pushd . cd /tmp/dir3 pushd . cd /tmp/dir4 pushd . dirs /tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1


23

With (t)csh, bash or zsh history expansion you could write: vagrant up && !#:0 ssh But, seriously, you wouldn't


23

Your confusion stems from the fact that many popular languages (especially C-based ones) stop evaluating && sequences when 0 is encountered, because 0 is considered false and everything else is true. In Bash, however, that's not the case. By convention, in POSIX systems (and all other Unix-like systems), return code 0 is considered SUCCESS (there was ...


19

zsh like most modern shells have a choice between two different keyboard mappings for command-line editing: a vi one and an emacs one. In some shells (like tcsh or readline-based ones like bash), the emacs one is the default and probably the one you expect. With zsh, you get emacs mode by default unless $EDITOR or $VISUAL contains vi (if you're a ...


14

Oooh, I found an explanation. To quote the relevant part: The zsh shell comes with (more than one) great feature(s), such as remote tabcompletion. If you for example want to copy a file over scp, simply hit tab at any part of the filename on the remote host. zsh is able to establish an ssh session on the background, and fetch the related information ...


13

You could do: eval vagrant\ {'up &&',ssh} but that would not make it easier. You could always make a function doall() { cmd=$1; shift for arg do "$cmd" "$arg" || return done } And then: doall vagrant up ssh


11

Yes, this happens because it is a "partial line". And by default zsh goes to the next line to avoid covering it with the prompt. When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a "%" for a normal user or a "#" for root. If set, the shell parameter PROMPT_EOL_MARK can be used to ...


10

To answer your question regarding "more history". No the cd - feature in Bash only supports a single directory that you can "flip" back to. As @Ramesh states in his answer. If you want a longer history of directories you can use pushd and popd to save a directory or return to a previous one. You can also see the list of what's currently in the stack with ...


8

The best way, to look at zsh documentation is using info. If you run info zsh, you can use the index (think of a book's index) to locate the section that describes the zmodload command. Press i, then you can enter zmo and press Tab. You'll get straight to the zmodload builtin description which will tell you all about it. In short, zmodload -F loads the ...


8

Different, documented behavior in zshmisc A list is a sequence of zero or more sublists, in which each sublist is terminated by ;, &, &|, &!, or a newline. This terminator may optionally be omitted from the last sublist in the list when the list appears as a complex command inside (...) or {...}. When a sublist is terminated ...


8

List all shell variables bash : use set -o posix ; set. The POSIX options is there to avoid outputing too much information, like function definitions. zsh : use typeset Shell variables and environment variables An environment variable is available to exec()-ed child processes (as a copy. if parent process change the variable, the childs are not aware of ...


8

Because there's no command called emacs -nw. There's a command called emacs to which you can pass a -nw option. To store commands, you generally use functions: foo() emacs -nw "$@" foo ... To store several arguments, you generally use arrays: foo=(emacs -nw) $foo ... To store a string containing several words separated by spaces and have it split on ...


8

The purpose of ${1+"$@"} for portability. POSIX defined $@ would expand to nothing if there're no positional arguments. But original Bourne shell (/bin/sh in Solaris 10 and before) would expand it to empty string "". Using ${1+"$@"} is a work around for this, since when "$@" only expanded if $1 was set. Unfortunately, this construct doesn't work in zsh 3.x ...


8

The return value from commands are different from typical boolan values. 0 is success when executing a command, anything else is failure. && expects 0 to me success here for that reason.


7

POSIX and Hyphens According to the POSIX standard, a function name must be a valid name and a name can consist of: 3.231 Name In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit. A hyphen is not listed among the characters ...


7

With zsh, use glob qualifiers: mv home*(.) dst moves only regular files. While mv home*(^/) dst moves files of any type except directories. mv home*(^-/) dst would also exclude symlinks to directories.


7

When you press Ctrl+Z in a terminal, this causes the foreground process group to receive the signal SIGTSTP (assuming the terminal is in cooked mode and the default key bindings are in place). If the process hasn't set a signal handler for SIGTSTP, this causes the process to be suspended (and even if the process has set a signal handler, it usually only does ...


7

This is very well known problem which is even described in zsh manual under chapter ALIASING (see man zshmisc). The recomended way of dealing with it is to use function instead of alias: foo() { ls; } ; foo or even better in case of ls: foo() { ls -- "${@:-.}"; } ; foo ps. semicolon at the end of the function definition (list) and spaces are not ...


7

You can not do it. Because aliases were expanded only after history expansion and entire line was read in one go, so when foo was executed, the alias expansion process was gone, it's too late for the shell to recognize new alias. The best way you can do is defining alias in .zshrc or using function like jimmij's answer or using eval: alias foo=ls; eval ...


6

You can install and use my dirhistory utility for bash. Basically, it's a daemon that collects directory changes from all your shells, and a Cdk program that displays the history and lets you pick any directory to switch to (so you're not limited to a stack).


6

I'll stick to scripting features. Rich interactive features (command line edition, completion, prompts, etc.) tend to be very different, achieving similar effects in wholly incompatible ways. What features are in zsh and missing from bash, or vice versa? gives a few pointers on interactive use. The closest thing to bash would be ATT ksh93 or mksh (the Korn ...


6

No need for ls here. It's the shell that lists the directory content by expanding the *.sql glob. On a GNU or FreeBSD system: printf '%s\0' *.sql | sort -nz | xargs -r0 cat -- (using \0 instead of \n together with -z/-0 makes sure it also works with file names containing newline characters). Or if you have zsh: cat ./*.sql(.n) (The n glob qualifier ...


6

Both are wrong with the zsh default option settings. You can easily see what's going on by using echo as the command instead of mv. Interactively, it looks like you have the null_glob option set. According to the zsh documentation that option is not set by default. What happens with that option unset depends on whether another option, nomatch, is set or ...


6

Having a global keybind to disown the foreground process is impossible: Keystrokes are received by the foreground process, not by the shell. You need to first suspend it with Ctrl+z if you want to disown it. However, turns out there's a zsh option to speed up disowning then continuing: With setopt AUTO_CONTINUE, disown will automatically also send SIGCONT. ...


5

In a shell command like PATH=~/bin:/opt/texbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games the tilde is expanded to your home directory when the shell command is executed. Thus the resulting value of PATH is something like /home/theconjuring/bin:/opt/texbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games. Make sure that the tilde ...


5

You've made grep an alias for grep $GREP_OPTIONS. Don't do that: the GNU grep command itself parses the GREP_OPTIONS environment variable. If you want to put options to a command in a variable, make that variable an array, and don't export it (you can't export arrays anyway, environment variables have string values only). LS_OPTIONS=(--color=auto -q) alias ...


5

There is built-in command functions in zsh for this purpose functions k.pstree.n For example in case of my preexec function: $ functions preexec preexec () { local cmd=${1:-} cmd=${cmd//\\/\\\\} [[ "$TERM" =~ screen* ]] && cmd="S $cmd" inf=$(print -Pn "%n@%m: %3~") print -n "\e]2;$cmd $inf\a" cmd_start=$SECONDS }


5

The result of a command substitution is broken into words using characters from IFS as separators, unless the command substitution is in double quotes. Thus " ABC" is split into a list of words, which contains one element "ABC". Use echo "$(echo " ABC")" to use the result of the command susbtitution as a string. Unlike normal Bourne/POSIX-style shells, ...


5

This feature can be tuned with ZLE_REMOVE_SUFFIX_CHARS and ZLE_SPACE_SUFFIX_CHARS shell parameters. If the ZLE_REMOVE_SUFFIX_CHARS variable is set, it should contain a set of characters that, when typed, will cause automatic suffixes from the completion to be removed. If ZLE_REMOVE_SUFFIX_CHARS is unset, the default behaviour equates to ...



Only top voted, non community-wiki answers of a minimum length are eligible