Text Manip
Contents
Utils
sed -i 's/GBP/INR/g' .html //replace all instances of "GBP" in all .html files to "INR" sed -i 's~dir/file~dir/dir/file~g' // can use any delimiter (~ in this case) to find/replace text with "/" in it
cat order. | sort | grep Patterns //// concatenates all files starting with order., sorts and looks for the word Pattern 8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99 cat order. | sort | grep Patterns | cut -d"," -f2,5 ///// as above + splits/cuts using delimiter "," and keeps 2nd and 5th entries 1, 39.99 -1, 39.99
cat regex_practice | sed s/"[0-9\:] ([0-9])\, (.*)"/"\2, '\1'"/ //// sed / regexp / replacement / Note the indexes on 'replacement'. Reqd. regexp needs to be surrounded by ( ... ) to be included in the index count
cat regex_practice | grep "Kindle|Hardcover" | cut -d"," -f3 | sort | uniq -c ////// counts the instances of
tail -f orderbook.log //// automatically less orderbook.log //// can use / for forward search and ? for backward search when in 'less' find /home/sridhar -name "filename*" find /tmp/folder/ -type d -name keys -exec cp /tmp/a.kool {}\; ///////// copy a.kool into all sub-directories (direct or not) of /tmp/folder/ called 'keys' find ./ -name build.xml -exec grep "search_string" {} \; ////// search for search_string in all files called build.xml under ./ find ./ -name build.xml -exec sed -i 's/old_string/new_string/g' {} \; //// replace with new string all strings found above
find ./ -name build.xml -exec sed -i 's|version1.5"/>|version1.5"/>-->|' {} \;
Couldn't escape backslashes in regexp so best option is to use sed because the arguments in sed s/old/new/ are plain strings w/o the need for surrounding single or double quotes or escaping special characters. So for instance even this is allowed:
find . -path './*/src/golang.gurusys.co.uk/vendor' -prune -o -type f -exec egrep 'SetAlarm' {} + //ignore directory tree vendor in find/grep
find /etc/rc -exec echo Arg: {} ';' // executes command separately for each argument find /etc/rc -exec echo Arg: {} '+' // executes least possible arguments
cat build.xml | sed 's|version1.5"/>|version1.5"/>-->|'
Markdown
bold: text italics: * text * links: • inline: Google • ref-style [Google][1] [1]: http://www.google.com • readable [Google][google] [google]: http://google.com lists: -/+/*
new line: two spaces at end to force new line
strikethrough: text
Vim
basics
vim
vim file_name
vim -O file1 file2 file3 // open files in vertically split windows
vim -o file1 file2 file3 // open files in horizontally split windows
i to insert
esc to stop insert
w/b to move forward/back by a word
G$ to move to end of file
gg to move to start of file
:e! to refresh file from buffer
:set list // view end of line characters
x to delete
:x to exit/save
dd // delete line
d$ // delete until end of line
:<command> // : used if in vim, not reqd. if using .vimrc
:tabe <filename> : opens file in new tab
:tabn : next tab
:tabp : previous tab
:tabnew : open new unnamed tab
:tabo : close all other tabs
:set syn=scala : sets syntax to scala - needs scala.vim in ~/.vim/syntax
:set spell spelllang=en_gb
:set shellcmdflag=-ic
nmap <C-n> :tabn<CR> //set in ~/.vimrc
nmap <C-p> :tabp<CR> //set in ~/.vimrc
:syntax on //set in ~/.vimrc
:r <filename> //inserts contents of file below cursor
:r! <command> //inserts results of command below cursor
:% !<cmd> // passes current buffer through command
:% !goimports // adds required imports to a go src file
:% s/keyword/new_keyword/g //find all instances of keyword and replace with new_keyword
* // find next occurrence of word under cursor
100>> //to indent 100 lines
5k // move 5 lines up
5j // move 5 lines down
set shiftwidth=2 //in ~/.vimrc
au BufNewFile *.go 0r ~/.vim/go.skel | let IndentStyle = "go"
:vnew // new vertically split file
:vnew | r! <command> // output of command in new vertically split file
:w <newfilename> // writes to newfilename but continues with <oldfile>
:wa //saves all open tabs
:qa //closes all open tabs
:sav <newfilename> //writes to newfilename, closes <oldfile> and opens <newfile>
:w !sudo tee % // saves file if opened without root access
:grep -r xyz ./ | vim - //pipes results of grep into new file in vim
//Different tab setting for different files types. 8 for .go, 4 for .java
au FileType java setl sw=4 sts=4 et
au FileType go setl sw=8 sts=8 et
macros
Macros get recorded to registers
qa // starts recording macro into register named 'a'
q // stops recording
@a // repeats macro actions recorded
"a // access content of macro - can paste it using "ap
^ // goes to end of line - useful while recording macro
$ // goes to start of line - useful while recording macro
Can save macro in .vimrc
let @r = '8k8yy7jp' // apply macro using @r
registers
:reg // list registers
:reg a b c // filter registers to view
"0 // latest yanked text
"9 // oldest yanked text
So to paste the 5th oldest yanked text type "5p
or in insert mode <Ctrl-r>5
4 read-only registers
". // last inserted text
"% // current file path
": // most recent executed command. can repeat last command using @:
"# // confusing - maybe last edited file
"= // expression register
Ctrl-r (in insert mode) followed by any of the registers will paste register value:
Ctrl-r = // activates cmdline with '=' can do arithmetic operaion like 3*5 will paste 15
Need vim-gtk to use *
and +
registers:
sudo apt-get install vim-gtk
"+y // yanks visual mode selection into clipboard which can be pasted using Ctrl-V outside vim
:%y + // yanks entire buffer into clipboard which can be pasted using Ctrl-V outside vim
windows
vim -o file1.go file2.go // opens files in windows
:vs file2.go // open file2.go in new vertically split window
:sp file2.go // open file2.go in new horizontally split window
Ctrl-WW // toggles between windows
Ctrl-W +|> // increases height|width of current window
Ctrl-W -|< // decreases height|width of current window
v // Visual mode where arrow keys can be used to select text
y //yanks selection in visual mode
{Visual} "xy // In Visual mode "xy yanks selected text into register x to be pasted later in normal mode
vep // pastes whatever is in buffer to replace word under cursor
zc // folds
zo // unfolds
u //undo
Cntl R //redo
N gg // go to line N
N k // go up N lines
N j // go down N lines
diff -y --suppress-common-lines <(head -500 file2) <(head -500 file2)
operations
gUw //word to upper case
guw //word to lower case
~ //toggle case of character under cursor
% // when used on an open parenthesis/brace jumps to the corresponding close
:digraph
emacs
C-h C-h : help
C-g : quit
C-x b : switch buffers
C-x right : right-cycle through buffers
C-x left : left-cycle through buffers
C-x k : kill buffer
C-x 0 : close the active window
C-x 1 : close all windows except the active window
C-x 2 : split the active window vertically into two horizontal windows
C-x 3 : split the active window horizontally into two vertical windows
C-x o : change active window to next window
C-x C-f : open file
C-x C-s : save file
C-x C-w : save file as
C-space : set region mark
C-w : kill region
C-k : kill region between point and end of current line
M-w : kill region without deleting
C-y : yank region from kill ring
M-y : move to previous item in the kill ring
M-Y : move to next item in the kill ring
C-_ : undo
C-s : search forwards
C-r : search backwards
M-% : query replace (‘space’ to replace, ‘n’ to skip, ‘!’ to replace all)
M-q : wrap text
C-left : move one word left
C-right : move one word right
C-up : move one paragraph up
C-down : move one paragraph down
home : move to the beginning of the line
end : move to the end of the line
page up : move up a page
page down : move down a page
M- : move to end of buffer
tmux
Split screen vertically: Ctrlb and Shift5 Split screen horizontally: Ctrlb and Shift" Toggle between panes: Ctrlb and o Close current pane: Ctrlb and x
You can achieve more complex layouts by splitting panes. You can also have multiple windows with panes and switch between them.
Create windows: Ctrl-b and c Switch to next window: Ctrl-b and n Switch to previous window: Ctrl-b and p Destroy current window: Ctrl-b and Shift7 // or exit
Remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
Split panes using | and -
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %
Copy/paste
C-a [ // start
C-space // move to end
Alt-w // copy into buffer
C-a ] // paste works in vim visual mode as well
tmux rename-window doofus
C-a, rename-window
Misc
gedit syntax highlighting: /usr/share/gtksourceview-2.0/language-specs/scala.lang