Nov 18, 2007

www.advancedlinuxprogramming

http://www.advancedlinuxprogramming.com/listings/

Basic Emacs Editor Commands

Colorado State University

Basic Emacs Editor Commands



What is Emacs?

Emacs is another editor available in UNIX. Like vi, emacs is a screen editor. Unlike vi, emacs is not an insertion mode editor, meaning that any character typed in emacs is automatically inserted into the file, unless it includes a command prefix.

Commands in emacs are either control characters (hold down the <Ctrl> key while typing another character) or are prefixed by one of a set of reserved characters: <Esc> or <Ctrl>-X. The <Esc> key can be typed by itself (because it really is a character) and then followed by another character; the <Ctrl> key must be held down while the next character is being typed. The conventions for describing these characters (since it takes too long to type out the whole thing) are ESC means <Esc> and C- means <Ctrl>.

One other distinction between emacs and vi is that emacs allows you to edit several files at once. The window for emacs can be divided into several windows, each of which contains a view into a buffer. Each buffer typically corresponds to a different file. Many of the commands listed below are for reading files into new buffers and moving between buffers.

To use emacs on a file, type
        emacs filename
If the file named filename exists, then the first screen's worth of the file is displayed; if it doesn't exist, a help message is displayed.

The easiest way to learn emacs is to start it up and go through the on-line tutorial. To access the on-line tutorial, type
        ESC help-with-tutorial
immediately after you have started emacs. The tutorial directs you further in learning the basic commands. One notational point you should know is that the tutorial uses M- to mean ESC.

To give you a head start, the following table lists the basic commands you need to know to use emacs to edit a file. An asterisk (* or star) to the left of a command indicate it is one to learn immediately.


Help Commands

* C-h help-command: first character in lots of useful help commands
* C-h t help-with-tutorial: command to run the tutorial
C-h i information: describes most of the emacs commands in man style pages
C-h k describe-key: tells you what a particular key stroke does
* C-h a command-apropos: prompts for a string and
then searches for all emacs commands that contains that string
ESC ? also does command-apropos
* C-h ? help-for-help: describes how to use the help facilities

File Reading and Writing Commands

* C-x C-f find-file: first prompts for a filename and
then loads that file into a editor buffer of the same name
* C-x C-s save-buffer: saves the buffer into the associated filename
C-x C-w write-named-file: prompts for a new filename and writes the buffer into it

Cursor/Screen Movement Commands

Depending on the terminal, some of the cursor movement can be handled by the arrow keys.

* C-a move cursor to (at) beginning-of-line
C-e move cursor to end-of-line
* C-f move cursor forward one character
* C-b move cursor backward one character
* C-n move cursor to next line
* C-p move cursor to previous line
C-v scroll file forward by one screenful
ESC v scroll file backward by one screenful
* ESC < go to beginning-of-buffer
* ESC > go to end-of-buffer
ESC f move cursor forward one word
ESC b move cursor backward one word

Copy and Delete Commands

C-d delete-char: delete character under cursor
ESC d delete-word: delete from cursor to end of word immediately ahead of the cursor
* C-k kill-line: delete the rest of the current line
* C-@ set-mark-command: mark is used to indicate the beginning of an area of text to be yanked
* C-w kill-region: delete the area of text between the mark and the current cursor position
* C-y yank: insert at current cursor location whatever was most recently deleted
ESC w copy-region-as-kill: copy area between mark and cursor into kill-buffer
so that it can be yanked into someplace else

Search Commands

* C-s isearch-forward: prompts for text string and
then searches from the current cursor position forwards in the buffer
C-r isearch-backward: like isearch-forward,
but searches from the current cursor position to end of buffer for text string
ESC % query-replace: prompts for a search string and
a string with which to replace the search string

Window and Buffer Commands

