Shortening my prompt in Zsh - shell

I'm having a lot of trouble getting zsh to shorten my prompt. I'm currently using zsh with the agnoster theme and oh-my-zsh package manager.
My prompt currently gets annoyingly long during work, usually around 110 characters, taking up the entire length of my terminal, which is just not very aesthetically pleasing.
I've looked at a few other people's .zshrc's and attempts to modify their prompt, but nothing seems to work in mine. I've tried copying many, many things into my .zshrc and have seen no effects.
My most recent attempt was to try to copy the prompt block from https://stackoverflow.com/a/171564/2416097
Nothing. Even when I disabled my theme while having this block included, the prompt is still at full length.
Additionally, I can't seem to find any simple or straightforward guides on how to format my prompt in the first place. Most of the results I found while searching only yielded long format strings without explanation or instruction on use.
Any help appreciated!

Old question, I know, but as an alternative solution I just discovered powerlevel9k, an extension of agnoster (they appear practically identical bar a fair few tweaks), which has this functionality built in.
Just set it as your zsh theme, then in .zshrc set
POWERLEVEL9K_SHORTEN_DIR_LENGTH=2
which ensures that only two directories are listed.
Alternate options are outlined in the readme.

First you would have to copy the theme into a different one in order to customize it to your liking.
Copy agnoster.zsh-theme to e.g. mytheme.zsh-theme and select it in your .zshrc
Then modify the theme to your liking
I looked at the agnoster theme and found a place where you could save space.
prompt_dir() {
prompt_segment blue $CURRENT_FG ' %~ '
}
could be changed to
prompt_dir() {
prompt_segment blue $CURRENT_FG ' %25<...<%~%<< '
}
This will truncate your path to 25 characters and replacing more with ...
How this works is described in the zsh manual (linked below).
Short explanation is:
%25<...< will truncate everything that is longer than 25 characters to ...
%<< will basically tell zsh that anything after this should not be truncated (limiting the truncation to the path part)
I leave it to you to find more places where you can save space by.
And for more customization needs take a look at zsh: 13 Prompt Expansion

add this to ~/.zshrc
prompt_dir() {
prompt_segment blue $CURRENT_FG '%2~'
}
Explanatory note: %<N>~ will limit the number of path segments leading up to the current directory to N. I.e. %2~ will show only two last segments: the current directory and its parent directory.

In case you stumbled upon this post coming from macOS' zsh like me and want to shorten the prefix/prompt there, I found this article helpful: https://www.makeuseof.com/customize-zsh-prompt-macos-terminal/.
I wanted to remove the name of my MacBook model from the prompt and was able to do so with the following steps:
vim .zshrc
append PROMPT="%n %1~ %# "
Verify with source .zshrc or simply open a new terminal
Before
chris#Christophers-MacBook-Pro ~ %
After
chris ~ %

In the Zsh themes folder you should search for this file agnoster.zsh-theme , open with an editor and change this piece of code :
prompt_dir() {
prompt_segment blue $CURRENT_FG ' %~ '
}
with this :
prompt_dir() {
prompt_segment blue $CURRENT_FG "%c"
}
This will prompt the current directory instead of the full path.

Related

How do you make the terminal $PS1 indent when wrapping to a second line?

