Skip to content

Instantly share code, notes, and snippets.

View mattboehm's full-sized avatar

Matthew Boehm mattboehm

View GitHub Profile
import itertools
#ugly one-liner
def _ugly_split_at(pred, seq):
return list(itertools.ifilter(
None,
[
(
(seq[idx:idx+1] + list(itertools.takewhile(lambda x: not pred(x), seq[idx+1:])))
if idx == 0 or pred(el)
else []
@mattboehm
mattboehm / gist:4234781
Created December 7, 2012 17:14
Count method/property usage in vim
"Copy all matches to a vim search and paste in new buffer separated by lines
"Source this file to try it out.
"Let's say you have a python file and want to find all the methods/properties
"on self mentioned in a file (commence esoteric example):
"class Student(object):
" def __init__(self, name):
" self.name = name
" def set_gpa(self, gpa):
@mattboehm
mattboehm / merge.vim
Created December 12, 2012 21:19
Crappy Vim "mail merge"
"I recorded a macro of the command. These are the keystrokes I hit to do one line
"To do all lines, I recorded the macro in register q (starting on the first line of names.csv)
"then did 2@q to repeat macro twice more.
"This assumes the template is in the same dir and is very brittle
"You should use a templating engine like genshi for non-trivial templating, but just wanted to show that this is possible
"Creates $email.html file for each email address.
0"ayt,f,l"by$:new^M;read template.html^M:%s;\$EMAIL;^Ra;g^M:s;$NAME;^Rb;g^M:w! ^Ra.html^M:bd^Mj
@mattboehm
mattboehm / marks.vim
Created March 15, 2013 01:35
Notes from my February NoVA Vim presentation on marks, jumps and registers. Mostly just a list of suggested :help sections to read.
"These lines map a hotkey that allow you to easily execute the current line or
"selection by hitting F9. It's a cheesy hack that made demoing while reading
"through this more simple.
"
"This is also why commands start with colons. Normally colons would be
"unnecessary if trying to execute these in your vimrc.
:nnoremap <F9> "ayy@a
:vnoremap <F9> "ay@a
"Marks {{{
@mattboehm
mattboehm / gist:7109927
Last active January 6, 2024 07:25
Highlighting lines in vim
"with your cursor on a line or a block selected, type `:HL`
"to remove, on each line call :sign unplace
highlight HL ctermbg=darkgray
sign define hl linehl=HL
let s:highlightLineSignId = 74000
function! g:HighlightLine()
execute 'sign place' s:highlightLineSignId 'line='.line(".") 'name=hl' 'file='.expand("%")
let s:highlightLineSignId += 1
endfunction
command! HL call g:HighlightLine()
@mattboehm
mattboehm / gist:7456743
Last active December 28, 2015 06:28
Tmux aliases
#tl: list sessions
alias tl='tmux ls'
#tn <name>: create a session named <name>
alias tn='tmux -2 new -s'
#ta <name>: attach to a session named <name>
alias ta='tmux -2 attach -t'
@mattboehm
mattboehm / gist:9977950
Created April 4, 2014 16:15
A mini-plugin to cycle through diffs of unstaged git changes by file (requires fugitive)
nnoremap <silent> <leader>gm :tab split<CR>:Glistmod<CR>
nnoremap <silent> <c-s-j> :call g:DiffNextLoc()<CR>
nnoremap <silent> <c-s-k> :call g:DiffPrevLoc()<CR>
command! Glistmod only | call g:ListModified() | Gdiff
function! g:ListModified()
let old_makeprg=&makeprg
let old_errorformat=&errorformat
let &makeprg = "git ls-files -m"
let &errorformat="%f"
@mattboehm
mattboehm / tetris_dense.py
Created April 13, 2014 16:59
Pycon 2014 Thumbtack solution ("dense" version)
import sys, re #thumbtack pycon tetris challenge. "dense" solution by @mattboehm
def shift_down(col):#shift piece down 1 space
loc = "".join(col[::-1])
space = loc.find("#") - 1
return col if loc == -2 else list(loc[:space] + loc[space+1:] + ".")[::-1]
def top_row_blank(board):#check if the top row has only .
return all(tile == "." for tile in board[0])
def find_piece(board):#Replace the X's for the piece with #
board, marked_board = board[:], []
while top_row_blank(board):#skip top blank rows
function! SplitRegex()
    tabnew
    setlocal buftype=nofile bufhidden=hide noswapfile
    let numlines=len(@")
    execute "normal! V".numlines."pggjl"
    let tmp=@z
    let @z="hv0r wlv$r j"
    execute "normal! ".(numlines-2)."@z"
    let @z=tmp
    execute "normal! gglv$r\<space>"
#!/usr/bin/env bash
if [ "$#" -ne 2 ]
then
echo "move a directory to a new location.
While normally, git mv old-dir new-dir should work, let's say you do that and then try to merge in another branch where someone's added new files to old-dir.
these files will stay in old-dir, and we need a way to merge their (arbitrarily deeply nested) contents into new-dir. This script uses cp -r to merge the contents,
followed by git add/rm. When you do git status, it should now show that all those files have been moved.
Warning: script will add the whole target directory, including uncommitted changes on files that existed before it ran."
echo "usage: $0 <old-dir> <new-dir>"