*NIX Tricks

[emacs] Absolute basics — one step at a time: files, buffers, windows

Posted in emacs by kousik on May 19, 2010

Manipulate files:

1. C-x C-f Find a file (allows TAB completion)
2.  C-x C-s Save a copy of the current buffer to
     the corresponding file. If the buffer is not associated
     with a file, it'll prompt for a file name.
3. C-x s Save some buffers with the option of selectively
    saving them.

Manipulate buffers:

1. C-x C-b List all buffers
   (then C-x 1 to kill the window showing the list)
2. C-x b Prompts for the buffer to go to (allows tab completion)
3. C-x k Prompts for the buffer name to kill
4. C-x C-q Toggle read-only mode

Manipulate windows:

1. C-x 0 Delete current window
2. C-x 1 Delete all but the current window
3. C-x 2 Split the window vertically
    (with a horizontal line in the middle)
4. C-x 3 Split the window horizontally
   (with a vertical line in the middle)
5. C-x o Move to the other window
6. C-M-v Scroll the other window

Reference: A tutorial introduction to emacs.

Tagged with: , , ,

[emacs] Absolute basics — one step at a time: delete, kill, yank and undo

Posted in emacs by kousik on May 19, 2010

Delete (non-retrievable)/ kill (retrievable)

Character:

   1. C-d forward delete a character
   2. Delback backward delete a character

Word:

   3. M-d forward kill a word
   4. M-delback kill the word immediately before the cursor

Line:

   
   5. C-k  kills upto the end of the line
         not including the newline charcacter
   6. C-u 0 C-k  kills upto the beginning of the line
        not including newline character
        (You may replace "C-u 0" by "M-0", too)

Sentence:

 
   7. M-k  kills upto the end of the sentence
   8. C-u -1 M-k kills upto the beginning of the current sentence

Region:

   
   9. C-spc Set a mark at the current cursor position
  10. C-w kill the region between the current cursor position and
      the previous mark set by C-spc.

Undo

   1. C-x u
   2. C-_
   3. C-/

Yank

   2. C-y yank previous kill
   3. M-y replace text inserted by previous C-y by the next entry in the "kill ring".

N.B.

      1. M-5 C-/ undo last 5 changes
      2. M-2 C-y Yank last kill 5 times
      3. C-x C-s Save all the changes in the current file.
Tagged with: , , , , ,

[emacs] Absolute basics — one step at a time: cursor movement

Posted in emacs by kousik on May 19, 2010

This is a continuation of my effort to get used to emacs editor. I just realized that there is no point in listing all the commands together at one post — simply because I missed a lot of useful ones the first time. So, let’s do it step-wise — one category at a time. Today, just the ‘cursor movement’: (Hit C-h t to get to the inbuilt tutorial)

Lines (vertically):

   
    1. C-n next line
    2. C-p previous line

Lines (Horizontally):

    3. C-a Move to the beginning of a line
    4. C-e Move to the end of a line

    5. C-f move forward one character
    6. C-b move backward one character

    7. M-f move forward one word
    8. M-b Move backward one word

Sentences:

    5. M-a Move to the beginning of a sentence
    6. M-e Move to the end of a sentence

Paragraphs:

    7. M-{ Beginning of a paragraph
    8. M-} End of a paragraph

Pages:

 
     9. C-x [ Beginning of the page
   10. C-x ] End of the page

Buffers:

   11. M-< Move to the beginning of the current buffer
   12. M-> Move to the end of the current buffer

N.B.C-u <n>" or "M-<n>" is the command for numeric argument, i.e.

   1. C-u 7 *  Inserts "*" seven times
   2. M-8 C-f forwards the cursor 8 letters
Tagged with: , ,

[emacs] Basics -2: Search and Replace

Posted in emacs by kousik on September 16, 2009

This is the second one in the series of posts in an effort to get to know Emacs better (it’s very hard for a vim person, you know!). In this post we will look into the search as well as search-and-replace options in Emacs.

1) Forward/backward search: (C-s and C-r)
press C-s and the minibuffer will ask you to enter the search phrase. Similarly, use C-r to search in the reverse direction. Press C-s again to move forward from one match to another, and C-r to move backward. Hit C-g to cancel search. The forward and backward search options wrap around the text.

2) Unconditional search and replace: (M-x replace-string)
Press M-x, and then enter replace-string. You may use tab-completion to save yourself from some typing. After entering the string hit enter and the minibuffer will present you with “Replace string: ”. Enter the string to be replaced (say, XXX) and hit enter again. The minibuffer will present you with “ Replace string XXX with: ”. Enter the the relevant string (say, YYY ) and hit enter and voila — all the occurrences of XXX will be replaced with YYY.
N.B. Substitute replace-string with replace-regex to use the power of regular expressions.

3) Queried search and replace: (M-%)
Press m-%. You will then be prompted for your search and replace strings. Emacs will find and display each occurrence of the search string and ask you for further instructions. You can respond with any of the following options:

  • Spacebar Replace text and find the next occurrence
  • Del Leave text as is and find the next occurrence
  • . (period) Replace text, then stop looking for occurrences
  • ! (exclamation point) Replace all occurrences without asking
  • ^(caret) Return the cursor to previously replaced text
  • e Edit the replacement string in the minibuffer. When you exit the minibuffer by typing RET, the minibuffer contents replace the current occurrence of the pattern. They also become the new replacement string for any further occurrences.

If you type any other character, Emacs will stop looking for occurrences and execute the character as a command.

To restart the queried search and replace, type:
C-x ESC ESC; this is generally used to repeat complex minibuffer commands. If you don’t see the correct command there, you can browse the the command history in the minibuffer using M-p (previous) and M-n (next), or search M-s (forward) and M-r (reverse). Once you reach the correct one, you can edit the command as well!

N.B. My experience shows that queried search-and-replace starts searching in the forward direction; so it’s better to put the cursor at the top with M-< first.

Reference: here.

[emacs] Absolute basics

Posted in emacs by kousik on September 8, 2009

This is my another effort to learn emacs :)

These are the absolute basics which must be kept in memory in order to even start to think about using emacs.

Get Help:

C-h C-h shows a list of help with keybindings as well as different commands, modes, and functions (you’ll need this!)

Cursor movement:

C-a beginning of line
C-e end of line
C-< top of document (in my case M-< worked)
C-> end of document (in my case M-> worked)
M-x goto-line Go to line
C-v page forward
M-v page backward

Deleting and moving text:

Backspace delete character before cursor
(subject to modification of .emacs)
C-d delete character under cursor
M-d delete word
C-k delete to end of line(s)
(restore with C-y)
C-SPC set mark
C-w delete from mark to cursor
(restore with C-y)

Search and replace:

C-s incremental search forward
C-r incremental search backward
C-% query replace
M-x replace-string <ENTER> <searchstr> <ENTER> <replstr>

Miscellaneous:

C-u n (or M-n) repeat the next keystroke or command n times (example: to print 7 *’s in a row use: M-7 * )

C-x u undo (I prefer, C-_ )
C-x C-s save file
C-x C-f find file
C-x 2 2 windows
C-x 1 1 window
C-x o switch between windows
C-x b switch buffers
M-q reformat paragraph
C-x C-c quit

Reference: link.

Tagged with: ,
Follow

Get every new post delivered to your Inbox.