Coding cheatsheet
Here’s a load of quick snippets of easy-to-forget but often used commands, tricks and tips. It’s got useful info about git, Vim, Python and Perl.
Git
First commands
These are the things you need to do when using git
on a new computer:
Adding remote
Add remote repository:
Then subsequently set local branch to track remote branch:
Setting up git submodules
Adding submodules in a git repository is as simple as editing the file .gitmodules
in the root of the repository, with the following syntax:
Then a quick:
Updating git submodules
You might be tempted to think that updating all your submodules from their respective remotes is as simple as running git submodule update
. But no, what that does is update the submodules to the version checked out in the remote of the root repo. What you need is to update the version of the submodules that is checked out by the root repo. Here’s how to do it, assuming your submodule is called subm
:
And you’re done! It can be a bit annoying for updating loads of subdirectories, so I’m working on a short script to do it all for you. I’ll update here once I finish it.
Git subsubmodules
If you’ve got nested submodules (i.e. a submodule within another submodule), then you need to do alter this slightly to update the nested submodules. As well as having each submodule within your submodule containing a [submodule "foobar"]
within both your root .gitmodules
and your submodule .gitmodules
, you need to do the following from within the root repository to update all submodules and subsubmodules:
Vim
Vim functions
Here’s the syntax for declaring vim script functions:
And you then call it in Vim with:
Recognise custom filetypes
I’ve got moo.vim
files in my ~/.vim/after/syntax
and ~/.vim/after/ftplugin
, for all moo
files with extension .moo
. To get Vim to recognise these .moo
files and apply the Vim scripts associated therewith, I need to create a file called moo.vim
in ~/.vim/ftdetect/
, which contains the following:
Note: You may have to wipe your ~/.vim/view
before Vim recognises old files as this new filetype.
Editing over scp
Vim comes with the ability to edit files remotely over scp. This can be achieved via:
However, trying to save gives the error:
In fact, running set buftype?
reveals that buftype
is set to nofile
, meaning the buffer cannot be saved to file. This can be bypassed by using :Nwrite
from the netrw.vim that comes bundled with Vim 7.0:
sshfs
To allow other non-root users to access a filesystem mounted over ssh, use:
Photoshop
Whilst I don’t generally like expensive proprietary software, particularly photoshop, given the importance of this small technique to my current project (on which I will write a full post soon), I felt it important to include how to mask parts of photos in Photoshop, ready to be imported into programs like PhotoScan (another piece of incredibly expensive proprietary software).
-
Firstly, select the region you want to mask (or keep unmasked, whichever is easier). The
w
key switches between the Quick Selection and Magic Wand tools, both useful in their own rights. -
Next, in the “Channels” group, click “New channel” at the bottom of the group box. The image should now turn black.
-
If you want to mask the selection, press
<CMD><SHIFT>I
to invert the selection and press<SHIFT><F5>
; select “White” and press<CR>
. If you want to mask everything apart from the selection, then simply press<SHIFT><F5>
, select “White” and press<CR>
-
Save the file as a format supporting alpha channels by pressing
<CMD><SHIFT>S
. For PhotoScan imports,TIFF
is recommended. Tick the box called “Alpha Channels” and press<CR>
twice to save. -
(optional) In PhotoScan, after loading the photos into the workspace with Workflow>Add photos/Add folder, click Tools>Import>Import masks…. Make sure that “Method” is set to “From Alpha” and click okay. The masked areas will then be darkened to indicate they are masked.
Bonus Tip: To import masks from one photo into another, simpler drag the channel onto the new photo.
Note that this is based on the ancient CS3 that’s installed on the Mac I’m currently using, and may not apply to newer versions.
Python
argparse
Skeleton argparse
template:
Replace ~
with home directory
This is just a fun little script that replaces ~
in a string with the path to the user directory, e.g. /home/drew
. Useful for taking input and output filenames from input, and want people to be able to use their familiar tilde.
Increase size of pyplot legend
Sometimes, the legend in matplotlib isn’t quite big enough. Increase it with:
Fix spacing in pyplot multiplots
Every time I do a subplot in pyplot, I get annoyed at the spacing, and every time I forget that all you need to do is put the following in your script and it will automagically sort the spacing out for you:
Why is this not a standard part of matplotlib? I don’t know.
Change x or y ticks on pyplot plot
You can use either np.arange
or specify the ticks yourself:
Set custom limits on pyplot plot
Here you can both retrieve and set the x and y limits with plt.xlim
and plt.ylim
:
Find the properties of an object
To find the properties of an object, you can do the following:
Perl
Pie
Probably the most useful thing that perl
can do is perl -pi -e
, often lovingly called Perl Pie. The syntax is:
This replaces string to find
with string to replace
in filenames. This is fully regex compatible. For instance, if I wanted to replace mispelt
with misspelt
in all files ending in .txt
, I would run: