Nov 30, 2012
Nov 29, 2012
URL Design using base36
http://pydanny.com/case-study-url-design-for-petcheatsheetscom.html
/<pet_id:base_36_encoded>/<pet_name:not_required>/
/<pet_id:base_36_encoded>/<pet_name:not_required>/
class Pet(models.Model):
name = models.CharField(_("Pet's name"), max_length=100)
identifier = models.CharField(_("identifier"), max_length=50,
null=True, blank=True, db_index=True)
class PetCreateView(LoginRequiredMixin, CreateView):
model = Pet
form_class = PetForm
def form_valid(self, form):
pet = form.save()
pet.identifier = base36.encode(pet.pk)
pet.owner = self.request.user
# Save again - it's not taking THAT many server cycles AND we needed
# the pet.pk in advance to generate the pet.identifier
pet.save()
return super(PetCreateView, self).form_valid(form)
urlpatterns = patterns("",
url(
regex=r"^(?P[\w\d]+)/(?P[\w\d\-\_]+)/$",
view=views.PetDetailView.as_view(),
name="pet_detail",
),
)
Labels:
url
Nov 23, 2012
Git HowTos
Apply diff
Forget all local changes but you cant push to remote branch
git diff > some.diff
git apply some.diff
Retaine all the untracked junk in your working directory
git stash
# Code is now as it was at last checkin.
git checkout fe9c4
# Look around here.
git checkout master # Or whatever commit you had started with
# Code is now as it was at last checkin, so replay stashed diffs with:
git stash pop
Take the working directory back to the state it was in when you last checked out
git reset --hard
Create branch
git branch new_leaf # Create a new branch...
git checkout new_leaf # then check out the branch you just created.
#or execute both steps at once with the equivalent:
git checkout -b new_leaf
git push -u origin new_leaf
Create tag:
git tag v1.2 # Create a new tag...
git push origin --tags
Merge
- Run
git merge other_branch
. - In all likelihood, get told that there are conflicts you have to resolve.
- Check the list of unmerged files using git status.
- Pick a file to manually check on. Open it in a text editor and find the merge-me marks if it is a content conflict. If it’s a filename or file position conflict, move the file into place.
- Run
git add your_now_fixed_file
. - Repeat steps 3−5 until all unmerged files are checked in.
- Run
git commit
to finalize the merge.
git mergetool
Replay on the testing branch all of the changes made on the main branch since the common ancestor
git branch testing # get on the testing branch
git rebase abcd123
# or equivalently: git rebase main
How to retrieve the hash for the current commit in Git?
git rev-parse HEAD
# 86816eb7515578112ee9460da8a4040200f478d7
# or short SHA-1 version:
git rev-parse --short HEAD
# 86816eb
Reset vs Revert
Forget all local changes but you cant push to remote branch
git reset --hard HEAD
# or
git reset COMMIT_NO
git revert COMMIT_NO
# or
git revert -m 2 COMMIT_NO # for merge
git push
How to count total lines changed by a specific author in a Git repository?
git log --author="_Your_Name_Here_" --pretty=tformat: --numstat \
| gawk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s removed lines: %s total lines: %s\n", add, subs, loc }' -
Git – how to determine the committers or authors
git shortlog -sn # or if you want to include the email
git shortlog -sne
Labels:
git
Nov 20, 2012
Practical Vim: Edit Text at the Speed of Thought - Drew Neil
Editing
. - repeats the last change
u - undo <C-r> - redox - deletes character under the cursor d{motion} - delete dl dd - delete current line db - deletes from the cursor’s starting position to the beginning of the word dw
diw daw dap df{char} d/{expression} c{motion} - change y{motion} - yank into register
A - append to the and of line A = $a C = c$ s = cl S = ^C I = ^i o = A<CR> O = ko ea - append at the end of the current word
xp - transpose characters ddp - transpose lines yyp - duplicate line
g~{motion} - swap case gu{motion} - to lower case gU{motion} - to upper case gUaw J - join two lines > - shift left < - shift right
= - autoindent
gg=G - autoindent from start of file >G - increases the indentation from the current line until the end of the file
Movement
j | Down one real line gj | Down one display line k | Up one real line gk | Up one display line 0 | To first character of real line g0 | To first character of display line ^ | To first nonblank character of real line g^ | To first nonblank character of display line $ | To end of real line g$ | To end of display line --------------------------------------- w | Forward to start of next word b | Backward to start of current/previous word e | Forward to end of current/next word ge | Backward to end of previous word gg - to the start of file {line}G - go to line
Jump
:jumps <C-o> - to prev edition place <C-i> - to next edition place [count]G | Jump to line number //pattern <CR> /?pattern <CR> / n / N | Jump to next/previous occurrence of pattern % | Jump to matching parenthesis (/) | Jump to start of previous/next sentence {/} | Jump to start of previous/next paragraph H/M/L | Jump to top/middle/bottom of screen gf | Jump to file name under the cursor <C-]> | Jump to definition of keyword under the cursor ’{mark} / `{mark} | Jump to a markChanges
:changes g; - traverse backward through the change list g; - traverse forward through the change list see also `. and `^Selection
a) or ab | A pair of (parentheses) i) or ib | Inside of (parentheses) a} or aB | A pair of {braces} i} or iB | Inside of {braces} a] | A pair of [brackets] i] | Inside of [brackets] a> | A pair of <angle brackets> i> | Inside of <angle brackets> a’ | A pair of 'single quotes' i’ | Inside of 'single quotes' a" | A pair of "double quotes" i" | Inside of "double quotes" a` | A pair of `backticks` i` | Inside of `backticks` at | A pair of <xml>tags</xml> it | Inside of <xml>tags</xml> ---------------------------------------------------- iw | Current word aw | Current word plus one space iW | Current WORD aW | Current WORD plus one space is | Current sentence as | Current sentence plus one space ip | Current paragraph ap | Current paragraph plus one blank lineRegisters
P - paste the contents of our unnamed register in front of the cursor gp gP - leave the cursor positioned at the end of the pasted text <C-r>{register} - paste from register in Insert mode "{register} :reg "0 - content of register "" - unnamed "0 - yank "_ - black hole "+ - system "* - selection "= - expression "a-"z - named Register | Contents ----------------------------------- "% | Name of the current file "# | Name of the alternate file ". | Last inserted text ": | Last Ex command "/ | Last search patternSearch
f{char} | Forward to the next occurrence of {char} F{char} | Backward to the previous occurrence of {char} t{char} | Forward to the character before the next occurrence of {char} T{char} | Backward to the character after the previous occurrence of {char} ; | Repeat the last character-search command , | Reverse the last character-search commandMarks
m{a-zA-Z} ’{mark} Keystrokes | Buffer Contents -------------------------------------------------------------- `` | Position before the last jump within current file `. | Location of last change `^ | Location of last insertion `[ | Start of last change or yank `] | End of last change or yank `< | Start of last visual selection `> | End of last visual selection % - jump between opening and closing sets of parenthesesInsert mode
i - activate <C-h> - delete back one char <C-w> - delete back one word <C-h> - delete back to start of line <C-r>{register} - paste from register <C-a> - perform addition on numbers <C-x> - perform subtraction on numbers <C-r>= - open calculator {number}<C-a> set nrformats= this will cause vim to treat all numerals as decimal, regardless of whether they are padded with zeros.Insert unusial character
<C-v>{123} - Insert character by decimal code <C-v>u{1234} - Insert character by hexadecimal code <C-v>{nondigit} - Insert nondigit literally <C-k>{char1}{char2} - Insert character represented by {char1}{char2} digraphInsert normal mode
<C-o> - enableReplace mode
R - activateVirtual Replace mode (tab as spaces)
gR - activateVisual modes
gv - Reselect the last visual selection o - Go to other end of highlighted text vit - select inside tag U - to uppercaseCharacter-wise Visual mode
vLine-wise Visual mode
VBlock-wise Visual mode
<C-v>Command-Line mode
:[range]delete [x] | | Delete specified lines [into register x] :[range]yank [x] | | Yank specified lines [into register x] :[line]put [x] | | Put the text from register x after the specified line :[range]copy {address} | :t | Copy the specified lines to below the line specified by {address} :[range]move {address} | :m | Move the specified lines to below the line specified by {address} :[range]join | | Join the specified lines :[range]normal {commands} | | Execute Normal mode {commands} on each specified line :[range]substitute/{pattern}/{string}/[flags] | | Replace occurrences of {pattern} with {string} on each specified line :[range]global/{pattern}/[cmd] | | Execute the Ex command [cmd] on all specified lines where the {pattern} matches @: - repeat last ex command :registers <C-r><C-w> - copies the word under the cursor and inserts it at the command-line prompt <C-w> - delete backward to the start of the previous word <C-u> - delete backward to the start of the lineExamples
:1 :print :$ :p :3p :2,5p :.,$p % - all lines of file :%p :%s/Practical/Pragmatic/ :'<,'>p - visual selection :/<html>/,/<\/html>/p - range of Lines by Patterns :{address}+n - Modify an Address Using an Offset :/<html>/+1,/<\/html>/-1p :.,.+3p :%normal A; - adds semicolons to all file lines ends :%normal i// - comments all lines :col<C-d> - autosuggestions set wildmode=longest,list - like bash # like zsh: set wildmenu set wildmode=full :%s//<C-r><C-w>/g - copy word under cursor to substitute command :write | !ruby % - combine two commands with | signRanges
Symbol | Address -------|----------------------- 1 | First line of the file $ | Last line of the file 0 | Virtual line above first line of the file . | Line where the cursor is placed 'm | Line containing mark m '< | Start of visual selection '> | End of visual selection % | The entire file (shorthand for :1,$) Command | Effect ---------|------------------------------------------- :6t. | Copy line 6 to just below the current line :t6 | Copy the current line to just below line 6 :t. | Duplicate the current line (similar to Normal mode yyp ) :t$ | Copy the current line to the end of the file :'<,'>t0 | Copy the visually selected lines to the start of the file Command | Action --------------------------------------------------------------- q/ | Open the command-line window with history of searches q: | Open the command-line window with history of Ex commands ctrl-f | Switch from Command-Line mode to the command-line window Command | Effect ---------------------|--------------------------------------------- :shell | Start a shell (return to Vim by typing exit) :!{cmd} | Execute {cmd} with the shell :read !{cmd} | Execute {cmd} in the shell and insert its standard output below the cursor :[range]write !{cmd} | Execute {cmd} in the shell with [range] lines as standard input :[range]!{filter} | Filter the specified [range] through external program {filter} :2,$!sort -t',' -k2Repeat
Intent | Act | Repeat| Reverse ---------------------------------|-----------------------|-------|-------- Make a change | {edit} | . | u Scan line for next character | f{char} / t{char} | ; | , Scan line for previous character | F{char} / T{char} | ; | , Scan document for next match | /pattern <CR> | n | N Scan document for previous match | ?pattern <CR> | n | N Perform substitution | :s/target/replacement | & | u Execute a sequence of changes | qx{changes}q | @x | u * - search words under cursorFiles
:ls :bn(ext) :bprev(ious) :bdelete N1 N2 N3 :N,M bdelete <C-^> - switch back to the already editing buffer <C-g> - command echoes the name and status of the current file :args :args {arglist} :args index.html app.js :args **/*.js **/*.css :args `cat .chapters` :lcd {path} - command lets us set the working directory locally for the current window :set path? - inspect path value gf - go to file under cursor Command | Effect ---------|----------------------------------------- :w[rite] | Write the contents of the buffer to disk :e[dit]! | Read the file from disk back into the buffer (that is, revert changes) :qa[ll]! | Close all windows, discarding changes without warning :wa[ll] | Write all modified buffers to diskWindows
<C-w>s | Split the current window horizontally, reusing the current buffer in the new window <C-w>v | Split the current window vertically, reusing the current buffer in the new window :sp[lit] {file} | Split the current window horizontally, loading {file} into the new window :vsp[lit] {file} | Split the current window vertically, loading {file} into the new window Command | Effect ------------------------------------ <C-w>w | Cycle between open windows <C-w>h | Focus the window to the left <C-w>j | Focus the window below <C-w>k | Focus the window above <C-w>l | Focus the window to the right Ex Command | Normal | Command Effect --------------------------------------------- :cl[ose] | <C-w>c | Close the active window :on[ly] | <C-w>o | Keep only the active window, closing all others Keystrokes | Buffer Contents ----------------------------------------------------- <C-w>= | Equalize width and height of all windows <C-w>_ | Maximize height of the active window <C-w>| | Maximize width of the active window [N]<C-w>_ | Set active window height to [N] rows [N]<C-w>| | Set active window width to [N] columnsTabs
Command | Effect ---------------------------------------------------- :tabe[dit] {filename} | Open {filename} in a new tab <C-w>T | Move the current window into its own tab :tabc[lose] | Close the current tab page and all of its windows :tabo[nly] | Keep the active tab page, closing all others Ex Command | Normal Command | Effect --------------------------------------------------------------- :tabn[ext] {N} | {N}gt | Switch to tab page number {N} :tabn[ext] | gt | Switch to the next tab page :tabp[revious] | gT | Switch to the previous tab page :tabmove [N] - rearrange tabsRecipies
Open Files and Save Them to Disk
:pwd :edit lib/framework.js :edit % <Tab> - % symbol is a shorthand for the filepath of the active buffer :edit %:h <Tab> - :h modifier removes the filename while preserving the rest of the pathSave files to nonexistent directories:
:!mkdir -p %:h :writeSave a file as the super user
:w !sudo tee % > /dev/null :find Main.js :set path+=app/**netrw
set nocompatible filetype plugin on Ex Command | Shorthand | Effect ------------------------------------------------------------------------- :edit . | :e. | Open file explorer for current working directory :Explore | :E | Open file explorer for the directory of the active bufferPlugins
https://github.com/tpope/vim-commentary \\ap \\G \\\ - comment current line https://github.com/kana/vim-textobj-entire http://github.com/tpope/vim-surround - !!! Surround.vim p.129Settings
:set shiftwidth=4 softtabstop=4 expandtab
Nov 19, 2012
Unix as IDE
Text Processing
Echo a file without the first and last linessed '1d;$d' file.txt
How to remove all blank lines in fie
grep -v '^$' old-file.csv > new-file.csv
# or
grep -v '^$' file.csv | sponge file.csv
# or if lines may contain spaces
egrep -v '^[[:space:]]*$' old-file.csv > new-file.csv
Search
String searchegrep -r "findmestr" .
Search and replace in file
sed 's/replaceme/byme/g' ffff > TMPFILE && mv TMPFILE ffff
run grep with multiple AND patterns?
awk '/regexp1/ && /regexp2/ && /regexp3/ { print; }'
How to search a PDF file from command line?
pdftotext document.pdf - | grep -C5 -n -i "search term"
Bash
ShortcutsExport enviroment variable for a single command
PANTS=kakhi PLANTS="ficus fern" env | grep 'P.*NTS'
if [ -e afile ] ; then VAR=val ./program_using_VAR ; fi
Bash auto completion
For Django commands http://docs.djangoproject.com/en/dev/ref/django-admin/#bash-completionecho '. /path/to/django-trunk/extras/django_bash_completion' >> .bashrc
Filesystem
Change file extensioncp /opt/nginx/conf/nginx.conf{,.bak}
Search and remove all *.pyc files
find . -name "*.pyc" -exec rm -rf {} \;
Sync dirs
rsync -avuzr <from_dir> <to_dir>
Find files by modification date and copy them to folder
find . -mtime -1 -maxdepth 1 -exec cp "{}" /some/dist_dir/ \;
Archives
tar.bz2tar -cjf ../ffff.tar.bz2 *
tar -xjf ffff.tar.bz2
tar
tar xvf ffff.tar
tar xvfz ffff.tar.gz
Navigation
Dirsdirs -v
pushd # / popd
Send file(s) via ssh
cat filename | ssh -l login ip_address 'cat > filename'
# get folder
tar -cjf - /path/to/files/ | ssh -l root ip_address 'cd needed_directory; tar -xjf -'
Administration
How can I get my external IP address in bash?curl http://automation.whatismyip.com/n09230945.asp
Routing table
route
How can I find which process sends data to a specific port?
netstat -npl | grep 5432
Repeat a unix command every x seconds
watch -n1 command
How to log all the events performed on directory or file?
inotifywait -m -r /dir
Look for a process with a given name
ps -C python
Find the process ID of a running program:
pidof java
man mail
man sendmail
Monitoring
Network usagesudo apt-get install bwm-ng
bwm-ng
Measuring Web Server Performance
sudo apt-get install httperf
httperf --server 127.0.0.1 --port 8000 --uri / --num-conns 200 --add-header='Accept-Encoding: gzip,deflate\n'
Labels:
unix as ide
Nov 16, 2012
Nov 15, 2012
Vim netrw plugin
Opening remote server via scp (the same for ftp):
h - show/hide system files
s - change the sort order
:e scp://[user]@hostname/path/
i - change the listing styleh - show/hide system files
s - change the sort order
Labels:
vim
Nov 14, 2012
Text browser elinks
http://en.wikipedia.org/wiki/ELinks
elinks podkova.by
Labels:
seo,
text browser,
unix as ide
Nov 13, 2012
Enable vim's system and selection registers (clipboards) in Ubuntu
sudo apt-get install vim-gnome
Add in .vimrc:
set clipboard=unnamedplus
Labels:
vim
Nov 9, 2012
Gnome terminal jellybeans theme
Original colors from jellybeans vim color theme: https://raw.github.com/nanotech/jellybeans.vim/master/ansi-term-colors.txt
Python: calculate lighter/darker RGB colors http://chase-seibert.github.com/blog/2011/07/29/python-calculate-lighterdarker-rgb-colors.html
Final result using this solution: http://django-notes.blogspot.com/2012/11/gnome-terminal-color-schemes.html
Python: calculate lighter/darker RGB colors http://chase-seibert.github.com/blog/2011/07/29/python-calculate-lighterdarker-rgb-colors.html
>>> def color_variant(hex_color, brightness_offset=40):
... rgb_hex = [hex_color[x:x+2] for x in [1, 3, 5]]
... new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]
... new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int]
... return "#%02x%02x%02x" % tuple(new_rgb_int)
Lighter colors:
>>> ':'.join(map(color_variant, ("#3b3b3b","#cf6a4c","#99ad6a","#d8ad4c","#597bc5","#a037b0","#71b9f8","#adadad")))
'#636363:#f79274:#c1d592:#ffd574:#81a3ed:#c85fd8:#99e1ff:#d5d5d5'
Original colors:
>>> ':'.join(("#3b3b3b","#cf6a4c","#99ad6a","#d8ad4c","#597bc5","#a037b0","#71b9f8","#adadad"))
'#3b3b3b:#cf6a4c:#99ad6a:#d8ad4c:#597bc5:#a037b0:#71b9f8:#adadad'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/foreground_color '#adadad'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/bold_color '#adadad'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/background_color '#151515'
gconftool --set --type string /apps/gnome-terminal/profiles/Default/palette '#3b3b3b:#cf6a4c:#99ad6a:#d8ad4c:#597bc5:#a037b0:#71b9f8:#adadad:#636363:#f79274:#c1d592:#ffd574:#81a3ed:#c85fd8:#99e1ff:#d5d5d5'
Vim color schemes
http://www.vimninjas.com/2012/08/26/10-vim-color-schemes-you-need-to-own/
https://github.com/flazz/vim-colorschemes/tree/master/colors
https://github.com/flazz/vim-colorschemes/tree/master/colors
sudo apt-get install ncurses-term
vim ~/.bashrc
export TERM=xterm-256color
vim ~/.vimrc
set t_Co=256
Labels:
vim
Nov 8, 2012
Nov 6, 2012
Subscribe to:
Posts (Atom)