Wednesday, August 26, 2015

git shell commands

You can install GitHub/Git Shell from http://desktop.github.com
This DOES NOT require admin privilege (will get a small install stub of 116kb
which will again download around 112 Mb for windows client

Use only PowerShell to get files affected count is command prompt. If Git Shell opens 
some other shell like windows cmd or ming, open GitGUI and change shell type in settings.


http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html

-----------------------------------------------------------------------------
know quickly the current working [branch] 
how many new files added(+),  # files modified(~), # files deleted(-)
-----------------------------------------------------------------------------
C:\folder [master +0 ~0 -1]> 
               |  |  |  |
               |  |  |  |__ # of files deleted
               |  |  | 
               |  |  |__ # of files modified
               |  |
               |  |___ # of new files added
               |
               |___ branch
 

-----------------------------------------------------------------------------
Create new git repo and clone
-----------------------------------------------------------------------------
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin http://github.com/nerunja/myproject.git
git push -u origin master
git clone -b master http://github.com/nerunja/myproject.git

-----------------------------------------------------------------------------
Ignore a file accidentally deleted in workspace from commiting
-----------------------------------------------------------------------------
Ignore a file deleted which is pending commit/push

C:\folder [master +0 ~0 -1]> git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        deleted:    file1.txt

no changes added to commit (use "git add" and/or "git commit -a")
C:\folder [master +0 ~0 -1]> git checkout -- file1.txt
C:\folder [master +0 ~0 -1]> git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean
C:\folder [master]>
-----------------------------------------------------------------------------
Remove/Rename a file from repo
-----------------------------------------------------------------------------
$git rm
$git mv
-----------------------------------------------------------------------------
checkout any older revision by looking at commit comments
-----------------------------------------------------------------------------
Start with:
$ git log
which shows you a list of recent commits with comments, and their SHA1 hashes.
Next, type:
$ git checkout  file
-----------------------------------------------------------------------------
Remove untracked files 
-----------------------------------------------------------------------------
$git clean --force

Remove ONLY ignored files (files specified in .gitignore)
$git clean --force -X

Remove both ignored files and untracked files
$git clean --force -x (note small x)

Remove untracked files INCLUDING the directory containing and also the gitignored files
$git clean -xdf

-----------------------------------------------------------------------------
$ git diff filename
-----------------------------------------------------------------------------
Amend message of last commit that is pending push

git commit --amend -m "New commit message"
-----------------------------------------------------------------------------
View latest n commit messages (say 5)
$ git log --oneline -n 5
-----------------------------------------------------------------------------
The ultimate log output
git log --oneline --graph --decorate [filename]

* 129cce6 (origin/master, origin/HEAD, master) Merge branch 'testing'
|\
| * a86067a (origin/testing, testing) testing branch commit
* | 1a36bd6 master branch commit
-----------------------------------------------------------------------------
show the commit, person, and date on which each line of file
git blame --date=short <filename>
-----------------------------------------------------------------------------
Review the changes made to a file
git diff <commit hash> <filename>

to see diff with previous commit use HEAD~1 instead giving the actual commit hash
$ git diff HEAD~1 <filename>

Then to revert a specific file to that commit use the reset command:
$ git reset <commit hash> <filename>

You may need to use the --hard option if you have local modifications.

-----------------------------------------------------------------------------
See all the file changes across between current and previous commit
- Just don't mention the filename in git diff 
$ git diff HEAD~1

-----------------------------------------------------------------------------
Unable to pull (after committing local changes) from repo due to local modifications in a file. 
   How to discard local changes on specific files 
  (Note: Files with modifications that need to be retained SHOULD be committed first before pull command)
$ git checkout -- <filename that has been modified in local / working directory >
$ git pull 
$ git push   (now it will successfully push committed changes)
-----------------------------------------------------------------------------
Reset git HEAD in current/any branch by moving & placing HEAD over any other commit
Resetting is a way to move the tip of a branch to a different commit. 
This can be used to remove commits from the current branch. 

$git reset --hard HEAD
$git config --global push.default current
$git pull

The following command moves the hotfix branch backwards by two commits. 

$git checkout hotfix
$git reset HEAD~2
                            hotfix/HEAD
                            |
                            |
                        |---A---B--C
                        |
------------a---b---c---d----e
                             |
                             |
                             master


The parameters passed to git reset and git checkout determine their scope. 
When you don’t include a file path as a parameter, they operate on whole commits. 
Note that git revert has no file-level counterpart.
git revert is ALWAYS at commit level.
-----------------------------------------------------------------------------
git revert
Reverting undoes a commit by creating a new commit. 
This is a safe way to undo changes, as it has no chance of re-writing the commit history. 
For example, the following command will figure out the changes 
contained in the 2nd to last commit, create a new commit undoing those changes

git revert HEAD~2

Command Scope Common use cases
git reset Commit-level Discard commits in a private branch or throw away uncommited changes
git reset File-level Unstage a file

git checkout Commit-level Switch between branches or inspect old snapshots
git checkout File-level Discard changes in the working directory

git revert Commit-level Undo commits in a public branch
git revert File-level (N/A)

-----------------------------------------------------------------------------
git log --after="2016-1-1" --before="2016-1-10" --author="Username "
-----------------------------------------------------------------------------
Git Reset to a previous commit:

Steps:
1. Clone the respective branch into a new directory
2. Git reset to previous commit by below command
   git reset --hard <CommitId to which we need to point back> [ex:ab870c42194351535d0d35d1f7200b78cf3f41ea]
3. git push -f [force push to the specified commit].
-----------------------------------------------------------------------------
Save/Stash changes in working directory temporarily and get the committed version before this local changes
NOTE: every git stash pushes the previous stashed ref to next number and makes the current stash available at ref stash@{0}. It works like a STACK. LIFO (last-in-first-out)

$git stash [save] (save param is optional)
$git stash list
$git diff stash@{0}
$git stash pop (applied changes saved in stash stack on working directory)
$git clear clear (remove everything from stash)

-----------------------------------------------------------------------------

Typical usage of STASH ( temporarily move the local changes to avoid conflicts)

$git stash
$git reset --hard
$git pull origin dev
$git stash apply

-----------------------------------------------------------------------------



Tuesday, August 18, 2015

Eclipse

Ctrl + Shift + P     - find matching brace / bracket
double click on 1st char after opening " - select entire string
double click on 1st char after opening { - select enter code block
double click on 1st char after opening ( - select enter argument
Ctrl + Shift + F - Format selected block of code
main   Ctrl+Space - public static void main(String args[]){}
sysout Ctrl+Space - System.out.println();

http://stackoverflow.com/questions/10637702/eclipse-quick-search-on-filename Quickly for a file / resource in the project Open Resource Shift+Ctrl+R for all resource files (including java files) Open Type Shift+Ctrl+T for all java classes in classpath They also filter the list as you type. Note that they search all files of all opened projects in current workspace.

Eclipse auto-complete (Ctrl + Space) and hyperlink navigation NOT working for a Maven Import Project In Project Explorer Right Click on the ProjectFolder From the contect menu Click on Maven --> Update Project... (Alt + F5)

Swith to Editor

Ctrl + Shift + e

Opening eclipse getting error could not load jvm.dll This happens when JDK's 32 or 64 bit is NOT same as that of Eclipse. Try opening eclipse with both 32bit version and 64bit version. One of it should fix this problem


Monday, August 10, 2015

Vide Editing ... CUT / CONVERT / INCREASE VOLUME OF MPEG/MP4 VIDEOS USING FFMPEG OPEN SOURCE TOOL


Download ffmpeg from https://www.ffmpeg.org/download.html for your OS

Cut/Extract portion of video between starting time stamp 00:03:55 and till 00:04:44 
ffmpeg -i D:\invideo.mp4 -ss 00:03:55 -to 00:04:44 -async 1 -strict -2 outvideo.mp4


If the -ss [Start time] is far from beginning of the video file, then
it takes long time before it starts generating the output file.
DO NOT kill the command thinking it's not running.

Convert DVD vob file to mp4 
ffmpeg -i D:\invideo.VOB -ac 2 -ab 128k -vcodec libx264  -threads 0 outvideo.mp4

Increase/Decrease/Mute volume in mp4 video
Increase volume by 10dB (decibel)
d:\ffmpeg\bin\ffmpeg -i invideo.mp4 -vcodec copy -af "volume=10dB" outvideo.mp4

Decrease volume by 10dB (decibel)
d:\ffmpeg\bin\ffmpeg -i invideo.mp4 -vcodec copy -af "volume=-10dB" outvideo.mp4

------------
Other softwares
Linux - YouTube Downloader;
from Menu  --- Software Manager search for 'You Tube Downloader' youtube-dl will be installed

Linux - Video Editing GUI Tool -- ShotCut

----
Installing ffmpeg in Mint Linux 17.x
https://mintguide.org/video/339-installing-ffmpeg-library-on-linux-mint-via-ppa.html

sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg



Saturday, August 8, 2015

GNU on Windows (unix commands on windows)


https://github.com/bmatzelle/gow/wiki
Install the GOW (GNU On Windows) from https://github.com/bmatzelle/gow/releases
unix find command is available as gfind

Wednesday, August 5, 2015

VIM Commands

VIM Commands (updated: 15-May-2016)
------------------------------------------------------------------------------------
SOME QUICKIES
:/Crtr+R* - Paste system clipboard text into command line to search the same
:bro[wse] ol[dfiles] - type the number and enter [press spacebar to view more files, note the of file num to edit, press q] 
:ol[dfiles]  - show recent files list. To choose file n :e #<n
:Vex d:\n (press tab) - open vertically split explorer showing d:/n... folders, press  
:tabnew        - open a new tab for a new untitled [No Name] file
:browse tabnew - open a new tab with file open dialog
i       - Insert text from cursor
I       - Insert text from START OF LINE
a       - Append text at char AFTER cursor
A       - Append text at end of line
R       - Overwrite mode until ESC
C       - Change from cursor to end of line
H       - Move to top line in the current screen
M       - Move to middle line in the current screen
L       - Move to bottom line in the current screen
w,W     - move forward  by word, place cursor at beginning of word (W leaps over special chars)
b,B     - move backward by word, place cursor at beginning of word (B leaps over special chars)
e,E     - move forward  by word, place cursor at end       of word (E leaps over special chars)
ggVG    - Select all(gg - goto top of file; V - line select mode, G - goto end of file)
ggVG"*y - Select all and copy to system clipboard
"*yy    - yank/copy to system clipboard from cursor till EOL without new line
vt"y    - Select text till char before " (closing quote) and COPY to vim clip. Useful 
          to copy string values. Place cursor on 1st char after starting " quote and performing this.
vt"p    - Select text till char before " (closing quote) and PASTE from vim clip. Useful 
          to overwrite string values. Place cursor on 1st char after starting " quote and performing this.
v%      - select block of text enclosed within {},(), [], WHILE cursor is on opening {,(,[
*       - find next occurrence of the word under cursor
#       - find prev occurrence of the word under cursor
:'<,'>s/red/green/g - find/replace only within visual selected block of lines
                      first select lines in visual mode and press : it will automatically enter the
                      range '<,'> and then type the command s/red/green/g (replace red with green)
:s/\%Vold/new/g  - replace the text 'old' to 'new' ONLY WITHIN the VISUAL selection (using atom %V)
                   (the difference is here it doesn't include the whole line in start of visual or
                   of of visual select but exactly from the start/end character)

CHANGE DEFAULT FONT
http://www.troubleshooters.com/linux/vifont.htm
gVim -- Edit --> Startup Settings
     gVim74 will open _vimrc file from vim installation / user home folder)
From command mode type
:set guifont=*
  this opens font dialog window
  choose any font / size (e.g Lucida Console / size 12)
To make this default font from command line type
:set guifont?
  you can see the font settings in vim command line something like below based on the font/size chosen

set gfn=Lucida_Console:h12:cANSI
or
set guifont=Lucida_Console:h12:cANSI
copy/paste the above like to _vimrc file to make this font change permanent
Note: any space after = in the command set gfn= should be escaped with a slash like
set guifont=Monospace\ 12


CHANGE DEFAULT VIM COLOR SCHEME
Note the color scheme name as seen at gVim -- Edit -- Color Scheme
open _vimrc file, add line
colors slate

------------------------------------------------------------------------------------
TIP 1: Create a line like -------------------- consisting of such chars
i-<Esc>x100p
           i - Enter into insert mode
           - - type the required char (-) using which the line is made
           <Esc> - Escape back to normal mode from insert mode
           x - Delete the char - (also copied to unnamed register ")
           100 - repeat 100 times the the following command 
           p   - paste 100 times the char from unnamed register "
------------------------------------------------------------------------------------
TIP 2: Replace ALL comma (,) in a line with EOL (End Of Line)
:%s/,/\r/g
------------------------------------------------------------------------------------
MOVE AROUND

h, j, k, l - Move Cursor (left, down, up, right respectively)
gj,gk      - Move Cursor down/up across same which is wrapped in the screen
% - Move to associated ( ), { }, [ ] cursor SHOULD be on  ( ), { }, [ ]
[( - Moves cursor to previous available parenthesis
]) - Moves cursor to next available parenthesis
[{ - Moves cursor to previous available bracket
]} - Moves cursor to next available bracket
H - Move to top line in the current screen
M - Move to middle line in the current screen
L - Move to bottom line in the current screen
w - Move forward word by word
0 - (num 0) Move to beginning of line
^ - Move to beginning of line with non white-space char
$ - Move to end of line
o - add a new line below current (caps letter O add new line above current line)
A - append text at end of line
J - join line
Ctrl f - Next page
Ctrl b - back page
Ctrl d - down half page
Ctrl u - up half page
Ctrl l - refresh screen
gg - Move to top of file
GG - Move to end of file
100G - goto line 100
10| - goto column 10
( - jump backward one sentence
) - jump forward  one sentence
{ - jump backward one paragraph
} - jump forward  one paragraph
e -  go to the end of the current word
E -  go to the end of the current WORD
b - go to the previous (before) word
B - go to the previous (before) WORD
w - go to the next word
W - go to the next WORD
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word -  word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
    192.168.1.1 - single WORD
    192.168.1.1 - seven words.
Ctrl v - Block/Column selection mode
         e.g
         Select a block by pressing CTRL-v 
          (if you want to select 5 lines from a given column till end of line, after 
          Ctrl-v, press j 5 times to select 5 lines and $ to select till end of lines for those 5 lines)
          Type I (Block Insert mode), 
          Edit the text. For example, if I wanted to prepend bullet points before each line type *
          Press 
          Watch as each line is prepended with *
          to read about "Visual-block Insert" type :help v_b_I)
Ctrl q    - Block / column wise selection mode
Alt Mouse - Block / column wise selection mode (gVIM only)
            (place cursor from where block select should be done
            hold down ALT key, drag-select using mouse)
------------------------------------------------------------------------------------
SCREEN MOVEMENT
z.  Center the screen on the cursor
zt  Scroll the screen so the cursor is at the top
zb  Scroll the screen so the cursor is at the bottom
------------------------------------------------------------------------------------
BOOKMARKS

m{a-zA-Z} - mark cursor position
`{a-z} - goto marked cursor position (line & column) only in current file (backtick key)
'{a-z} - goto marked line (cursor at the beginning of line) only in current file
`{A-Z} - goto marked cursor position (line & column) only in current file (backtick key)
'{A-Z} - goto marked line (cursor at the beginning of line) in any file
(you may be editing ten files. Each file could have mark a, but only one file can have mark A)
'' - goto previous line position and place cursor at the beginning of line(two single quotes key) `` - goto cursor position before the latest jump and place cursor exactly at previous line & column
(two back ticks above tab key) (To jump to a mark enter an apostrophe (') or backtick (`) followed by a letter.l
Using an apostrophe jumps to the beginning of the line holding the mark, while
a backtick jumps to the line and column of the mark.) '. - goto previously modified line ------------------------------------------------------------------------------------ DELETE Commands X - backspace (backward delete char) x - forward delete char dw - delete word d$ - delete to end of line dd - delete line (cut, p - to paste this) U - undo undo u - Undo (repeat to get to the oldest change) Ctrl r - Redo (repeat to get to the latest change) ce - change to end of word c$ - change to end of line ~ - change case ------------------------------------------------------------------------------------ MACROS . - repeat the last command ^N - auto complete qa - record macro at a @a - replay macro a ------------------------------------------------------------------------------------ GVIM STARTUP SETTINGS Create ~/.vimrc file (In Windows GVim7.4 Edit->Startup Settings, will create _vimrc file. Enter the following commands in the file and save it.) "turn on auto numbering set num "highlight search set hls set incsearch "wrap text (line break) set lbr syntax on set guifont=Lucida_Console:h9:cDEFAULT From command line to dynamically change WRAP TEXT ------------------------------------------------- :set wrap :set nowrap ------------------------------------------------------------------------------------ SEARCH / - search forward ? - search backward n - next match N - prev match * - Go to the next occurrence of the current word under the cursor. # - Go to the previous occurrence of the current word under the cursor. :noh - clear highlight or previous search (no highlight) ------------------------------------------------------------------------------------ SPLIT VIEWS :vsp - create vertical split of current window :sp - create horizontal split of current window :Sexplore - browse by splitting a new horiz window :Explore - browse directory, o any file to open :Vexplore - Explore with vertical window ------------------------------------------------------------------------------------ CUT/COPY/PASTE v - enter visual selection mode followed by use any movement commands to select text select block of text using h,j,k,l, $, } etc.,(any movement command) V - visual select whole line (ref. MOVE AROUND sec. here for more details) Ctrl-v - Block / column wise selection mode Ctrl-q - Block / column wise selection mode Alt+Mouse - Block / column wise selection mode (place cursor from where block select should be done hold down ALT key, drag-select using mouse) y - copy/yank x - cut p - paste yVj - copy 2 lines (V select current and each j after that selects consecutive lines) COPY NON-CONSECUTIVE MULTIPLE LINES INTO A REGISTER ------------------------------------------------------------------------------------ To copy 2 lines into register a "aY - one line is copied into register "a (Y is synonym for yy - yank line) "AY - after moving to a different line, append this line to register a (referring register in capital appends instead of over writing) "ap - this pastes 2 lines copied as above COPY A HTML TAG BLOCK / FRAGMENT ------------------- vat - Place the cursor on tag say .. and then type this command. Selects full block including .. vit - While the cursor in on inner text of an html tag block type this. e.g for the tag my sample text typing vit while cursor is on m of my selects the text 'my sample text' vat - for the above same example selects the whole my sample text fragment yiw - yank inner word (copy word under cursor, say "first") ... Move the cursor to another word (say "second") viwp - select "second", then replace it with "first" ... Move the cursor to another word (say "third") viw"0p - select "third", then replace it with "first" vg_y - yank/copy to vim clipboard from cursor till EOL without new line vg_"*y - yank/copy to system clipboard from cursor till EOL without new line "*yy - yank/copy to system clipboard from cursor till EOL without new line "ay - register 'a' copies text already selected ( using v(visual mode) command) "ayy - register 'a' copies(yanks) current line "ap - register 'a' contents pasted s - substitute (cut/paste) R - Replace(overwrite) mode until ESC r - replace single char Multiple clipboards using registers a-ZA-Z e.g "fy - copy firstname to register f (after selecting firstname in visual mode) "ly - copy lastname to register l (after selecting lastname in visual mode) "fp - paste firstname from register f (after selecting the text to be overwritten if needed) "lp - paste lastname from register l (after selecting the text to be overwritten if needed) ------------------------------------------------------------------------------------ PASTE IN INSERT MODE 1. Get into insert mode using any commands like i, a, A etc., 2. press Ctrl R" (Press Control R and ") at the place you want to insert text from last yank/copy or press Ctrl R* (Press Control R and *) at the place you want to insert text from SYSTEM clipboard or press Ctrl R (Press Control R and any register that has saved text) NOTE: " (quote) is register command and Ctrl R automatically gets into read from register mode notice the " char at cursor position) once you press Ctrl R, which means we need to press only the register a-ZA-Z from which we need to read the text from. "" - register always has the latest/last yanked text "* - has the SYSTEM clipboard text a-zA-Z - has user stored texts in registers a though z or A through Z ------------------------------------------------------------------------------------ SYSTEM CLIPBOARD "* - represents system clipboard "*yy - copies current line to system clipboard "*p - pastes content from system clipboard e.g v$"*y - enter visual mode, select till EOL and copy to system clipboard v}"*y - enter visual mode, select paragraph and copy to system clipboard ------------------------------------------------------------------------------------ :wa - save all files :xa - exit all saving changes ZZ - close and exit :bufdo bd - close all buffers :b 1 - switch to buffer 1 :b - offers all buffers in a menu ( :b is short cut of buffer) :b car - offers car.c car.h etc., from buffers :b! 2) would allow to hide buffer 1 (keeping its changes), and display buffer 2 Set the hidden option (:set hidden) so any buffer can be hidden (keeping its changes) without first writing the buffer to a file. This affects all commands and all buffers. Set the confirm option (:set confirm) so that when you tell Vim to abandon a buffer but you have unsaved changes, Vim will ask you whether to save your changes first, abandon them, or cancel the action. The choices given do not seem to allow simply hiding the buffer as in the previous options. Set either the autowrite or the autowriteall options (:set autowrite or :set autowriteall) to automatically save any changes made to the buffer before it is hidden. vim -p file1.txt file2.txt - open multiple files :tabs - list all tabs :tabm0 - move to first tab :tabfirst - move to first tab :tabm - move to last tab :tablast - move to last tab :tabn - move to next tab gt - move to next tab :tabT - move to previous tab gT - move to previous tab 0gt or 1gt - move to first tab :tabclose - close current tab :tabonly - close all other tabs except current ------------------------------------------------------------------------------------ FIND & TILL fx - find next x Fx - find prev x tx - find next x but just before x Tx - find prev x but just after x ; - Repeat latest f, t, F or T [count] times , - Repeat latest f, t, F or T in opposite direction [count] times dtx - delete till x dfx - delete till and including x ctx - change till x cfx - change till and including x :g/pattern/command - apply command to all matches for the pattern e.g :g/temp/d - delete all occurrences of the word temp :1,$/^#/d - delete all lines starting with # (1,$ is equal to g) :g!/^#/d - delete all lines NOT starting with # :5,10m0 - move lines 5 to 10 above 1st line of file ------------------------------------------------------------------------------------ FIND/REPLACE ONLY WITHIN VISUAL SELECTED LINES When text is visually selected, press : to enter a command. The command line will automatically enter the range: :'<,'> :'<,'>s/red/green/g NOTE: '< visual selection automatically bookmarks start line in the key < '> visual selection automatically bookmarks end line in the key > `< (back tick key above tab) visual selection automatically bookmarks start character in the key < `> (back tick key above tab) visual selection automatically bookmarks end character in the key < (only the access key ' and ` is different for LINE and CHARACTER position bookmark as they both are bookmarked using < and > registers gv - command selected the last visual selection made (using the above bookmark key) ------------------------------------------------------------------------------------ FIND/REPLACE ONLY WITHIN VISUAL SELECTED TEXT The substitute(s command) by default works on line mode search. To consider find/replace ONLY within the characters visually selected, Autom %V is used :s/\%Vold/new/g - replace the text 'old' to 'new' ONLY WITHIN the VISUAL selection (using atom %V, \before % is escape char) ------------------------------------------------------------------------------------ FIND/REPLACE WHOLE WORD AND DON'T MATCH PARTIAL TEXTS Original Text: This is his idea enclose the word to be matched within \< and \> :s/\/her/ Translated Text: This is her idea (Note: if \< and \> weren't used the the his in the word 'This' also would have got changed) ------------------------------------------------------------------------------------ FIND MULTIPLE WORDS USING REGEX WORD AND REPLACE Original Text: Linux is good. Life is nice. :%s/\(good\|nice\)/awesome/g Translated Text: Linux is awesome. Life is awesome. ------------------------------------------------------------------------------------ FIND/REPLACE TO PREFIX LINE NUMBER IN ALL LINES :%s/^/\=line(".") . ". "/g ------------------------------------------------------------------------------------ FIND/REPLACE TEXT HAVING SPECIAL CHARACTERS Original Text: c:/windows/system32/ %s!/!\\!g Translated Text: c:\windows\system32\ NOTE: Here we are using ! as argument separator instead of / in the substitute(s) command first \ in \\ is for escaping FIND/REPLACE :%s/fff/rrrrr - % represents whole file - for all lines in a file, find string fff and replace with rrrrr only for 1st occurrence :%s/fff/rrrrr/g - replace all occurrences :%s/fff/rrrrr/gc - replace all with confirmation :%s/fff/rrrrr/gi - ignore case :'a,'bs/fff/rrr/gi - for lines between line marked "a" (ma) and marked "b" (mb) :5,20s/fff/rrrrr/gc - for all lines between 5 and 20 :%s/*$/ - for all lines in a file, delete blank spaces at end of line :%s/\(.*\):\(.*\)/\2:\1/g - for all lines in a file, move last field delimited by ":" to the first field.
Swap fields if only two :%s#<[^>]\+>##g - find and remove all HTML tage but keep the text contents :%s/^\(.*\)\n\1$/\1/ - find and remove all duplicate lines Find/Replace in ALL files The following performs a search and replace in all buffers (all those listed with the :ls command): :bufdo %s/pattern/replace/ge | update (use :wa to save all buffers after the changes) If you don't wish to save the results of your replacement, but want to review each changed
buffer first, you can force the bufdo to continue without saving files with bufdo!:
:bufdo! %s/pattern/replace/ge Find/replace is in both current directory and all sub-directories for given set of file extensions :arg **/*.cpp - All *.cpp files in and below current directory. :argadd **/*.h - And all *.h files. :arg - Optional: Display the current arglist. :argdo %s/pattern/replace/ge | update - Search and replace in all files in arglist Replacing current word (under cursor) if all files :arg *.cpp All *.cpp files in directory. :argadd *.h And all *.h files. ... Move cursor to word that is to be replaced. * Search for that exact word. :argdo %s//replace/ge | update Search and replace in all files in arglist. ------------------------------------------------------------------------------------ SORTING :'a,'b !sort - sort block of lines between and including marked by ma and mb ------------------------------------------------------------------------------------ REVERSING LINES :'a, 'b !tac - reverse of order of lines in the block marked ma and mb :r !!date - replace with date by executing shell command date ( 2 exclamations required in windows) :sh - open a shell :exit - exit back to vim ------------------------------------------------------------------------------------ TEXT OBJECT is, as - represent text object - is ignores spaces - as includes space e.g., das - delete current sentence including spaces ------------------------------------------------------------------------------------ USING COMMANDS FROM HISTORY type : and pres up/down arrow to pick from recent list. This method allows to edit the command before using it q: - (type in normal mode) lists command history in [Command Line] window use movement keys j,k to locate the command to run and press ENTER - CTRL C for command line of [Command Line] window - Another CTRL C to quit from [Command Line] window :his - lists the command history, :his / - lists the history of searches made ------------------------------------------------------------------------------------ GENERAL INFO / ISSUES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ q1. p(paste) command automatically yanks(copies) text being replaced and hence next p doesn't paste what was originally yanked(copied). - Use registers to paste multiple time. For effectiveness use multiple registers. Like for example "fy - copy firstname to register f (after selecting firstname in visual mode) "ly - copy lastname to register l (after selecting lastname in visual mode) "fp - paste firstname from register f(after selecting the text to be overwritten if needed) "lp - paste lastname from register l(after selecting the text to be overwritten if needed) :reg - view all registers There are nine types of registers: *registers* *E354* 1. The unnamed register "" 2. 10 numbered registers "0 to "9 3. The small delete register "- 4. 26 named registers "a to "z or "A to "Z 5. four read-only registers ":, "., "% and "# 6. the expression register "= 7. The selection and drop registers "*, "+ and "~ 8. The black hole register "_ 9. Last search pattern register "/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ q2. Commonly used short-cut keys in any editor http://vim.wikia.com/wiki/Using_standard_editor_shortcuts_in_Vim ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Open LAST VIM Session 1. While VIM has multiple files opened in many tabs save this session :mksession d:\vim_session 2. Open gVim74.exe -S D:\vim_session (update gVim exe shortcut link target with -S session_file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ENCRYPTING FILE WITH PASSWORD :X will ask to enter the password twice :w d:/encrypted_file.txt (will save the file encrypted) Close VIM application fully Open the VIM and open the file d:/encrypted_file.txt VIM will ask for the password to decrypt the file NOTE: closing and opening the file in the same VIM application session will NOT ask for the password to be entered again. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~