[emacs] Basics -2: Search and Replace

Tags

, , , ,

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.

[network] Port forwarding via SSH

Tags

, , , , , , ,

Let’s say you have physical access to your home computer h.com and your work computer w.com and besides that you work on a remote server r.com. Let’s also assume that the usernames are huser, wuser and ruser, respectively.

There may be various accessibility scenarios, but let’s just assume that you can access the remote servers from both home and work computers, but you cannot access home computer from work directly and vice versa. A possible solution to have all the computers accessible is forward a port on the remote machine to the local port 22 (we are interested in ssh connection only, at least for the time being!).

Forwarding remote port:
So at home before you leave for work, forward port 2222 of r.com to port 22 of local machine h.com
$ ssh ruser@r.com -R 22:localhost:2222
Type in localhost as it is! It creates a secure socket from remote machine’s port 2222 to local machine’s port 22, meaning the ssh traffic coming to port 2222 of r.com will be forwarded to port 22 of h.com.
When you get to work, first ssh to the remote machine from your work computer:
$ ssh ruser@r.com.
You can then access the files at your home while you are still on h.com by simply doing
$ ssh huser@r.com -p 2222 (you’ll be asked for the password for your home computer). The flag -p stands for port.

Now, before you leave your work, forward port 22 of w.com to another port (say, 2223) of r.com using the above method. At home, you first ssh to r.com; and the using
$ ssh wuser@r.com -p 2223 (you’ll be asked for the password for the work computer),
you may access files on your work computer.

Forwarding local port:
I don’t know how I can best use it yet, but anyway, here is how to do it along with one “possible” use of it. Let’s say from your home computer you cannot ssh to r2.com, another remote server, but you can from r.com. In order to connect to r2.com form home, the most obvious way is to connect to r.com from h.com first, and then to r2.com. You have to do this for every new ssh connection r2.com from h.com. But if you forward an available local port (say, 2224) to port 22 of r2.com via r.com, it'll be a lot easier. Issue the following command while you are on h.com:
$ ssh ruser@r.com -L 2224:r2.com:22.
This will enable you to connect to r2.com from h.com by using
$ ssh r2user@localhost -p 2224
in another terminal on the home computer (r2user is the username for r2.com).

File copying between those computers also becomes a breeze:
$ scp -P 2224 file_to_be_copied r2user@localhost:/desired_dir/copied_file
(notice the capital P).

N.B. (1) In order to check if a port (say, 2225) is available on the locahost, try
$ nc localhost 2225,
(2) Needless to say, you must have sshd running on the machines that you want to connect to via ssh.

[emacs] Absolute basics

Tags

,

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.

[vim] Copy-paste between two vim sessions

Tags

, , , ,

These three methods may be used to copy some text in the vim buffer, which may then be pasted in another vim session. All the operations in vim are done in the normal mode.

Method 1:
1. "f3yy — yank 3 lines into buffer f
2. go to the other file opened using vim
2. "fp — paste the contents of buffer f

Method 2:
1. :100,200ya f — yank lines 100 through 200 to buffer f
2. go to the other file opened using vim
3. :pu f — paste the contents of buffer f

Method 3: (visual mode)
1. Select the code using any of v, V, or ^V
2.Copy into the X clipboard: "*y
3.Switch to the other vim
4. "*p

[cli] Line editing in bash

Tags

, , , , , , , ,

Although this blog serves as my personal notebook, however, since I’m putting this on the internet, I should humbly cite the reference here first. Most of the tricks are from the above reference, arranged according to my need. These are mostly Mac OSX terminal specific line-editing commands, but  should work in any other terminal running bash, as well.

[NB: If you don’t have a META key, then ESC may be used to replace it: e.g., to get the same functionality as META+t, first hit ESC then the letter t. In my mac, I did not have a META key by default. However, I’ve just found a solution (Google is your friend!) — go to terminal preferences, then select the keyboard tab and check “Use option as meta key”]

Cursor movement

CTRL+a
Move cursor to beginning of the line.
CTRL+e
Move cursor to the end.
CTRL+f
Move forward one character. Identical to .
CTRL+b
Move backward one character. Identical to .
META+f
Move forward one word.
META+b
Move backward one word.
CTRL+x CTRL+x
Mark current location in line and jump to beginning of line or second mark if defined. Repeat to jump to between both marks.

Cutting

CTRL+k
Delete everything from under the cursor to the end of the line. (I think of this as killing the rest of my line.)
CTRL+u
Delete everything from under the cursor the beginning of the line.
CTRL+w
Delete from under the cursor to the beginning of the word.

CTRL+d

Forward delete.
CTRL+h
Backspace.

Pasting/Inserting/Undoing

CTRL+y
Paste the most previously-deleted string. Basically a sort of command-line editting “undo.”
CTRL+_
Incremental undo.
META+r
Undo all the changes
CTRL+v
Insert next character verbatim. This is how you escape control sequences. For instance, to literally insert
^[, press ESC.
CTRL+j
Carriage return. Identical to hitting the return key.
CTRL+m
Newline. Identical to return.

Swapping

CTRL+t
Transpose (swap) the two characters before the cursor with one another.
META+t
Transpose (swap) the two words before the cursor with one another.

Changing case

META+c
Capitalize word under cursor and move to next word.
META+u
Uppercase word under cursor and move to next word.
META+l
Lowercase word under cursor and move to next word.

Auto-completion

META+.
Insert last word from previous command after cursor.
TAB
Auto-completes file, folder, and program names.
META+?
List the possible completions
CTRL+x /
List the possible filename completions
META+/
Attempt filename completion
CTRL+x ~
List the possible variable completions
META+~
Attempt username completion
CTRL+x $
List the possible variable completions
META+$
Attempt variable completion
CTRL+x @
List the possible hostname completion
META+@
Attempt hostname completion
CTRL+x !
List the possible command completions
META+!
Attempt command completion
META+TAB
Attempt completion from previous commands in the history list
CTRL+p
Recall previous command executed. Identical to .

Various

CTRL+z
Stop the current process and send it to the background.
CTRL+c
Send an SIG_HUP to the current process.
CTRL+d
Send an end-of-file special character to the current process. Doing this at the command line is identical to closing your terminal window.
CTRL+l
Repaint screen.

Keyboard macros (link)

CTRL+x (
Start a macro
CTRL+x )
End macro
CTRL+x e
Execute last macro.

Using vi inbuilt
However, the sweetest of all is if you set the vim inbuilt (either in ~/.bashrc or in the command line), you can actually use the most of the functionalities of vi in the command line
set -o vi
It’s in the insert mode by default; in order to go to the command mode, hit ESC!
You may also want to add
bind -m vi-command -r 'v'
to your ~/.bashrc so that you are not taken to a blank file every time you hit v in the command mode.

You may add the following in ~/.inputrc so that vi is always accessible
set editing-mode vi

History. If you’re using vi inbuilt you may also want to know how to browse through history: that’s easy too — just press j (to go up) and k (to go down) in history in command mode, just like regular vi.

File/directory name completion. Type the beginning characters of the file, hit escape followed by \ (backslash). However, the backslash will take you to insert the next character if it cannot uniquely identify the file.

Here's the link for the commandline vi completion tricks.

NB: TAB-completion works fine in the insert mode.

Design a site like this with WordPress.com
Get started