Wednesday, November 30, 2011

VIM vs EMACS

Comparison between VIM and EMACS commands.
In EMACS - C means "Control" key and M means "Alt" key.
In VIM - most of the commands are done in escape mode unless specifically mentioned.

VIM EMACS
Navigation
j - move one line down
k - move one line up
l - move cursor right
h - move cursor left
w - move cursor one word forward
b - move cursor one word backward

H moves cursor to HOME of the window
M moves cursor to MIDDLE of the window
L moves cursor to BOTTOM of the window

gg - go to beginning of the buffer
G - go to end of the buffer

0 or ^ - go to start of line
$ - go to end of line

Ctrl-f to forward one page
Ctrl-b to backward one page

ESC - to kill current command
n <cmd> to repeat command n times

:set number to show line numbers
To load by default (:set number) in .vimrc

Wrap lines
:set nowrap - to remove wrapping of lines
:set wrap - to enable wrapping of lines

:shell or sh go to shell
exit to come back to vi

:n to jump to line n

% find matching parenthesis


(insert mode)Ctrl+p or Ctrl+n for word completion

:sort to sort

:set textwidth=190
(come to next line once 190 column is reached


Navigation
C-n move one line down
C-p move one line up
C-f move cursor right
C-n move cursor left
M-f move cursor one word forward
M-b move cursor one word backward

M-0 M-r moves cursor to HOME of the window
M-m M-r moves cursor to MIDDLE of the window
M-- M-r moves cursor to BOTTOM of the window

M-< or ESC-< go to beginning of the buffer
M-> or ESC-> go to end of the buffer

C-a go to start of line
C-e go to end of line

C-v to forward one page
M-v to backward one page

C-g to kill current command
M-n <cmd> to repeat command n times

M-x global-linum-mode
To load by default (global-linum-mode 1) in .emacs

Wrap lines
M-x toggle-truncate-lines
(setq-default truncate-lines t)

M-x shell go to shell
Use C-x right or left to go next or previous buffer

M-g g n to go to line n

C-M-f find matching parenthesis forward
C-M-b find matching parenthesis backward

M-/ for word completion

M-x sort-lines to sort

M-x line-number-mode
M-x column-number-mode
C-x f to set column width
M-q to wrap the current paragraph with column width 
Undo
u - undo
Ctrl-r - redo
Undo
C-_  or C-x u or C-/ to undo

Search 
:set incsearch to set increment search
:set ignorecase to set ignore case while search

/word to search forward
n- go to next match
N- go to previous match

?word to search backward
n- go to next match
N- go to previous match

By default regexp search


* or gd - to search word under cursor forward
# - to search word under cursor backward
Search
By default increment search
By default ignore case while search

C-s to search forward
C-s go to next match
C-r go to previous match

C-r to search backward
C-r go to next match
C-s go to previous match

Use C-M-s or C-M-r for regexp search.
or Esc-C-r or Esc-C-s

C-s C-w to search word under cursor forward
C-r C-w to search word under cursor backward
Column editing 
Ctrl+q or Ctrl+v to start column editing
I <any-character> ESC - to start insert at start of the block
A <any-character> ESC - to start insert at end of the block
h/j/k/l to select the block.
d - cut the selected region
y - yank the selected region
gv - to select the previous visual selection.












Column editing
M-x cua-mode to enable column editing (toggles)
C-RET to start the selection (toggles)
once mark is set traverse using C-n/p/f/b/a/e to select region
After selection
C-x to cut the selected region
C-c or M-w to copy the region
C-y or C-v to paste the region
Start typing having mouse at the start of the
   region to insert at the start of region
Same goes to end of the region
Ret to move around the corners of the region.

Rectangles -
C-Space to set mark
move to any other region,
C-x r k to kill the region
C-x r y to paste that region
C-x r c to clear the region with spaces
C-x r t String to replace rectangle block with String
C-x r r a to copy the rectangle area to register a
C-x r i a to paste the rectangle area to register a
Replace
:%s/xyz/abc/g  - to replace xyz with abc
:%s/xyz/abc/gc - to replace xyz with abc with confirmation.





Replace
M-x replace-string RET xyz RET abc RET to replace xyz with abc
M-x query-replace RET xyz RET abc RET to replace xyz with abc
by asking user
<SPACE> to replace
<DEL> to skip
! to replace others

M-% to replace same as M-x query-replace
Indenting XML
:set ft=xml
:%s/>\s*</>\r</g
ggVG=
J - to join line with one space
gJ - to join lines with no space
Indenting XML
M-x nxml-mode
M-x replace-regexp RET  > *< RET >C-q C-j< RET
C-M-\ to indent
M-x replace-regexp RET C-q C-j RET Space RET join lines 1 space
M-x replace-regexp RET C-q C-j RET RET to join lines no space
Cut, Copy and Paste
0D or ^D - cut from cursor to end of line (no newline)
dd or Vd - cut current cursor to end of line (including current line)
D - cut from current cursor to end of line (no newline)
x  - to delete character under cursor
dw - delete next word
yy or Vy - to copy current line (including current line)
ddp - swap current line and next line







Cut, Copy and Paste
C-k cut from current cursor to end of the line (no newline)
C-k C-k to cut the whole line (including new line)
C-Shift-n C-w to cut the current line (including new line)
M-k cut from current cursor to end of sentence
C-d to delete next character
<Delete> to delete previous character
M-d to delete next word
M-<Delete> to delete previous words
C-y paste
C-@ or C-Space mark set
C-w cut from mark set to cursor
M-w Copy from mark set to cursor
C-a C-Space C-n M-w to copy whole line  (including new line)
C-Shift-n M-w to copy the whole line (including new line)
C-a C-Space C-n C-w C-n C-y swap current line and next line
Dealing with case
gUU - to make whole current line to upper case
guu - to make whole current line to lower case
ggVGU - to make whole file to upper case
ggVGu - to make whole file to lower case
~ - to invert case.

Dealing with case
C-a C-Space C-n C-x C-u - to make whole current line to uppercase
C-a C-Space C-n C-x C-l - to make whole current line to lowercase
M-< C-Space M-> C-x C-u - to make whole file to upper case
M-< C-Space M-> C-x C-l - to make whole file to lower case
M-l to make next word lower case
M-u to make next word upper case
Dealing with files & Buffers
:e filename/vi filename to open file in vi
:view filename to only view the file. restricted to write
:set ro readonly mode
:set ro! readonly mode
:enew to edit new file
:sp to edit same file in H split
:vsp to edit same file in V split
:new to edit new file in H split
:vnew to edit new file in V split
:tabe to edit new file in new tab
:e! return to unmodified file
:q to quit
:q! to quit without save
:wq to save and quit
:w to write and be there in program
:x to write and exit
:x! not to write and exit
ZZ to save and exit in command mode (capital Z)
:w filename to write the content on file
:w! filename2 to overwrite the existing filename to filename2

:ls to list all the buffers
:buffers to list all buffers
:buffer n to jump to buffer n
:bp to move to previous buffer
:bn to move to next buffer
:bd to remove from buffer
:args **/* to keep all files in the current directory in vim (SUPER)
:rew to first file in args
:te Buffers to show the menu buffer
:badd add file to buffer
:b# to alternate buffer
Ctrl-^ is to switch alternate buffers
:tab ball to open all buffers in tabs
:ball to open all buffers Horizontally
:vert ball to open all buffers vertically

ctrl+w o keep open only the current window
ctrl+w w to switch between windows
ctrl-w-v to open a new window vertically
ctrl-w-n to open a new window horizontally

ctrl-w + increase the size of window
ctrl-w - decrease the size of window
ctrl-w = to equalize the size of each split window
ctrl-w | to maximize in vertical split
ctrl-w _ to maximize in Horizontal split
ctrl-w h to move to the window left
ctrl-w j to move to the window below
ctrl-w k to move to the window above
ctrl-w l to move to the window right
ctrl-w H to move current window to far right
ctrl-w J to move current window to far Down
ctrl-w K to move current window to far Up
ctrl-w L to move current window to far left
ctrl-w T to move current window to new tab
ctrl-w n to open new file in H split
ctrl-w q to close
ctrl-w o to close all splits except current window (:only also do the same)
Dealing with files & Buffers
C-x C-f to find the file to load into buffer
C-x C-q to only view the file. restricted to write (toggles)
C-x b *scratch* to edit new file
C-x 2 to split horizontally same file
C-x 3 to split vertically same file
C-x 4 C-f *scratch* to open file in H split
C-x 4 b buffer to open buffer in V split
C-x 3 C-x b buffer to open new buffer in V split
C-x C-s to save buffer to file
C-x s to save some buffers
C-c C-x to exit emacs



C-x C-b to list all buffers
C-x <Right> go to next buffer
C-x <Left> go to previous buffer
C-x k to kill current buffer
C-x b <buffer name> to switch to that buffer
C-x h select everything in buffer
M-x buffer-menu to list all buffers in separate window
~ clear modified flag on that buffer
k mark buffer for delete
x to delete all marked buffers
o to open the buffer in below split window
Note - * says it is modified, % means it is read only file
C-x 1 keep open only the current window
M-x make-frame same as above
C-x o to switch between windows
C-M-v to scroll the other window

C-x } to increase the size of window horizontally
C-x { to decrease the size of window vertically





























Registers
Marking registers -
ma - to mark position on a
'a - to jump to position a

Copying & paste to registers -
"ayy - yank current line to register a
"Ayy - append current line to register a
V"ay - in visual mode, to register a
V"Ay - append current selection to register a
"ap to paste content of register a
:reg to view all registers
:reg a to view contents in register a
Registers
Marking registers -
C-x r Space a - to mark position on a
C-x r j a - to jump to position on a  

Copying & paste to registers -
C-x r s a yank selected region to register a
C-x r i a insert selected region
M-x view-register <CR> a to view contents of register a
M-x append-to-register to append selected region to register a
M-x prepend-to-register to prepend selected region to register a
C-x r r a to copy the rectangle area to register a
C-x r i a to paste the rectangle area to register a
To remove duplicate lines
:sort u to remove duplicate lines




















To remove duplicate lines
Keep below functions in .emacs file
;; To remove duplicate lines
(defun uniquify-all-lines-region (start end)
      "Find duplicate lines in region START to END keeping first occurrence."
          (interactive "*r")
              (save-excursion
                   (let ((end (copy-marker end)))
                        (while
                           (progn
                             (goto-char start)
                               (re-search-forward "^\\(.*\\)\n\\(\\(.*\n\\)*\\)\\1\n" end t))
                               (replace-match "\\1\n\\2")))))

  (defun remove-duplicate-lines ()
        "Delete duplicate lines in buffer and keep first occurrence."
            (interactive "*")
                (uniquify-all-lines-region (point-min) (point-max)))


M-x remove-duplicate-lines
To delete matching lines
:g/<regexp>/d to delete matching lines
To delete empty lines
:g/^\s*$/d



To delete matching lines
M-< M-x delete-matching-lines RET <regexp> RET
To delete empty lines
M-< M-x delete-matching-lines RET ^ *$ RET
Note: there is space between ^ and *
or
M-< M-x flush-lines RET <regexp> RET
To delete non-matching lines
:v/<regexp>/d to delete non-matching lines


To delete non-matching lines
M-< M-x delete-non-matching-lines RET <regexp> RET
or
M-< M-x keep-lines RET <regexp> RET
To copy matching lines
:g/<regexp>/y A to copy matching lines to register a
"ap to paste matching lines




















To copy matching lines
M-< M-x keep-lines RET <regexp> RET
C-x h C-x r s a copy matching lines to register a
C-x r i a to paste matching lines

function to copy matching lines to buffer
reference - http://stackoverflow.com/questions/2289883/emacs-copy-matching-lines
;; To copy matching lines to another buffer *matched*
(defun copy-lines-matching (re)
    "find all lines matching the regexp RE in the current buffer
  putting the matching lines in a buffer named *matching*"
    (interactive "sRegexp to match: ")
      (let ((result-buffer (get-buffer-create "*matching*")))
           (with-current-buffer result-buffer (erase-buffer))
           (save-match-data
             (save-excursion
               (goto-char (point-min))
               (while (re-search-forward re nil t)
                   (princ (buffer-substring-no-properties (line-beginning-position) (line-beginning-position 2))
                          result-buffer))))
                    (pop-to-buffer result-buffer)))

No comments:

Post a Comment