C-x 0 zero-window: deletes current window
C-x 2 double-window: splits current window into two parts,
allowing you to edit at two different locations in the same file
or permitting you to view two different files at the same time
C-x b switch-to-buffer: display a different buffer on the screen
C-x o other-window: move the cursor to the other window
(assuming that you have two windows/buffers open at once
* C-x C-b list-buffers: lists those buffers currently loaded into emacs

Exiting Emacs, Fixing Mistakes and Other Important Stuff

* C-x C-c save-buffers-kill-emacs: when you are finished editing,
to save the edited but unsaved buffers
and to return you to the UNIX prompt
* C-g keyboard-quit: if while typing a command you make a mistake and want to stop,
this aborts a command in progress
C-u universal-argument: if you want to do a command several times,
type this command
followed by a number (for the number of times)
followed by the command you wish repeated
* C-x u undo: undoes the last command typed, in case you made a mistake
* ESC x execute-extended-command: prompts for the name of an emacs command;
allows you to execute a command
if you know roughly what it is called
but cannot remember the key strokes for it


Other Emacs Bindings

Emacs has many other useful commands. As you get more proficient at it, try listing the key bindings (ESC-x for describe-bindings) to find other commands. Also note that emacs has what is called escape-completion: as you type in commands or filenames, you can type a space at any time and emacs will either fill out the rest (if there is only one possible completion) or it will list the possible completions in a new window.

A Web page with a complete list of emacs bindings is also available.



Comments:
Copyright &#169 1998: Colorado State University, CS Department. All rights reserved.

Emacs Command Summary

Emacs command summary, Thu Jul 25.

C-SP set-mark-command C-q quoted-insert
C-a beginning-of-line C-r isearch-backward
C-b backward-char C-s isearch-forward
C-c exit-recursive-edit C-t transpose-chars
C-d delete-char C-u universal-argument
C-e end-of-line C-v scroll-up
C-f forward-char C-w kill-region
C-h help-command C-x Control-X-prefix
TAB indent-for-tab-command C-y yank
LFD newline-and-indent C-z suspend-emacs
C-k kill-line ESC ESC-prefix
C-l recenter C-] abort-recursive-edit
RET newline C-_ undo
C-n next-line SPC .. ~ self-insert-command
C-o open-line DEL delete-backward-char
C-p previous-line

C-h v describe-variable C-h d describe-function
C-h w where-is C-h k describe-key
C-h t help-with-tutorial C-h c describe-key-briefly
C-h s describe-syntax C-h b describe-bindings
C-h n view-emacs-news C-h a command-apropos
C-h C-n view-emacs-news C-h C-d describe-distribution
C-h m describe-mode C-h C-c describe-copying
C-h l view-lossage C-h ? help-for-help
C-h i info C-h C-h help-for-help
C-h f describe-function

