Color code of ls function - unix

For an exercice I try to code the ls function.
I'm copying the -G option.
This option will color the name of the file depending of the type.
Until now, I understood than :
- Symbolic link are in magenta
- directory is in cyan
Does any one know which are the other colors used and for what kind of file ?
I'm also looking for the background color.
Kind regards

I showed a script in How to remove dir background in ls -color output which colorizes the output of dircolors -p (and would be useful in this question, since it preserves the comments which explain what's being colored).

The colors used by ls are determined by the LS_COLORS environment variable; or else by a builtin database. The environment variable LS_COLORS is normally set by evaluating the output of dircolors; see man dircolors for details. Use dircolors -p | less to see the current values.

Related

Is it possible to keep the output of less on the screen after quitting?

I'm using oh-my-zsh which pipes the output of some functions like git diff and git log into less, whilst this is great for reading the output in the terminal. If I need to refer back to it it isn't possible after quitting with :q
Is there an option to preserve the current view on the file in my terminal after quitting?
Secondly, If there is an option where would I need to edit my oh-my-zsh config to ensure anything piped to less passes this option?
To prevent less from clearing the screen on exit you can start it with the option -X:
less -X FILE
If you want to pass this option automatically to every instance of less, you can set the LESS environment variable accordingly in your ~/.zshrc:
export LESS="-X"
Note:
If your shell has syntax coloring enabled, the -X option will cause your less output to display those color change escape sequences as inline ESC text.
This can be fixed by also passing the raw-control-chars display option, -r. For example:
export LESS="-Xr"
This also includes instances where less is started by another program, for example man. If you want to disable this option for a single command, you can just prepend LESS=. For example
LESS= man less
For Git specifically, this can be handled with the following
git config --global color.ui true
git config --global core.pager 'less -Xr'

complete command's parameters with oh-my-zsh

Is there a way to make zsh complete parameters of commands (just like the fish shell do). For example, when I type ls -- and use TAB it will pop up a list of all ls parameters.
I know that I can use fish shell for this, and I know that fish shell is amazing, but there is a lot of missing functionalities in the fish shell. That's why I am looking to bring this parameter completion in zsh.
Try with single hyphen, like ls -. All built in commands are working fine. Btw for custom oh-my-zsh plugin autocompletion, it depends on plugin configuration.
oh-my-zsh has over 200 plugins for different commands. But unfortunately the auto completions are all hand made. So there will be never auto complete function for every command.

oh-my-zsh alias color hightlight

I started use zsh with plugin named "oh-my-zsh", and set up my personal alias in ~/.zshrc.
alias ls='ls -aF'
and, then
source ~/.zshrc
but the command ls doesn't make the output highlighted. (The command works properly though.)
I don't really get why.
Any ideas?
By default the output of ls is not colored and neither -a (also show hidden files) nor -F (append indicator for file type) does change that.
In order to get colored output from ls you need to pass the --color:
ls -aF --color
As you are planning to use it in an alias it would be a good idea to set --color=auto so that colors are only used when printing to standard output but not when redirecting the output (for example with > SOMEFILE or | SOMECOMMAND):
alias ls='ls -aF --color=auto'

Change to xth directory terminal

Is there a way in a unix shell (specifically Ubuntu) to change directory into the xth directory that was printed from the ls command?
I know you can sort a directory in multiple ways, but using the output from ls to get the xth directory?
An example shell:
$ ls
$ first_dir second_dir third_really_long_and_complex_dir
where I want to move into the third_really_long_and_complex_dir by passing 3 (or 2 in proper array format).
I know I could simply copy and paste, but if I'm already using the keyboard, it would be easier to type something like "cdls 2" or something like that if I knew the index.
The main problem with cd in an interactive session is that you generally want to change the current directory of the shell that is processing the command prompt. That means that launching a sub-shell (e.g. a script) would not help, since any cd calls would not affect the parent shell.
Depending on which shell you are using, however, you might be able to define a function to do this. For example in bash:
function cdls() {
# Save the current state of the nullglob option
SHOPT=`shopt -p nullglob`
# Make sure that */ expands to nothing when no directories are present
shopt -s nullglob
# Get a list of directories
DIRS=(*/)
# Restore the nullblob option state
$SHOPT
# cd using a zero-based index
cd "${DIRS[$1]}"
}
Note that in this example I absolutely refuse to parse the output of ls, for a number of reasons. Instead I let the shell itself retrieve a list of directories (or links to directories)...
That said, I suspect that using this function (or anything to this effect) is a very good way to set yourself up for an enormous mess - like using rm after changing to the wrong directory. File-name auto-completion is dangerous enough already, without forcing yourself to count...

UNIX: Strange output if piped to less

If I execute ls command with pipe to less, I get strange output
ESC[00mESC[00mfile1.ccESC[00m
ESC[00file2.ccESC[00m
ESC[00file3.ccESC[00m
(means ESC string in between).
Without ls, the output is:
file1.cc file2.cc file3.cc
How to correct this?
I'm guessing that you have the --color=always option to ls set, either through an alias, functions or the LS_COLORS environment variable and ls is sending color directives to a non-terminal (that is, your pipe to less).
Use less -R or set the LESS environment variable to -R.
What you're seeing are ANSI escape sequences for setting colors. Run ls --color=no.
You need to make less output raw control characters using less -r.

Resources