Where to save/store unix function? - macos

This is probably a very basic question but I could not find a good answer anywhere, also sorry I didn't post this to the Unix version of StackExchange I didn't want to create an account for a simple question. Heads up I'm using a Mac, so not sure if that will effect the answer
I created a unix function
tap()
{
touch $1
open $1
}
My problem is I don't know where I should write/save the function.
I put this function in my .bashrc but when I try calling tap it does not work. Is there something I should do to activate it? I did source .bashrc but this did not work. Where should I put it.

Save it as a file.
vim tap
touch $1
open $1
:wq
chmod +x tap
sudo mv tap /usr/local/bin

Related

Setting up vim autocomplete for the Go programming language

EDIT:
I was generally interested to know how people had their go autocomplete set up for vim and was looking for advice on it.
Related to my original question, I think I know I can just go to:
https://github.com/nsf/gocode
copy the files that they provide there and just start using the vim autocomplete. However, I wanted to know what people thought and how the go community has the vim autocomplete set up.
Also, I have followed the intructions as were posted there and I still cannot make the autocompletion work for my go in vim. So I am looking for other alternatives or ideas on how to make it work. Not sure what the problem is. Currently when I try to autocomplete it simply writes the word PANIC instead of showing me the options for autocompletion.
ORIGINAL:
I was trying to set up my vim such that it could auto complete the Go programming language, however, I was following the instructions in the following page:
https://github.com/nsf/gocode
and I was not sure what they meant and I was a little scared of maybe changing my vim set up in a way that might be damaging by doing it wrongly.
The first thing that confused me is it says:
Install official Go vim scripts from $GOROOT/misc/vim. If you did that already, proceed to the step 2.
However, I was not sure what that even meant. I did go to that directory in my terminal and read the readme.txt file and I it said how to activate the syntax highlighting which I already had anyway. Is that everything I have to do for that step?
On step 2 it says:
do:
vim/update.sh
They actually provide the code that update.sh is but I was not sure what the beginning of the cp command meant i.e. its:
#!/bin/sh
mkdir -p "$HOME/.vim/autoload"
mkdir -p "$HOME/.vim/ftplugin/go"
cp "${0%/*}/autoload/gocomplete.vim" "$HOME/.vim/autoload"
cp "${0%/*}/ftplugin/go/gocomplete.vim" "$HOME/.vim/ftplugin/go"
But what does the ${0%/*} part do? and even if I know what the update.sh is, where do I even run this, since this vim/update.sh is done at a relative path?
I know update.sh wants me to copy some files to $HOME/.vim/ftplugin/go and $HOME/.vim/autoload, but I even did a find from ~ and couldn't find such files, so I am unsure on what to copy. I know where it should go, but not where the file even is. Does someone know where those files are or an easier or more detailed explanation on how to make vim auto-complete for go?
Thanks for the help in advance! :)
Some of the problems that I have discovered that I have, not sure if its expected, but in the $GOROOt/misc/vim/ftplugin/go I do not have the gocomplete.vim file at all. I have other stuff that seems irrelevent like an fmt.vim import.vim and a test.sh file.
But the odd thing is that at $GOROOt/misc/autoload I do not have the gocomplete.vim file but I instead have a complete.vim file. Not sure if that the same or why the name of the file would have changed. Anyway has their go autocomplete set up and mind sharing what they have and if they know what the differences might be with what I have encountered? Thanks!
ADDITION to Question
I am also generally interested in how other people have their auto-complete set up for their go in vim. Feel free to share that too!
Have you executed the update.sh command already? I'm pretty confident that it will work.
All of your Vim configuration is stored in ~/.vim/, ~/.vimrc and ~/.gvimrc (with Vim 7.4, you can put the last two also inside the first directory). To backup your Vim configuration, just store those somewhere (or put all of your dotfiles under version control, as many now do).
The ${0%/*} manipulates the script's file name ($0) like dirname does: It cuts off the script file name itself (everything at the end * until the last /). With this, you can invoke the script from anywhere, e.g. $GOROOT/misc/vim/update.sh or cd misc; vim/update.sh or cd misc/vim; ./update.sh.
The script also ensures that the autoload and ftplugin subdirs exist, and creates them if they don't yet. Just give it a try!

Terminal (Mac) create alias that includes user input

In Mac OS Terminal, I'm learning the basics of Unix. I'm having trouble with a hopeful easy fix, but cannot figure out where to start looking.
cd __________ && ls
That is a pretty common pattern for me, to check and see the file folder I'm working in. To save myself keystrokes, I thought to make an alias in my .profile
alias cd='cd && ls'
Now, the obvious flaw (which was not obvious to me) was I would not be able to give it a directory to actually change to
cd ~/Documents/ && ls
This is what I would like to do, but not have to type those last four characters. Any ideas on how I could incorporate my user input (maybe some kind of $(pbpaste) option?
Any help or suggestions would be appreciated.
You need to first create BASH function for this instead of alias:
mycd() { cd "$1"; ls; }
alias cd='mycd '
then use it like this:
cd ~/Documents/

cygwin clearscreen from bash

I want to clearscreen from bash in a cygwin terminal.
I have tried
cmd /c cls
echo -e "^V^L"
echo -e "\014"
None of these work - they all give a "linefeed". Pressing CTRL+L does work - but I want to call it from a Bash script.
Any ideas?
Or you could try ctrl-L :)
That should work in most emulated terminals as well.
There is two ways:
Using shortcuts
Alt+F8 to clear screen and Ctrl-L to skip page
Using Clear alias
Go to: cygwin\home\USER\.bashrc
Open .bashrc file and put this alias alias clear='printf "\033c"'.
restart your cygwin terminal.
to clear your screen use clear command.
If it's contained in your cygwin install (run Cygwin's setup and make sure you have the package called "ncurses"), try
tput clear
I have this in my ~/.bashrc:
alias clear='printf "\033c"'
likewise, printf "\033c" clears the screen from within a bash script.
Alt+F8 to clear screen and Ctrl-L to skip page
Using shortcuts (for mitty console users)
Alt+F8 to clear screen and Ctrl-L to skip page
Using sh bash adding a command
If you use console2 or same consoles, you can add this sh code as without extension this location : ..\cygwin64\bin\
cls and /or clear (without extension)
#!/bin/sh
printf "\033c"
Then you can use cls and|or clear in cygwin command.
I was trying to do something similar and upon researching I found the above answers, I tried to follow some of the above solutions, I added the line to .bashrc, but it did not accept the function, I tweaked around and found out another solution...it basically executes the ' printf "\33c" but instead of tweaking the .bash.rc file, you simply go to the directory - /cygwin/bin ,
Write the following command :- printf "\033c"
And save it with the filename "cls" , in the bin directory, ensure that there is NO EXTENSION to the file, thus it will have the type "file" ..save it,open the shell and the command will run...
I found this method by playing around with msys and cygwin, I have both on my windows PC and turns out msys supports the "cls" command, I found a file with the same name in it's bin directory and copied it to the cygwin's bin folder and edited the file with the printf function and this worked like a charm...hope this helps.

How to "source" ~/.bashrc automatically once it has been edited?

I would like to create an alias that does the following:
Opens TextMate with ~/.bashrc and allows me to edit it
Once I close TextMate, "sources" ~/.bashrc (so if I add a new alias, for example, it will be available immediately)
I tried the following:
alias b="/usr/bin/mate -w ~/.bashrc; source ~/.bashrc"
but it doesn't work: when I close TextMate, the shell doesn't return.
Any ideas?
I hesitate to suggest it, but if this is a feature you really want, you can make something similar happen by setting the PROMPT_COMMAND variable to something clever.
PROMPT_COMMAND is run every time the shell shows the shell prompt So, if you're okay with the shells updating only after you hit Enter or execute a command, this should nearly do it.
Put export PROMPT_COMMAND="source ~/.bashrc" into your ~/.bashrc file. Re-source it into whichever shell sessions you want the automatically updating behavior to work in.
This is wasteful -- it re-sources the file with every prompt. If you can get your editor to leave the old version in a specific file, say ~/.bashrc~ (where the first ~ means your home directory and the last ~ is just a ~, a common choice for backup filenames) then you could do something more like (untested):
export PROMPT_COMMAND="[ ~/.bashrc -nt ~/.bashrc~ ] && touch ~/.bashrc~ && source ~/.bashrc "
then it would stat(2) the two files on every run, check which one is newer, and re-source only if the ~/.bashrc is newer than its backup. The touch command is in there to make the backup look newer and fail the test again.

In Mac OSX 10.5, it can't find my Terminal commands sudo, find, etc

I don't know what has happened, but in my Terminal on Mac OSX 10.5 it can no longer find my sudo command, or find command, etc. They are there because if I put /usr/bin/sudo or /usr/bin/find it works fine...
My .bash_login file looks like this:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin$PATH"
My .bash_profile file looks like this:
export PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:/Library/Python/2.5/site-packages/django_trunk/django/bin:/usr/local/mysql/bin:/usr/bin/sudo$PATH"
I'll say now, I don't really know what I'm doing with the Terminal. I'm just a beginner to it all, and I must of done something for the environment variables (is that what they're called?) to be lost. I presumed I'd just have to make sure the /usr/bin/ path is in my bash files, but they are, and it doesn't seem to work. Please help!
Also, when I do use the /usr/bin/find command, it says "Permission denied" to me, even though I am logged into Mac OSX as the System Administrator account. I don't understand.
Any help would be grand. Thank you - James
It looks like both of your PATH exports are malformed:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin$PATH"
The end bit there won't work. It should be:
export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:$PATH
Notice the colon before '$PATH'? It's important ;)
Also, the double quotes are not necessary.
If this doesn't work, we will need more information. It is possible that something else is modifying your path even after your shell configurations are loaded.
Can you post the results of:
$ echo $PATH
Configuration files are not always a good indication of the current environment variables, since they are modified by many programs and files, all across your system. To see all of your environment variables, you can run:
$ env
This should fix the problem completely and permanently.
first, export environment paths by using below command in the terminal.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/b‌​in
now you have the commands you want. (eg. try ls. You'll see the command is working). But this is only for the current session. If you close the terminal and open a new one, you will have the previous issue. To make this change permanent, use below command,
go to home directory
cd ~
open .bash_profile file in nano / vim (I'm using nano here)
nano .bash_profile
This will open up nano editor. In a new line, paste the following;
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:$PATH
press 'control'+'o' to save (WriteOut) and 'control'+'x' to exit nano.
All done ! Now try the commands.
Check out --- http://www.sweeting.org/mark/blog/2008/05/26/mac-os-x-tip-setting-path-environment-variables
I went trough the same issue and here is how I solved it.
First of all I reverted the file to its original doing this way
/usr/bin/nano ~/.bash_profile
In my case I was not able to make work any command alias. Even vi or vim didnt work without specifying the full path of that command.
If nano is not installed just replace nano in the command by the editor installed
After that just restart the computer. In my case as I said bellow I could not use any command. When trying to do /usr/bin/source ~/.bash_profile
that command failed. So I had to restart the OS and it worked

Resources