C-x C-a add-mode-abbrev C-x 5 split-window-horizontally
C-x C-b list-buffers C-x ; set-comment-column
C-x C-c save-buffers-kill-emacs C-x < scroll-left
C-x C-d list-directory C-x = what-cursor-position
C-x C-e eval-last-sexp C-x > scroll-right
C-x C-f find-file C-x [ backward-page
C-x C-h inverse-add-mode-abbrev C-x ] forward-page
C-x TAB indent-rigidly C-x ^ enlarge-window
C-x C-l downcase-region C-x ` next-error
C-x C-n set-goal-column C-x a append-to-buffer
C-x C-o delete-blank-lines C-x b switch-to-buffer
C-x C-p mark-page C-x d dired
C-x C-q toggle-read-only C-x e call-last-kbd-macro
C-x C-r find-file-read-only C-x f set-fill-column
C-x C-s save-buffer C-x g insert-register
C-x C-t transpose-lines C-x h mark-whole-buffer
C-x C-u upcase-region C-x i insert-file
C-x C-v find-alternate-file C-x j register-to-dot
C-x C-w write-file C-x k kill-buffer
C-x C-x exchange-dot-and-mark C-x l count-lines-page
C-x C-z suspend-emacs C-x m mail
C-x ESC repeat-complex-command C-x n narrow-to-region
C-x $ set-selective-display C-x o other-window
C-x ( start-kbd-macro C-x p narrow-to-page
C-x ) end-kbd-macro C-x q kbd-macro-query
C-x + add-global-abbrev C-x r copy-rectangle-to-register
C-x - inverse-add-global-abbrev C-x s save-some-buffers
C-x . set-fill-prefix C-x u advertised-undo
C-x / dot-to-register C-x w widen
C-x 0 delete-window C-x x copy-to-register
C-x 1 delete-other-windows C-x { shrink-window-horizontally
C-x 2 split-window-vertically C-x } enlarge-window-horizontally
C-x 4 ctl-x-4-prefix C-x DEL backward-kill-sentence

ESC C-SP mark-sexp ESC = count-lines-region
ESC C-a beginning-of-defun ESC > end-of-buffer
ESC C-b backward-sexp ESC @ mark-word
ESC C-c exit-recursive-edit ESC O ??
ESC C-d down-list ESC [ backward-paragraph
ESC C-e end-of-defun ESC delete-horizontal-space
ESC C-f forward-sexp ESC ] forward-paragraph
ESC C-h mark-defun ESC ^ delete-indentation
ESC LFD indent-new-comment-line ESC a backward-sentence
ESC C-k kill-sexp ESC b backward-word
ESC C-n forward-list ESC c capitalize-word
ESC C-o split-line ESC d kill-word
ESC C-p backward-list ESC e forward-sentence
ESC C-s isearch-forward-regexp ESC f forward-word
ESC C-t transpose-sexps ESC g fill-region
ESC C-u backward-up-list ESC h mark-paragraph
ESC C-v scroll-other-window ESC i tab-to-tab-stop
ESC C-w append-next-kill ESC j indent-new-comment-line
ESC ESC ?? ESC k kill-sentence
ESC C- indent-region ESC l downcase-word
ESC SPC just-one-space ESC m back-to-indentation
ESC ! shell-command ESC q fill-paragraph
ESC $ spell-word ESC r move-to-window-line
ESC % query-replace ESC t transpose-words
ESC ' abbrev-prefix-mark ESC u upcase-word
ESC ( insert-parentheses ESC v scroll-down
ESC ) move-past-close-and-reindent ESC w copy-region-as-kill
ESC , tags-loop-continue ESC x execute-extended-command
ESC - negative-argument ESC y yank-pop
ESC . find-tag ESC z zap-to-char
ESC 0 .. ESC 9 digit-argument ESC | shell-command-on-region
ESC ; indent-for-comment ESC ~ not-modified
ESC < beginning-of-buffer ESC DEL backward-kill-word

C-x 4 C-f find-file-other-window C-x 4 d dired-other-window
C-x 4 . find-tag-other-window C-x 4 f find-file-other-window
C-x 4 b pop-to-buffer C-x 4 m mail-other-window

Nov 17, 2007

linux command

常用命令动词 简要说明
cd directory 进入指定的目录
cd .. 进入上一级目录
cd /directory 进入目录
cd 进入用户自己的目录
cp file_from file_to 拷贝文件
ln [-s] source linkname 为一个文件建立连结
ls [directory] 查看指定目录下的文件
ls -l [directory] 查看指定目录下文件的详细
ls -a [directory] 查看指定目录下的所有文件
mkdir new_directory 建一个新目录
more file 查看一个文本文件的内容
rm file 删除一个文件
rm -r directory 删除一个目录
rmdir directory 删除一个目录
find . -name "file" 从当前目录开始查找指定的文件
adduser 创建新用户
alias 设置别名或替代名
bg fg 使挂起的进程继续运行
ps ax 查询当前进程
mount 连接文件系统
more less 浏览文件内容
chown chgrp 改变文件的拥有者
chmod 改变文件属性
halt 关闭系统
man 显示手册页
passwd 改变用户口令
grep 查找字符串
find 查找文件
dd 复制磁盘或文件系统
kill 杀掉一个进程
killall 杀掉进程

Linux BASH: vi Editor Commands


Linux BASH syntax : vi Editor Commands

Switch to Text or Insert mode:

Open line above cursor
O
Insert text at beginning of line
I
Insert text at cursor
i
Insert text after cursor
a
Append text at line end
A
Open line below cursor
o

Switch to Command mode:

Switch to command mode
<ESC>

Cursor Movement (command mode):

Scroll Backward 1 screen
<ctrl>b
Scroll Up 1/2 screen
<ctrl>u
Go to beginning of line
0
Go to line n
nG
Go to end of line
$
Scroll Down 1/2 screen
<ctrl>d
Go to line number ##
:##
Scroll Forward 1 screen
<ctrl>f
Go to last line
G
Scroll by sentence f/b ( )
Scroll by word f/b w b Move left, down, up, right h j k l
Left 6 chars
6h
Directional Movement Arrow Keys
Go to line #6
6G

Deleting text (command mode):

Change word
cw
Replace one character
r
Delete word
dw
Delete text at cursor
x
Delete entire line (to buffer)
dd
Delete current to end of line
D
Delete 5 lines (to buffer)
5dd
Delete lines 5-10
:5,10d

Editing (command mode):

Copy line
yy
Copy n lines
nyy
Copy lines 1-2/paste after 3
:1,2t 3
Paste above current line
P

Paste below current line
p
Move lines 4-5/paste after 6
:4,5m 6
Join previous line
J
Search backward for string
?string
Search forward for string
/string Find next string occurrence n
% (entire file) s (search and replace) /old text with new/ c (confirm) g (global - all)
:%s/oldstring/newstring/cg
Ignore case during search
:set ic
Repeat last command
.
Undo previous command
u
Undo all changes to line
U

Save and Quit (command mode):

Save changes to buffer
:w
Save changes and quit vi
:wq
Save file to new file
:w file
Quit without saving
:q!
Save lines to new file
:10,15w file

Related commands:

Book - Learning the vi Editor (6th Edition)
Full list of VI commands
vi Tutorial
nano-editor.org - simple text editor
Pico - short for Pine Composer
Gnotepad.sourceforge.net



Back to the Top

Simon Sheppard
SS64.com

vi command summary

The following tables contain all the basic vi commands.

Starting vi

Command Description
vi file start at line 1 of file
vi +n file start at line n of file
vi + file start at last line of file
vi +/pattern file start at pattern in file
vi -r file recover file after a system crash

Saving files and quitting vi

Command Description
:e file edit file (save current file with :w first)
:w save (write out) the file being edited
:w file save as file
:w! file save as an existing file
:q quit vi
:wq save the file and quit vi
:x save the file if it has changed and quit vi
:q! quit vi without saving changes

Moving the cursor

Keys pressed Effect
h left one character
l or <Space> right one character
k up one line
j or <Enter> down one line
b left one word
w right one word
( start of sentence
) end of sentence
{ start of paragraph
} end of paragraph
1G top of file
nG line n
G end of file
<Ctrl>W first character of insertion
<Ctrl>U up ½ screen
<Ctrl>D down ½ screen
<Ctrl>B up one screen
<Ctrl>F down one screen

Inserting text

Keys pressed Text inserted
a after the cursor
A after last character on the line
i before the cursor
I before first character on the line
o open line below current line
O open line above current line

Changing and replacing text

Keys pressed Text changed or replaced
cw word
3cw three words
cc current line
5cc five lines
r current character only
R current character and those to its right
s current character
S current line
~ switch between lowercase and uppercase

Deleting text

Keys pressed Text deleted
x character under cursor
12x 12 characters
X character to left of cursor
dw word
3dw three words
d0 to beginning of line
d$ to end of line
dd current line
5dd five lines
d{ to beginning of paragraph
d} to end of paragraph
:1,. d to beginning of file
:.,$ d to end of file
:1,$ d whole file

Using markers and buffers

Command Description
mf set marker named ``f''
`f go to marker ``f''
´f go to start of line containing marker ``f''
"s12yy copy 12 lines into buffer ``s''
"ty} copy text from cursor to end of paragraph into buffer ``t''
"ly1G copy text from cursor to top of file into buffer ``l''
"kd`f cut text from cursor up to marker ``f'' into buffer ``k''
"kp paste buffer ``k'' into text

