Using an alias to get further into directory - unix

I created a alias to the directory data,
now I want to be able to access subdirectories for a script in the same manner as a normal directory, for example data/sub1.
The alias on its own works in the terminal but I can't access the path it stands for ( /User/usr/Folder/Subfolder/data ) in the command line arguments of the script.
python skript.py -input data/path/ -output data/path.
I tried $data, {data} and different quotation marks around the path but wasn't able to access it.
I saw that at some similar questions link -ln was suggested but it sounds like this is similar to an environment variable, the later didn't work for me reliably therefor I haven't tried the link yet.
Any suggestions, again I would prefer the alias but I'm open for anything else.
Kind regards

Related

Return one folder above current directory in Julia

In Julia, I can get the current directory from
#__DIR__
For example, when I run the above in the "Current" folder, it gives me
"/Users/jtheath/Dropbox/Research/Projects/Coding/Current"
However, I want it to return one folder above the present folder; i.e.,
"/Users/jtheath/Dropbox/Research/Projects/Coding"
Is there an easy way to do this in a Julia script?
First, please note that #__DIR__ generally expands to the directory of the current source file (it does however return the current working directory if there are no source files involved, e.g when run from the REPL). In order to reliably get the current working directory, you should rather use pwd().
Now to your real question: I think the easiest way to get the path to the parent directory would be to simply use dirname:
julia> dirname("/Users/jtheath/Dropbox/Research/Projects/Coding/Current")
"/Users/jtheath/Dropbox/Research/Projects/Coding"
Note that AFAIU this only uses string manipulations, and does not care whether the paths involved actually exist in the filesystem (which is why the example above works on my system although I do not have the same filesystem structure as you). dirname is also relatively sensitive to the presence/absence of a trailing slash (which shouldn't be a problem if you feed it something that comes directly from pwd() or #__DIR__).
I sometimes also use something like this, in the hope that it might be more robust when I want to work with paths that actually exist in the filesystem:
julia> curdir = pwd()
"/home/francois"
julia> abspath(joinpath(curdir, ".."))
"/home/"

Use bash_profile aliases in jupyter notebook

It looks like my Jupyter notebook picks up everything that I export in my .bash_profile, but nothing that I alias.
I think ! uses bin/sh, so it's understandable that the aliases from the bash profile don't port over, but the %%bash magic also does not pick up the aliases I've written.
Is there a way to make the aliases in my bash profile available through ! (ideally) or, at the least, using %%bash?
This seems to work (python3, modified from a hack I found in a jupyter issue)
import subprocess
lines = subprocess.check_output('source ~/.bash_profile; alias',shell=True).split(b'\n')
manager = get_ipython().alias_manager
for line in lines:
line = line.decode("utf-8")
split_index = line.find('=')
cmd = line[split_index+1:]
alias = line[:split_index]
cmd = cmd[1:-1]
print ("ALIAS:{}\t\tCMD:{}".format(alias,cmd))
manager.soft_define_alias(alias, cmd)
Here's another alternative, which is less a solution than a workaround: you can define aliases locally to the notebook using the %alias magic, and make those aliases available in the future using the %store magic. More alias trickiness here: https://github.com/ipython/ipython/wiki/Cookbook:-Storing-aliases
More on the %store magic here: http://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html
The next step is hacking the %store magic to persist these aliases: https://github.com/ipython/ipython/blob/master/IPython/extensions/storemagic.py
For posterity, here are the results of some experiments I ran before finally finding a solution:
I sourced my .bash_profile in a %%bash cell. From within that cell, I was able to interrogate the values of variables I defined in my .bash_profile, and was able to list aliased commands by invoking alias. However, I was still not able to use aliased commands. Additionally, variables defined in my .bash_profile were only accessible inside the cell with the source call: trying to access them in subsequent %%bash cell didn't work, and the alias command also failed. More interesting still: if I sourced using !, I wasn't able to interrogate variables defined in my bash profile nor list my aliases with ! shell commands in the same cell.
Suffice it say, the %%bash magic is finicky.

cmder ~ alias for user home folder

Simple question :
I am using cmder and i would like to be able to define ~(or ~USERID) as my %USERPROFILE% environment variables. I edited %CMDERROOT%\config\aliases to add this line : ~=%USERPROFILE% it does work for ls command but not for cd for instance.
Is there a better way to have this unix-like user profile aliases ?
For those who would like an answer to that question, it have been addressed in the following github issue :
https://github.com/cmderdev/cmder/issues/41
Not a direct answer, but a decent work-around was mentioned in the discussion on that page:
clink, the vendor package that would handle this has a similar issue report marked as "wontfix" So, here's the workaround
I'm using an AutoHotKey script for that:
#IfWinActive ahk_class VirtualConsoleClass
::~::D:/nicolas
#IfWinActive
It replaces automatically ~ with D:/nicolas in ConEmu console (and only ConEmu console)
I have not tried it in a script executed from the command line, but it works from the command line directly, as nicolas suggests.
I don´t remember where I got it but add these lines to your profile:
function cuserprofile { Set-Location ~ }
Set-Alias ~ cuserprofile -Option AllScope
you just hit ~ (enter) and you'll send to your home directory.
PS. I'm using PowerShell in cmder, regards.

How to get path to the vim being executed in vimscript

For example, in directory /abc the command vim def/my.txt would edit file /abc/def/my.txt.
I want to get the path /abc in my vim plugin script, but I don't know which function can do it.
I have read related articles, like
(Please add http:// to following items if you want to access, I don't have enough reputation to post more than 2 links o(╯□╰)o ):
learnvimscriptthehardway.stevelosh.com/chapters/40.html
vim.wikia.com/wiki/Get_the_name_of_the_current_file
How to get path to the current vimscript being executed
But useless.
Thanks.
==============================
I noticed at http://vim.wikia.com/wiki/Get_the_name_of_the_current_file, it said:
:echo #% def/my.txt
I thought I found the hope.
Because I can get /abc by minus def/my.txt from full path.
But I tested above command in my vim environment, it didn't work. It just print my.txt.
I think you're looking for the :pwd command / getcwd() function.
When you edit def/my.txt from /abc, the latter is your current directory, and all files are addressed relative to it. (Unless you have something like :set autochdir.)
For dealing with file paths, the fnamemodify() function is also very helpful; e.g. to turn filespecs into absolute ones (:p argument), or to cut off head, tail, or file extension.

How to use a template in vim

This is really a newbie question - but basically, how do I enable a template for certain filetypes.
Basically, I just want the template to insert a header of sorts, that is with some functions that I find useful, and libraries loaded etc.
I interpret
:help template
the way that I should place this in my vimrc
au BufNewFile,BufRead ~/.vim/skeleton.R
Running a R script then shows that something could happen, but apparently does not:
--- Auto-Commands ---
This may be because a template consists of commands (and there are no such in skeleton.R) - and in this case I just want it to insert a text header (which skelton.R consist of).
Sorry if this question is mind boggeling stupid ;-/
The command that you've suggested is not going to work: what this will do is run no Vim command whenever you open ~/.vim/skeleton.R
A crude way of achieving what you want would be to use:
:au BufNewFile *.R r ~/.vim/skeleton.R
This will read (:r) your file whenever a new *.R file is created. You want to avoid having BufRead in the autocmd, or it will read the skeleton file into your working file every time you open the file!
There are many plugins that add a lot more control to this process. Being the author and therefore completely biased, I'd recommend this one, but there are plenty of others listed here.
Shameless plug:
They all work in a relatively similar way, but to explain my script:
You install the plugin as described on the linked page and then create some templates in ~/.vim/templates. These templates should have the same extension as the 'target' file, so if it's a template for .R files, call it something like skeleton.R. In your .vimrc, add something like this:
let g:file_template_default = {}
let g:file_template_default['R'] = 'skeleton'
Then create your new .R file (with a filename, so save it if it's new) and enter:
:LoadFileTemplate
You can also skip the .vimrc editing and just do:
:LoadFileTemplate skeleton
See the website for more details.
Assume that your skeletons are in your ~/.vim/templates/ directory, you can put this
snippet in your vimrc file.
augroup templates
au!
" read in templates files
autocmd BufNewFile *.* silent! execute '0r ~/.vim/templates/skeleton.'.expand("<afile>:e")
augroup END
Some explanation,
BufNewFile . = each time we edit a new file
silent! execute = execute silently, no error messages if failed
0r = read file and insert content at top (0) in the new file
expand(":e") = get extension of current filename
see also http://vim.wikia.com/wiki/Use_eval_to_create_dynamic_templates
*fixed missing dot in file path
Create a templates subdirectory in your ~/.vim folder
$ mkdir -p ~/.vim/templates
Create a new file in subdirectory called R.skeleton and put in the header and/or other stuff you want to automagically load upon creating a new ".R " file.
$ vim ~/.vim/templates/R.skeleton
Then, add the following to your ~/.vimrc file, which may have been suggested in a way by "guest"
autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.skeleton
Have a look at my github repository for some more details and other options.
It's just a trick I used to use .
It's cheap but If you ain't know nothing about vim and it's commands it's easy to handle.
make a directory like this :
~/.vim/templates/barney.cpp
and as you konw barney.cpp should be your template code .
then add a function like ForUncleBarney() to end of your .vimrc file located in ~/.vimrc
it should be like
function ForBarneyStinson()
:read ~/.vim/templates/barney.cpp
endfunction
then just use this command in vim
:call ForBarneyStinson()
then you see your template
as an example I already have two templates for .cpp files
:call ForBarney()
:call ACM()
sorry said too much,
Coding's awesome ! :)
Also take a look at https://github.com/aperezdc/vim-template.git.
I use it and have contributed some patches to it and would argue its relatively full featured.
What about using the snipmate plugin? See here
There exist many template-file expanders -- you'll also find there explanations on how to implement a rudimentary template-file expander.
For my part, I'm maintaining the fork of muTemplate. For a simple start, just drop a {ft}.template file into {rtp}/template/. If you want to use any (viml) variable or expression, just do. You can even put vim code (and now even functions) into the template-file if you wish. Several smart decisions are already implemented for C++ and vim files.

Resources