Color setting works for only for "HELP" tab in man page, dosen't work for the entire man page - pager

I'm trying to colorize the manual page for better visaul effect, but the setting(export some env variable in .bashrc) fails to work for man page, and it only work when I push h to get SUMMARY OF LESS COMMANDS
I put these setting in my .bashrc file:
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
export PAGER='less'
And in /etc/man_db.conf, the default setting for man is:
#DEFINE pager less -s
#DEFINE cat cat
#DEFINE tr tr '\255\267\264\327' '\055\157\047\170'
#DEFINE grep grep
#DEFINE troff groff -mandoc
#DEFINE nroff nroff -mandoc
It seems that man dosen't generate termcap control sequence correctly when invoking troff. ( or groff, nroff? I'm not sure ) Thus the less pager can't generate the color escape sequence.
Did I get anything wrong? Or is there something I can try to fix this problem?
terminal emultator: xterm
operating system: Fedora 22
(The vocabulary may be odd, English is not my native language, sorry for that)
Here are the screenshot showing this weird situation:
(I can not include image due to low reputation.)
man: no color rendered
help tab in man: color rendered
Thanks for your helps.

I've installed Fedora 22, reproduced your problem and managed to solve it with the following command:
export GROFF_NO_SGR=1
I found the solution here: https://unix.stackexchange.com/questions/6010/colored-man-pages-not-working-on-gentoo

Related

Replacing a fixed part of PWD in a tcsh prompt

