Skip to content

Instantly share code, notes, and snippets.

View seanh's full-sized avatar

Sean Hammond seanh

View GitHub Profile
@seanh
seanh / vimgrep.md
Last active April 5, 2024 19:28
vimgrep cheatsheet

vimgrep

  • Vimcasts on vimgrep

  • Uses native vim regexes (which are slightly different from the regexes used by grep, ack, ag, etc) so the patterns are the same as with vim's within-file search patterns.

You can do a normal within-file search first, then re-use the same pattern to

@seanh
seanh / netrw.md
Last active April 5, 2024 02:13
Netrw Cheatsheet (Vim's Built-in Directory Browser)

Netrw Cheatsheet (Vim's File Browser)

See also:

  • vinegar.vim, which makes - open netrw in the directory of the current file, with the cursor on the current file (and pressing - again goes up a directory). Vinegar also hides a bunch of junk that's normally at the top of netrw windows, changes the default order of files, and hides files that match wildignore.

    With vinegar, . in netrw opens Vim's command line with the path to the file under the cursor at the end of the command. ! does the same but also prepends ! at the start of the command. y. copies the absolute path of the file under the cursor. ~ goes to your home dir. Ctrl+6 goes back to the file (buffer) that you had open before you opened netrw.

To launch netrw:

@seanh
seanh / formatFilename.py
Created April 11, 2009 18:30
Turn any string into a valid filename in Python.
def format_filename(s):
"""Take a string and return a valid filename constructed from the string.
Uses a whitelist approach: any characters not present in valid_chars are
removed. Also spaces are replaced with underscores.
Note: this method may produce invalid filenames such as ``, `.` or `..`
When I use this method I prepend a date string like '2009_01_15_19_46_32_'
and append a file extension like '.txt', so I avoid the potential of using
an invalid filename.
@seanh
seanh / gist:232821
Created November 12, 2009 11:11
Creating and removing files and directories with Python os module
"""This is done with the os module, which has lots of methods for handling files and dirs.
<http://docs.python.org/lib/os-file-dir.html>
Effbot's page on the os module: <http://effbot.org/librarybook/os.htm>
The shutil module is useful here also: <http://docs.python.org/lib/module-shutil.html>
"""
import os
@seanh
seanh / gist:232828
Created November 12, 2009 11:17
List all the files in a directory with a one-line list comprehension (Python)
# http://www.diveintopython.org/file_handling/os_module.html
# Use a one-line list comprehension to get all the files in a given directory with a given extension.
import os
dir = '.'
ext = '.txt'
txt_files = [f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir,f)) and f.endswith(ext)]
# os.path.join joins a directory and a filename into a path. You can also split a path name into directory and file with
# os.path.split(), and you can split a filename with extension into filename and extension with os.path.splitext()
@seanh
seanh / obsession.vim.md
Last active June 12, 2019 22:44
obsession.vim cheatsheet (simple, no-hassle Vim sessions from Tim Pope)

obsession.vim cheatsheet

obsession.vim is simple, no-hassle Vim sessions from Tim Pope. It's a small plugin that just provides :Obsession, a better replacement for the standard :mksession command.

  • Adds the :Obsession command:

  • :Obsession with no argument creates a Session.vim file in the current

@seanh
seanh / README.md
Created November 19, 2012 12:46 — forked from rufuspollock/data.json
A script that pulls some demo data (gold prices, Malawi aid projects, etc.) from datahub.io and pushes them to your CKAN instance.

Installation

virtualenv load_demo_data
. load_demo_data/bin/activate
mkdir load_demo_data/src
cd load_demo_data/src
git clone git://gist.github.com/4110454.git load_demo_data
pip install -r load_demo_data/pip-requirements.txt
@rufuspollock
rufuspollock / data.json
Created October 1, 2012 18:32
CKAN - Load Demo Data
{
"datasets": {
"adur_district_spending": {
"author": "Lucy Chambers",
"author_email": "",
"extras": {
"spatial-text": "Adur, West Sussex, South East England, England, United Kingdom",
"spatial": "{ \"type\": \"Polygon\", \"coordinates\": [ [ [-0.3715, 50.8168],[-0.3715, 50.8747], [-0.2155, 50.8747], [-0.2155, 50.8168], [-0.3715, 50.8168] ] ] }"
},
"license": "License Not Specified",
@seanh
seanh / README.markdown
Created March 26, 2012 15:56
virtualenv bootstrap script for creating a virtual environment and installing CKAN into it

To create a virtual environment and install CKAN into it:

git clone git://gist.github.com/2206132.git
python 2206132/ckan-bootstrap.py pyenv

This makes a directory pyenv in the current working directory, creates a virtual environment in the pyenv dir, and installs CKAN (and all CKAN's dependencies) into the virtual environment.

Activate the virtual environment and run the CKAN tests:

@seanh
seanh / gist:232824
Created November 12, 2009 11:15
Import a module from a directory (Python)
"""When you want to import a python file but that file is not in the same directory as the python file you're importing from (e.g. it's in a subdir). You need to add the directory that contains the file you want to import to your path environment variable sys.path. sys.path contains all the places python will look for a file when you do an import."""
import sys
sys.path.append('./markdown-1.7')
from markdown import Markdown