Replacing a fixed part of PWD in a tcsh prompt - configuration

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

Related

Local variable in makefile is not expanded correctly

I have the following function in makefile:
define INSTALL_SCRIPT
SRC_DIR = $(ROOT)\src
cd $(SRC_DIR)
$(SRC_DIR)\stage.bat
endef
I also echo the steps, so here's the output of the above snippet:
$SRC_DIR = C:\project_root\src
'SRC_DIR' is not recognized as an internal or external command,
operable program or batch file.
$cd
C:\project_root
\stage.bat
'\stage.bat' is not recognized as an internal or external command,
operable program or batch file.
It seems that in assignment statement the value is expanded correctly but then $(SRC_DIR) gives an error. Then cd goes to one directory up (and not src), then when I need to execute the batch file, $(SRC_DIR)'s value seems to be empty.
Assuming you're trying to do this from a recipe context, you would need to do it as follows:
define INSTALL_SCRIPT
set SRC_DIR=$(ROOT)\\src & \
cd %SRC_DIR% & \
%SRC_DIR%\\stage.bat
endef
sometarget:
#$(INSTALL_SCRIPT)
You need the \ at the end of each line to concatinate them into a single recipe line (other wise the variable you set will fall out of context when the first recipe line's shell terminates). You seem to be using windows so I believe you need to use the %varname% syntax to refer to the variables. Notice that $(ROOT) is a makefile variable in this case, so it still uses the $ syntax. (Note that if you were in bash you would need to use $$ to refer to shell variables). You also need to double the \\ in directory names, as make will interpret the first slash as an escape, and then pass a single slash to cmd.
Note that my windows machine doesn't have make installed on it, so I couldn't test the above, so it's quite possible I missed something.

Output last command

I am using a function that I found in YADR which should insert the output of the last command.
# Use Ctrl-x,Ctrl-l to get the output of the last command
zmodload -i zsh/parameter
insert-last-command-output() {
LBUFFER+="$(eval $history[$((HISTCMD-1))])"
}
zle -N insert-last-command-output
bindkey "^X^L" insert-last-command-output
For some reason, it does not seem to work by pressing ctrl-x ctrl-l but running
echo $(eval $history[$((HISTCMD-1))])
command on the terminal does produce the output of the last command.
Running bindkey -M viins shows "^X^L" insert-last-command-output
as one of the entries. Therefore, the function is registered.
I don't really understand how the function works. I think that the variable LBUFFER holds the output of all last commands but when I echo $LBUFFER, it returns the function code.
Can anyone help me get this working?
I finally found a solution.
I had been trying to use the shortcut inside tmux which did not work. However, outside tmux, everything worked. It turns out that tmux will not allow a shortcut with two keys. I changed the shortcut to just alt-L and everything works.

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.)

Bash read line from variables

Hi & thanks in advance.
I'm trying to update a column(version) on an MySQL table from a Bash script.
I've populated a variable with the version numbers, but it fails after applying the first version in the list.
CODE:
UP_VER=`seq ${DB_VER} ${LT_VER} | sed '1d'`
UP_DB=`echo "UPDATE client SET current_db_vers='${UP_VER}' WHERE client_name='${CLIENT}'" | ${MYSQL_ID}`
while read -r line
do
${UP_DB}
if [[ "${OUT}" -eq "0" ]]; then
echo "Database upgraded.."
else
echo "Failed to upgrade.."
exit 1
fi
done < "${UP_VER}"
Thanks
Hopefully solved... My $UP_VER is in a a row not a column.
You're misunderstanding what several shell constructs do:
var=`command` # This executes the command immediately, and stores
# its result (NOT the command itself) in the variable
... < "${UP_VER}" # Treats the contents of $UP_VER as a filename, and tries
# to use that file as input
if [[ "${OUT}" -eq "0" ]]; then # $OUT is not defined anywhere
... current_db_vers='${UP_VER}' ... # this sets current_db_vers to the entire
# list of versions at once
Also, in the shell it's best to use lowercase (or mixed-case) variable names to avoid conflicts with the variables that have special meanings (which are all uppercase).
To fix the first problem, my recommendation is don't try to store shell commands in variables, it doesn't work right. (See BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!.) Either use a function, or just write the command directly where it's going to be executed. In this case I'd vote for just putting it directly where it's going to be executed. BTW, you're making the same mistake with ${MYSQL_ID}, so I'd recommend fixing that as well.
For the second problem, you can use <<< "${UP_VER}" to feed a variable's contents as input (although this is a bashism, and not available in generic posix shells). But in this case I'd just use a for loop:
for ((ver=db_ver+1; ver<=lt_ver; ver++)); do
For the third problem, the simplest way to test the success of a command is to put it directly in the if:
if somecommand; then
echo "Database upgraded.."
else # ... etc
So, here's my take at a rewrite:
mysql_id() {
# appropriate function definition goes here...
}
for ((ver=db_ver+1; ver<=lt_ver; ver++)); do
if echo "UPDATE client SET current_db_vers='${ver}' WHERE client_name='${client}'" | mysql_id; then
echo "Database upgraded.."
else
echo "Failed to upgrade.."
exit 1
fi
done
... but I'm not sure I understand what it's supposed to do. It seems to be updating current_db_vers one number at a time until it reaches $ver_lt... but why not set it directly to $ver_lt in a single UPDATE?
try something like :
done <<< "${UP_VER}"

tcl open pipe seems to misshandle spaces in parameters

I have this open:
set r [catch {open "|[concat $config(cmd,sh) [list $cmd 2>#1]]" r} fid]
where $config(cmd,sh) is cmd /c and I am trying to pass a file name (and possibly a command such as echo) in $cmd. If there is no space in the file name, i.e. :
cmd is echo /filename
all is well. With a space, i.e.:
cmd is echo "/file name"
what appears to be passed is:
\"file name\".
When I try this on Linux, I get "file name" (no backslashes). I have tried replacing the spaces in the file name with "\ ", but then the target gets two file names, i.e. the space is used to break up the file name.
I am beginning to think I have found a bug in the Windows port of Tcl...
Ugh, that looks convoluted! To pass this sort of thing into the pipe creation code, you need to use exactly the right recipe:
set r [catch {open |[list {*}$config(cmd,sh) $cmd 2>#1] r} fid]
That is, always use the form with |[list ...] when building pipes as the documentation says that is what the pipe opener looks for. (This is the only command like that in Tcl.)
And of course, using the (8.5+) {*} syntax is much simpler in this case too, as it is more obviously doing the right thing.