My prompt is currently displayed like here:
[Aug-27 14:36] /x/y/z/w/u/v/dir1/dir2/dir3>
What I would like to do is replace the constant partial-path of the current working directory
/x/y/z/w/u/v
with
$WORK
so eventually what will be displayed is
[Aug-27 14:36] $WORK/dir1/dir2/dir3>
/x/y/z/w/t/u is always the same path from which I usually do my work and for which I have a local variable $WORK set (very similar to the home ~ idea).
A straight-forward solution will be most-welcomed as I really don't know much about setting a shell.
Just put those lines into ~/.tcshrc:
set WORK='/x/y/z/w/u/v'
set dollar='$'
alias precmd 'printf "%b" "\e[36m"; date +"[%b-%d %H:%M] " | tr -d "\n"; [ `expr "$PWD" : "$WORK*"` -gt 0 ] && printf "%s" "$dollar$PWD" | sed "s|$WORK|WORK|" - || printf "%s" "$PWD"'
set prompt='%#%{\e[0;0m%} '
# The default tcsh ^L binding for screen clearing does not run precmd.
# This one does.
bindkey -s "^L" "clear\n"
precmd is a command, which is run before a prompt is shown to you. You can use it to customize your prompt using other commands available on your system.
When it comes to colors, you can add them using those special color sequences like \e[36m (more details here). In the my example I turned on non-bold cyan for the whole prompt by prepending printf "%b" "\e[36m"; to the definition of precmd. You add your own colors this way, just put that a similar printf command somewhere in there. I turned off colors (bringing back the default text color of the terminal) by appending %{\e[0;0m%} to the prompt, end of which happens to be set by the prompt variable. I'm using %{...%} because this is how you change colors inside when setting the prompt variable. So basically you should use printf "%b" "..."; for the precmd alias and %{...%} for the prompt variable.
I used those for reference:
Setting a part of a PWD as a prompt and keeping a variable updated (SO)
Customizing your shell prompt (www.nparikh.org)
Setting the current path in the command prompt in (t)csh (www.unix.com)
How to write If-else statement in one line in csh? (SO)
How to get a list of tcsh shortcuts? (Unix SE)
Tested on Ubuntu 17.04 with tcsh --version returining tcsh 6.20.00 (Astron) 2016-11-24 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,nd,color,filec.
This is just a custom prompt that probably could give you an idea about how to create/improve yours:
set COLOR1="%{\e[0;32m%}"
set COLOR2="%{\e[0;33m%}"
set COLOR3="%{\e[0;36m%}"
set COLOR4="%{\e[0;0m%}"
set COLOR5="%{\e[0;33m%}"
set prompt="$COLOR2\[$COLOR3%n#%M$COLOR2\:$COLOR1%~$COLOR2\] [%p %d]\n$COLOR5>$COLOR4 "
set promptchars = "%#"
The prompt will be something like:
[user#host:/current/dir] [current date]
>
Like the COLOR variables you could set WORK.
Also, this answer could help: https://stackoverflow.com/a/20871994/1135424

How to add html attributes and values for all lines quickly with vim and plugins?

My os:debian8.
uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
Here is my base file.
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
I want to create a html file as bellow.
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
Every line was added href and id attributes,whose values are line content pasted .html and line content itself correspondingly.
How to add html attributes and values for all lines quickly with vim and plugins?
sed,awk,sublime text 3 are all welcomed to solve the problem.
$ sed 's:.*:&:' file
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
if you want to do this in vi itself, no plug-in neccessary
Open the file, type : and insert this line as the command
%s:.*:&
it will make all the substitutions in the file.
sed is the best solution (simple and pretty fast here) if your are sure of the content, if not it need a bit of complexity that is better treated by awk:
awk '
{
# change special char for HTML constraint
Org = URL = HTML = $0
# sample of modification
gsub( / /, "%20", URL)
gsub( /</, "%3C", HTML)
printf( "%s\n", URL, Org, HTML)
}
' YourFile
To complete this easily in Sublime Text, without any plugins added:
Open the base file in Sublime Text
Type Ctrl+Shift+P and in the fuzzy search input type syn html to set the file syntax to HTML.
In the View menu, make sure Word Wrap is toggled off.
Ctrl+A to select all.
Ctrl+Shift+L to break selection into multi-line edit.
Ctrl+C to copy selection into clipboard as multiple lines.
Alt+Shift+W to wrap each line with a tag-- then tap a to convert the default <p> tag into an <a> tag (hit esc to quit out of any context menus that might pop up)
Type a space then href=" -- you should see this being added to every line as they all have cursors. Also you should note that Sublime has automatically closed your quotes for you, so you have href="" with the cursor between the quotes.
ctrl+v -- this is where the magic happens-- your clipboard contains every lines worth of contents, so it will paste each appropriate value into the quotes where the cursor is lying. Then you simply type .html to add the extension.
Use the right arrow to move the cursors outside of the quotes for the href attribute and follow the two previous steps to similarly add an id attribute with the intended ids pasted in.
Voila! You're done.
Multi-line editing is very powerful as you learn how to combine it with other keyboard shortcuts. It has been a huge improvement in my workflow. If you have any questions please feel free to comment and I'll adjust as needed.
With bash one-liner:
while read v; do printf '%s\n' "$v" "$v" "$v"; done < file
(OR)
while read v; do echo "$v"; done < file
Try this -
awk '{print a$1b$1c$1d}' a='' d='' file
home
help
variables
compatibility
modelines
searching
selection
markers
indenting
reformatting
folding
tags
makefiles
mapping
registers
spelling
plugins
etc
Here I have created 4 variable a,b,c & d which you can edit as per your choice.
OR
while read -r i;do echo ""$i";done < f
home
help
variables
compatibility
To execute it directly in vim:
!sed 's:.*:&:' %
In awk, no regex, no nothing, just print strings around $1s, escaping "s:
$ awk '{print "" $1 ""}' file
home
help
If you happen to have empty lines in there just add /./ before the {:
/./{print ...
list=$(cat basefile.txt)
for val in $list
do
echo ""$val"" >> newfile.html
done
Using bash, you can always make a script or type this into the command line.
This vim replacement pattern handles your base file:
s#^\s*\(.\{-}\)\s*$#\1#
^\s* matches any leading spaces, then
.\{-} captures everything after that, non-greedily — allowing
\s$ to match any trailing spaces.
This avoids giving you stuff like home .
You can also process several base files with vim at once:
vim -c 'bufdo %s#^\s*\(.\{-}\)\s*$#\1# | saveas! %:p:r.html' some.txt more.txt`
bufdo %s#^\s*\(.\{-}\)\s*$#\1# runs the replacement on each buffer loaded into vim,
saveas! %:p:r.html saves each buffer with an html extension, overwriting if necessary,
vim will open and show you the saved more.html, which you can correct as needed, and
you can use :n and :prev to visit some.html.
Something like sed’s probably best for big jobs, but this lets you tweak the conversions in vim right after it’s made them, use :u to undo, etc. Enjoy!

Function to open a file and navigate to a specified line number

I have the output of recursive grep (actually ag) in a buffer, which is of the form filename:linenumber: ... [match] ..., and I want to be able to go to the occurrence (file and line number) currently under the cursor. This told me that I could execute normal-mode movements, so after extracting the file:line portion, I wrote this function:
function OpenFileNewTab(name)
let l:pair=split(a:name, ":")
execute "tabnew" get(l:pair, 0)
execute "normal!" get(l:pair, 1) . "G"
endfunction
It is supposed to open the specified file in a tab and then do <lineno>G, like I am able to do manually, to go to the specified line number. However, the cursor just stays on line 1. What am I doing wrong?
This question, by title alone, would be an exact duplicate, but it talks locating symbols in other files, while I already have the locations at hand.
Edit: My mappings for grep / ag are as follows:
nnoremap <Leader>ag :execute "new \| read !ag --literal -w" "<C-r><C-w>" g:repo \| :set filetype=c<CR>
nnoremap <Leader>gf ^v2t:"zy :execute OpenFileNewTab("<C-r>z")<CR>
To get my grep / ag results, I put the cursor on the word I want to search and enter <leader>ag, then, in the new buffer, I put the cursor on a line and enter <leader>gf - it selects from the start up to the second colon and calls OpenFileNewTab.
Edit 2: I'm on Cygwin, if it is of any importance - I doubt it.
Why don't you set &grepprg to call ag ?
" according to man ag
set grepprg=ag\ --vimgrep\ $*
set grepformat=%f:%l:%c:%m
" And then (not tested)
nnoremap <Leader>ag :grep -w <c-r><c-w><cr>
As others have said in the comments, you are just trying to emulate what the quickfix windows already provides. And, we are lucky vim can call grep, and it has a variation point to let us specify which grep program we wish to use: 'grepprg'.
Use file-line plugin. Pressing Enter on a line in the quicklist will normally open that file; file-line will make any filename of the form file:line:column (and several other formats) to open file and position to line and column.
I only found this (old) thread after I posted the exact same question on vi.stackexchange: https://vi.stackexchange.com/q/39557/44764. To help anyone who comes looking, I post the best answer to my question below as an alternative to the answers already given.
The gF command, like gf, opens the file in a new tab but additionally it also positions the cursor on the line after the colon. (I note the OP defines <leader>gf so maybe vim/neovim didn't auto-define gf or gF at the time this thread was originally created.)

Use LibreOffice to convert HTML to PDF from Mac command in terminal?

I'm trying to convert a HTML file to a PDF by using the Mac terminal.
I found a similar post and I did use the code they provided. But I kept getting nothing. I did not find the output file anywhere when I issued this command:
./soffice --headless --convert-to pdf --outdir /home/user ~/Downloads/*.odt
I'm using Mac OS X 10.8.5.
Can someone show me a terminal command line that I can use to convert HTML to PDF?
I'm trying to convert a HTML file to a PDF by using the Mac terminal.
Ok, here is an alternative way to do convert (X)HTML to PDF on a Mac command line. It does not use LibreOffice at all and should work on all Macs.
This method (ab)uses a filter from the Mac's print subsystem, called xhtmltopdf. This filter is usually not meant to be used by end-users but only by the CUPS printing system.
However, if you know about it, know where to find it and know how to run it, there is no problem with doing so:
The first thing to know is that it is not in any desktop user's $PATH. It is in /usr/libexec/cups/filter/xhtmltopdf.
The second thing to know is that it requires a specific syntax and order of parameters to run, otherwise it won't. Calling it with no parameters at all (or with the wrong number of parameters) it will emit a small usage hint:
$ /usr/libexec/cups/filter/xhtmltopdf
Usage: xhtmltopdf job-id user title copies options [file]
Most of these parameter names show that the tool clearly related to printing. The command requires in total at least 5, or an optional 6th parameter. If only 5 parameters are given, it reads its input from <stdin>, otherwise from the 6ths parameter, a file name. It always emits its output to <stdout>.
The only CLI params which are interesting to us are number 5 (the "options") and the (optional) number 6 (the input file name).
When we run it on the command line, we have to supply 5 dummy or empty parameters first, before we can put the input file's name. We also have to redirect the output to a PDF file.
So, let's try it:
/usr/libexec/cups/filter/xhtmltopdf "" "" "" "" "" my.html > my.pdf
Or, alternatively (this is faster to type and easier to check for completeness, using 5 dummy parameters instead of 5 empty ones):
/usr/libexec/cups/filter/xhtmltopdf 1 2 3 4 5 my.html > my.pdf
While we are at it, we could try to apply some other CUPS print subsystem filters on the output: /usr/libexec/cups/filter/cgpdftopdf looks like one that could be interesting. This additional filter expects the same sort of parameter number and orders, like all CUPS filters.
So this should work:
/usr/libexec/cups/filter/xhtmltopdf 1 2 3 4 5 my.html \
| /usr/libexec/cups/filter/cgpdftopdf 1 2 3 4 "" \
> my.pdf
However, piping the output of xhtmltopdf into cgpdftopdf is only interesting if we try to apply some "print options". That is, we need to come up with some settings in parameter no. 5 which achieve something.
Looking up the CUPS command line options on the CUPS web page suggests a few candidates:
-o number-up=4
-o page-border=double-thick
-o number-up-layout=tblr
do look like they could be applied while doing a PDF-to-PDF transformation. Let's try:
/usr/libexec/cups/filter/xhtmltopdfcc 1 2 3 4 5 my.html \
| /usr/libexec/cups/filter/cgpdftopdf 1 2 3 4 5 \
"number-up=4 page-border=double-thick number-up-layout=tblr" \
> my.pdf
Here are two screenshots of results I achieved with this method. Both used as input files two HTML files which were identical, apart from one line: it was the line which referenced a CSS file to be used for rendering the HTML.
As you can see, the xhtmltopdf filter is able to (at least partially) take into account CSS settings when it converts its input to PDF:
Starting 3.6.0.1 , you would need unoconv on the system to converts documents.
Using unoconv with MacOS X
LibreOffice 3.6.0.1 or later is required to use unoconv under MacOS X. This is the first version distributed with an internal python script that works. No version of OpenOffice for MacOS X (3.4 is the current version) works because the necessary internal files are not included inside the application.
I just had the same problem, but I found this LibreOffice help post. It seems that headless mode won't work if you've got LibreOffice (the usual GUI version) running too. The fix is to add an -env option, e.g.
libreoffice "-env:UserInstallation=file:///tmp/LibO_Conversion" \
--headless \
--invisible \
--convert-to csv file.xls

How can I define custom colors for use in ZSH prompt?

I'm having some difficulty configuring my zsh prompt. Specifically I would like the font to have the color defined by the hex code: #87afdf
Currently, I've set up the prompt as follows:
PROMPT='%B[%d]
➞ %b'
I've attempted to add colors in the following way:
autoload -U colors && colors
PROMPT='%{$fg[#87afdf]%}%B[%d]
➞ %b%{$reset_color%}'
But this only gives me the following gibberish:
$fg[#87afdf][/Users/gregory]
➞ $reset_color
Any ideas on how to proceed would be very much appreciated.
You have to use a 256-color palette. You can see the numerical values for each of the 256 colors in ZSH using the following command:
for code in {000..255}; do print -P -- "$code: %F{$code}Color%f"; done
The same for bash:
for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Color"; done
Solution for Oh-My-Zsh users
How to print available colors
As already mentioned you have to use a 256-color palette.
The easiest way to see which colors are available is to use the following command (as ZSH uses spectrum underneath):
spectrum_ls
it will print all available colors
...
How to use color in Oh-my-zsh theme
To use color in your theme you have to write it like $FG[<0-255>] for example $FG[172]
Upper case might be important there as $fg[172] does not work on my console!
Possible problem
Your terminal might support only 8 colors instead of 256. If it is true you will not see all the colors after executing spectrum_ls.
In such case you have to configure your terminal to support 256 colors.
Source
https://dev.to/yujinyuz/custom-colors-in-oh-my-zsh-themes-4h13
Unless you're using a very unusual terminal, you can't use just any color combination that you would like. Standard terminals are limited to (at best) a 256-color palette.
The colors function which ships with zsh is simply to allow the colors from the old 16-color palette to be referred to by name, it will not help in using colors outside of that range.
There is a simple script available which will setup $FG and $BG arrays to provide a way to use colors from the 256-color palette by number, but without needing to deal with the escape sequences necessary for the terminal to deal with those.