Skip to content

Instantly share code, notes, and snippets.

View AndrewRadev's full-sized avatar

Andrew Radev AndrewRadev

View GitHub Profile
@AndrewRadev
AndrewRadev / tabularize.vim
Last active September 26, 2015 22:48
Tabularize mappings for specific use cases
" Tabularize mappings
"
" sa= -- align by equals
" sa> -- align by "=>"
"
" and so on. Note that any character can be entered and the mappings will
" attempt to align by that, in the simplest way possible.
"
" sa| -- equivalent to ":Tab/|"
"
@AndrewRadev
AndrewRadev / spec_helper.rb
Created September 23, 2011 09:01
Make the "=~" operator treat ActiveRecord relations like arrays in rspec
# So you can do something like:
#
# Post.some_scope =~ [some_post, some_other_post]
#
# Matching the results of the scope without caring about the order.
#
RSpec::Matchers::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::MatchArray)
@AndrewRadev
AndrewRadev / nerdtree_fix.vim
Created November 11, 2011 18:28
When :edit-ing in NERDTree, open the file in the closest buffer instead
" Whenever you execute an `:edit somefile` command with the cursor in the
" NERDTree, the file gets edited in the NERDTree buffer, which is usually not
" something you want. This autocommand attempts to edit the given file in the
" closest non-NERDTree buffer instead.
"
" It's fairly hacky, but seems to work. I'll probably use it myself, so I'll
" try to remember to update it if I find any problems.
augroup AvoidEditingNERDTree
autocmd!
@AndrewRadev
AndrewRadev / bookmarks.vim
Created November 16, 2011 20:02
Simple bookmark management in Vim
set viminfo+=!
if !exists('g:BOOKMARKS')
let g:BOOKMARKS = {}
endif
" Add the current [filename, cursor position] in g:BOOKMARKS under the given
" name
command! -nargs=1 Bookmark call s:Bookmark(<f-args>)
function! s:Bookmark(name)
@AndrewRadev
AndrewRadev / qf.vim
Created December 2, 2011 20:14
Delete lines from the quickfix window (with undo)
" Place in ftplugin/qf.vim
xnoremap <buffer> d :DeleteLines<cr>
nnoremap <buffer> dd :DeleteLines<cr>
nnoremap <buffer> u :UndoDelete<cr>
if !exists(':DeleteLines')
let b:deletion_stack = []
" Delete by a pattern (with undo placing them all on top):
@AndrewRadev
AndrewRadev / tabj.vim
Created December 13, 2011 14:22
Simple jumping through buffers
command! -nargs=1 -complete=custom,s:TabjCompletion Tabj call s:Tabj(<f-args>)
function! s:Tabj(name)
for i in range(tabpagenr('$'))
let tabnr = i + 1
for bufnr in tabpagebuflist(tabnr)
if stridx(bufname(bufnr), a:name) >= 0
exe 'tabnext '.tabnr
while bufnr('%') != bufnr
@AndrewRadev
AndrewRadev / storeundo.vim
Last active September 5, 2023 17:30
Store named savepoints in vim
command! -nargs=1 StoreUndo call s:StoreUndo(<f-args>)
command! -nargs=1 -complete=custom,s:CompleteUndoStates RestoreUndo call s:RestoreUndo(<f-args>)
function! s:StoreUndo(label)
if !exists('b:stored_undo_state')
let b:stored_undo_state = {}
endif
let b:stored_undo_state[a:label] = undotree()['seq_cur']
endfunction
@AndrewRadev
AndrewRadev / ginitpull.vim
Last active March 9, 2016 22:58
Start a github pull request from Vim
" Usage:
"
" :Ginitpull [remote-name] [branch-name]
"
" Initiates a github pull request in the default browser, from the given
" branch name to master, on the given remote. Tab-completes both arguments.
"
" If called without any arguments, defaults to the "origin" remote and the
" current branch name, which is probably what you usually want.
"
@AndrewRadev
AndrewRadev / mappings.vim
Created December 20, 2011 08:15
Map <space> to enter command-line mode and upcase the next keypress
" <space>x -> :X
" For easier typing of custom commands
nnoremap <space> :call <SID>SpaceMapping(0)<cr>
xnoremap <space> :<c-u>call <SID>SpaceMapping(1)<cr>
function! s:SpaceMapping(visual)
echo
let c = nr2char(getchar())
if a:visual
normal! gv
endif
@AndrewRadev
AndrewRadev / cheerlights.sh
Created December 22, 2011 13:55
Simple shell script that checks the current cheerlights color
#! /bin/sh
current_color=''
last_color=''
while :;
do
current_color=`curl -s 'http://api.thingspeak.com/channels/1417/field/1/last.txt'`
if [ "$current_color" != "$last_color" ]