Searching for text

Search Finds
/and next occurrence of ``and'', for example, ``and'', ``stand'', ``grand''
?and previous occurrence of ``and''
/^The next line that starts with ``The'', for example, ``The'', ``Then'', ``There''
/^The> next line that starts with the word ``The''
/end$ next line that ends with ``end''
/[bB]ox next occurrence of ``box'' or ``Box''
n repeat the most recent search, in the same direction
N repeat the most recent search, in the opposite direction

Searching for and replacing text

Command Description
:s/pear/peach/g replace all occurrences of ``pear'' with ``peach'' on current line
:/orange/s//lemon/g change all occurrences of ``orange'' into ``lemon'' on next line containing ``orange''
:.,$/<file/directory/g replace all words starting with ``file'' by ``directory'' on every line from current line onward, for example, ``filename'' becomes ``directoryname''
:g/one/s//1/g replace every occurrence of ``one'' with 1, for example, ``oneself'' becomes ``1self'', ``someone'' becomes ``some1''

Matching patterns of text

Expression Matches
. any single character
* zero or more of the previous expression
.* zero or more arbitrary characters
< beginning of a word
> end of a word
quote a special character
* the character ``*''
^ beginning of a line
$ end of a line
[set] one character from a set of characters
[XYZ] one of the characters ``X'', ``Y'', or ``Z''
[[:upper:]][[:lower:]]* one uppercase character followed by any number of lowercase characters
[^set] one character not from a set of characters
[^XYZ[:digit:]] any character except ``X'', ``Y'', ``Z'', or a numeric digit

Options to the :set command

Option Effect
all list settings of all options
ignorecase ignore case in searches
list display <Tab> and end-of-line characters
mesg display messages sent to your terminal
nowrapscan prevent searches from wrapping round the end or beginning of a file
number display line numbers
report=5 warn if five or more lines are changed by command
term=ansi set terminal type to ``ansi''
terse shorten error messages
warn display ``[No write since last change]'' on shell escape if file has not been saved


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005