I'm trying to make my PS1 more readable and one issue I'm currently having is that my file prompts are too long. I don't want to shorten them however, since that would mean loss of exactitude.
My solution so far is to simply send the prompt after the file location to a new line. But this still doesn't look clean, as the file location is cut off mid filename and then continued underneath the username.
What I'm trying to do is have it look something like this in the end:
gingerbread: ~/file/file/file/file/file/
file/file/file/file.txt
But what I have right now is this:
gingerbread: ~/file/file/file/file/file/fil
e/file/file/file.txt
N.B. I already have a function doing the name prompt, cos I'm an idiot and made the username show up in all the colors of the rainbow... Here's the code for that in case it helps:
fancyName()
{
PERSPS='';
NAME='gingerbread';
LENGTHNAME=${#NAME};
for i in `seq 1 $LENGTHNAME`;
do
NUM=$((($i % 8) + 30));
PERSPS="$PERSPS\[\033[01;$NUM";
PERSPS+="m\]${NAME:$i-1:1}";
done
PERSPS="$PERSPS\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\n\[$\] ";
}
I then use $PERSPS as the $PS1. The whole thing just cycles through colors basically.
How do I control where the prompt wraps and where it wraps to?
Thanks in advance,
-Me.

How to edit file content using zsh terminal?

I created an empty directory on zsh and added a file
called hello.rb by doing the following:
echo 'Hello, world.' >hello.rb
If I want to make changes in this file using the terminal
what's the proper way of doing it without opening the file
itself using let's say TextEditor?
I want to be able to make changes in the file hello.rb strictly
by using my zsh terminal, is this at all possible?
Zsh is not a terminal but a shell. The terminal is the window in which the shell executes. The shell is the text program prompting you commands and executing them.
If you want to edit the file within the terminal, then using vim, nano, emacs -nw or any other text-mode text editor will do it. They are not Zsh commands, but external commands that you can call from Zsh or from any other shell.
If you want to edit the file within Zsh, then use zed. You will need to run once (in ~/.zshrc)
autoload zed
and then you can edit hello.rb using:
zed hello.rb
(exit and save with Control-j)
You have already created and edited the file.
To edit it again, you can use the >> to append.
For example
echo "\nAnd you too!\n" >> hello.rb
This would edit the file by concatenating the additional string.
Edit, of course, by your use and definition of 'changing' a file, this is the simplest way to do so using the shell.
In a normal way, though you probably want to use a terminal editor.
Zed is a great answer, but to be even more stripped down - for a level of editing that even a script can do - zsh can hand all 256 characters/byte-values (including null) in variables. This means you can edit line by line or chunk by chunk almost any kind of file data directly from the command-line. This is approximately what zed/vared does. If you have a current version with all the standard modules included, it is a great benefit to have zsh/mapfile or zsh/system loaded so that you can capture any of the characters that are left out by command-expansion (zed uses $(<$file) to read a file to memory). Here is an example of a way you could use this variable manipulation method:
% typeset -T Buffer buffer $'\n'
% typeset -T Edit edit $'\n'
It is most common to use newline to divide a text file one wishes to edit.
This handy feature will make zsh give you full access to one line or a range of lines at a time without unintentionally messing with the data.
% zmodload zsh/mapfile
% Buffer=$mapfile[path/to/file]
Here, I use the handy mapfile module because I can load the contents of a file byte-for-byte. Alternately you can use % Buffer="$(<path/to/file)", like zed does, but you will always have the trailing newlines removed and other word splitting is possible with a typo or environment variation, so the simplicity of the module's method is best. When finished, you save the changes by simply assigning the $Buffer value back to the $mapfile[file] or use a more classic command like printf '%s' $Buffer >path/to/file (this is exact string writing, byte-for-byte, so any newlines or formatting you added back will be written).
You transfer the lines between Buffer and Edit using the mapped arrays as follows, however, remember that in its simplest form assigning one array to another drops elements that are completely empty (one \n \n two \n three becomes one \n two \n three). You can suppress this empty-element removal by quoting the input array and adding an '#' symbol to its index "$buffer[#]", if using the whole array; and adding the '#' symbol to the flags if using a range of the array "${(#)buffer[2,50]}". Preserving empty lines can be a bit troublesome for typing, but these multiple arrays should only be used in a script or function, since you can just edit one line at a time from the command line with buffer[54]="echo This is a newly written line."
% edit=($buffer[50,70])
...
% buffer[50,70]=($edit)
This is standard Zsh syntax, that means in the ... area you can edit and manipulate the $edit array of lines or the $Edit scalar block of text all you want, including adding more lines or taking some away. When you add the lines back into $buffer it will replace the specified block of lines (50-70) with the new lines, automatically expanding or reducing the other array elements to accommodate the reintegrated lines. -- Because of the dynamic array accommodations, you can also just insert whatever you need as a new line like this buffer[40]=("new string as new line" "$buffer[40]"). This inserts it before the index given, while swapping the order of the elements ("$buffer[40]" "new string as new line") inserts the new line after the index given. Either will adjust all following elements, including totally empty elements, to their current index plus one.
If you wanted to re-write the zed function to use this method in some complex way like: newzed /path/to/file [start-line] [end-line], that would be great and handy too.
Before I leave, I wanted to mention that using vared directly, once you have these commands typed on the interactive terminal, you may find it frustrating that you can't use "Enter" for inserting or appending new lines. I found that with my terminal and Zsh version using ESC-ENTER worked well, but I don't know about older versions (Mac usually comes stocked with a not-most-recent version, if my memory is right). If that doesn't work, you may have to do some documentation digging to learn how to set up your ZLE (Zsh Line Editor, a component of Zsh) or acquire a newer version of Zsh. Also, some other shells, when indexing a scalar variable may count by the byte because in ascii and C a byte is the same as a character, but Zsh supports UTF8 and will index a scalar string by the UTF8 character unless you turn off the shell option multibyte (on by default). This will help with manipulating each line if you need to use the old byte-character indexing. Also, if you have a version of Zsh that for whatever was not compiled with zsh/mapfile or zsh/system, then you can achieve a similar effect using number of options to the read builtin, like <path/to/file |read -u 0 -k $[5 * 2**20] -r -s Buffer ||(($#Buffer)). As you can see here, you have to make the read length big enough to accommodate the file's size or it will leave off part of the file, and the read return code will nearly always be an error because of not being able to read the full length of the string. We fix this with ||(($#Buffer)), but this builtin was simply not meant to handle large scale byte manipulation efficiently, so what you see is what you can get from it.

Installing emacs plugins on windows

I already looked through other topics, but I still couldn't find a solution. I'm trying to install "nxhtml" plugin for Emacs in windows 7. I already setup my "HOME" environment variable as "C:\". So, my .emacs.d folder is there, and I put the nxhtml in there and added the following line to my "_emacs.d" file, as the readme says:
(load "C:\.emacs.d\nxhtml\autostart.el")
But it doesn't load.
I also tried putting:
(add-to-list 'load-path "C:\.emacs.d\nxhtml")
(load "autostart.el")
But to no avail... can anyone shed some light here? tnx.
A number of points here:
Firstly, _emacs.d is not a default file name for your init file, ie emacs will not load it automatically. Try ~/.emacs.d/init.el, or ~/.emacs instead.
Secondly, Windows 7 has a feature where it prevents programs from writing to certain system directories, but for backwards compatibility for the many old programs that do this, rather than causing them to fail, it silently redirects the write elsewhere, in an application specific directory. C:\ is one of those directories, so setting your HOME to point there is asking for trouble.
Thirdly, see the other response about backslash being an escape character in Lisp strings.
\ is special in the (double-quote) read syntax for strings, as certain characters take on a new meaning when prefixed by a backslash (e.g. \n is a newline, \t is a tab, and \" is a double-quote character). When the following character does not have any special meaning in conjunction with the backslash, that character is used verbatim, and the backslash is ignored.
"C:\.emacs.d\nxhtml\autostart.el" is actually the string:
C:.emacs.d
xhtml^Gutostart.el
To include a \ in the string you need to write \\
However, although it will understand the backslashes, Emacs is nowadays consistent across all platforms in allowing / as a directory separator1; so just do that instead.
1 and the obsolete directory-sep-char variable has been removed entirely.

Custom Oh My Zsh theme: long prompts disappear / cut off

I had a go at making my own Oh My Zsh theme earlier. All is well, except when I type long lines in the prompt (any longer than the line seen below), the line disappears. The line re-appears if I resize the window, however.
Is there something in my theme that is causing this to happen?
If I type an additional character and then erase one, the cursor appears at the edge of the window.
You can view the code for the theme here. Here’s the bit I think we are concerned with:
# Build the prompt
PROMPT='
' # Newline
PROMPT+='${style_user}%n' # Username
PROMPT+='${style_chars}#' # #
PROMPT+='${style_host}%m' # Host
PROMPT+='${style_chars}: ' # :
PROMPT+='${style_path}%c ' # Working directory
PROMPT+='$(git_custom_status)' # Git details
PROMPT+='
' # Newline
PROMPT+='${style_chars}\$${RESET} '
Incidentally, your link is broken, highlighting one of the issues with posting a link to code instead of code itself - any future viewers of your question can't get a full picture.
I think your problem is that the 'color' characters you use should be escaped in a pair of %{...%}:
%{...%}
Include a string as a literal escape sequence. The string within the braces
should not change the cursor position. Brace pairs can nest.
Using your latest commit on github, I don't see this issue - did you fix it? However, I'm seeing some issues with cursor placement and line-drawing, particularly with TAB. When pressing TAB, the cursor is moved up one line:
Pressed TAB here.
Pressed TAB here.
The PROMPT is being re-drawn 'up' one line every time. This is fixed by encapsulating the color codes within the %{...%}:
# Solarized Dark colour scheme
BOLD="%{$(tput bold)%}"
RESET="%{$(tput sgr0)%}"
SOLAR_YELLOW="%{$(tput setaf 136)%}"
SOLAR_ORANGE="%{$(tput setaf 166)%}"
SOLAR_RED="%{$(tput setaf 124)%}"
SOLAR_MAGENTA="%{$(tput setaf 125)%}"
SOLAR_VIOLET="%{$(tput setaf 61)%}"
SOLAR_BLUE="%{$(tput setaf 33)%}"
SOLAR_CYAN="%{$(tput setaf 37)%}"
SOLAR_GREEN="%{$(tput setaf 64)%}"
SOLAR_WHITE="%{$(tput setaf 254)%}"
I'm not 100% sure without the original ~/.zshrc, but this should improve your prompt a little. :)
Apart from the orange, you can also use a terminal-based Solarized profile and the zsh colors, which might be more portable. I couldn't get the orange right without tput, though.
#autoload colors && colors
#SOLAR_YELLOW="%{$fg[yellow]%}"
#SOLAR_ORANGE="%{$(tput setaf 166)%}"
#SOLAR_RED="%{$fg[red]%}"
#SOLAR_MAGENTA="%{$fg[magenta]%}"
#SOLAR_VIOLET="%{$fg_bold[magenta]%}"
#SOLAR_BLUE="%{$fg[blue]%}"
#SOLAR_CYAN="%{$fg[cyan]%}"
#SOLAR_GREEN="%{$fg[green]%}"
#SOLAR_WHITE="%{$fg[white]%}"

How does one align code (braces, parens etc) in vi?

How do you prettify / align / format code in vi? What is the command?
I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.
x
These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...
Automatic Indentation
To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:
:filetype indent on
You can disable auto-indentation with
:filetype indent off
Automatically adjusting/correcting indentation
In general, ={motion} will align code to an indentation level.
== align the current line
=i{ align the inner block
=% align to the matching parenthesis/bracket under the cursor
=14j or 14== align the next 14 lines
=G align to the end of the file
vG= same thing, align to the end of the
file (but using visual mode)
vjjj= align four lines (using visual mode)
Manual indentation
If vim is not guessing the indentation level correctly, there are two ways to change it:
If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D
Aligning equals signs, etc
If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294
Adjusting indentation/tab sizes
If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)
:set expandtab
To set how many spaces equal a tab, I usually do this:
:set expandtab softtabstop=3 tabstop=3 shiftwidth=3
tabstop - how many columns a tab counts for (affects display of existing tab characters)
shiftwidth - controls reindentation size with << and >>, among other commands.
softtabstop - how much space to insert when you press the tab key
expandtab - expand tab keys to spaces
But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:
function! Ktabs(tabsize)
execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize
"set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize
endfunction
noremap <leader><Tab> :call Ktabs(3)<Left>
If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:
:retab
={motion}
:h =
P.S. You shouldn't use vi if vim is available.
If manually adjusting indents I will open a visual block with V on the first or last line I want to re-indent, move to the brace containing the block, goto the other brace with % then shift the line with > or <
If indents are off by a lot I will shift everything all the way left with < and repeat it with . and then re-indent everything.
Another solution is to use the unix fmt command as described in Your problem with Vim is that you don't grok vi., {!}fmt

Resources