Keep walking

Whatever you need to grow, just do it faster…

Posts Tagged ‘vi’

UNIX: vi Editor

Posted by ZyK on 03/22/2012

General Introduction

The vi editor (short for visual editor) is a screen editor which is available on almost all Unix systems. Once you have learned vi, you will find that it is a fast and powerful editor. vi has no menus but instead uses combinations of keystrokes in order to accomplish commands. If you are just beginning to learn Unix, you might find the Pico editor easier to use (most command options are displayed at the bottom of the screen). If you use the Pine email application and have composed or replied to a message you have probably already used Pico as it is used for text entry. For more information please refer to the Pine/Pico page.

Starting vi

To start using vi, at the Unix prompt type vifollowed by a file name. If you wish to edit an existing file, type in its name; if you are creating a new file, type in the name you wish to give to the new file.

%vi filename

Then hit Return. You will see a screen similar to the one below which shows blank lines with tildes and the name and status of the file.~

~

“myfile” [New file]

vi’s Modes and Moods

vi has two modes: the command mode and the insert mode. It is essential that you know which mode you are in at any given point in time. When you are in command mode, letters of the keyboard will be interpreted as commands. When you are in insert mode the same letters of the keyboard will type or edit text. vi always starts out in command mode. When you wish to move between the two modes, keep these things in mind. You can type i to enter the insert mode. If you wish to leave insert mode and return to the command mode, hit the ESC key. If you’re not sure where you are, hit ESC a couple of times and that should put you back in command mode.

General Command Information

As mentioned previously, vi uses letters as commands. It is important to note that in general vi commands:

  • are case sensitive – lowercase and uppercase command letters do different things
  • are not displayed on the screen when you type them
  • generally do not require a Return after you type the command.

You will see some commands which start with a colon (:). These commands are ex commands which are used by the ex editor. ex is the true editor which lies underneath vi — in other words, vi is the interface for the ex editor.

Entering Text

To begin entering text in an empty file, you must first change from the command mode to the insert mode. To do this, type the letter i. When you start typing, anything you type will be entered into the file. Type a few short lines and hit Return at the end of each of line. Unlike word processors, vi does not use word wrap. It will break a line at the edge of the screen. If you make a mistake, you can use the Backspace key to remove your errors. If the Backspace key doesn’t work properly on your system, try using the Ctrl h key combination.

Cursor Movement

You must be in command mode if you wish to move the cursor to another position in your file. If you’ve just finished typing text, you’re still in insert mode and will need to press ESC to return to the command mode.

Moving One Character at a Time

Try using your direction keys to move up, down, left and right in your file. Sometimes, you may find that the direction keys don’t work. If that is the case, to move the cursor one character at the time, you may use the h, j, k, and lkeys. These keys move you in the following directions:

h	left one space		l	right one space j	down one space		k	up one space

If you move the cursor as far as you can in any direction, you may see a screen flash or hear a beep.

Moving among Words and Lines

While these four keys (or your direction keys) can move you just about anywhere you want to go in your file, there are some shortcut keys that you can use to move a little more quickly through a document. To move more quickly among words, you might use the following:

w	moves the cursor forward one word  b	moves the cursor backward one word (if in the middle of a 
 	word, b will move you to the beginning of the current word). e	moves to the end of a word.

To build on this further, you can precede these commands with a number for greater movement. For example, 5w would move you forward five words; 12b would move you backwards twelve words. [You can also use numbers with the commands mentioned earlier. For example, 5j would move you down 5 characters.]

Command Keys and Case

You will find when using vi that lower case and upper case command keys are interpreted differently. For example, when using the lower case w, b, and e commands, words will be defined by a space or a punctuation mark. On the other hand, W, B, and E commands may be used to move between words also, but these commands ignore punctuation.

Shortcuts

Two short cuts for moving quickly on a line include the $ and the 0 (zero) keys. The $ key will move you to the end of a line, while the 0 will move you quickly to the beginning of a line.

Screen Movement

To move the cursor to a line within your current screen use the following keys:

H	moves the cursor to the top line of the screen. M	moves the cursor to the middle line of the screen. L	moves the cursor to the last line of the screen.

To scroll through the file and see other screens use:

 ctrl-f	scrolls down one screen ctrl-b	scrolls up one screen ctrl-u	scrolls up a half a screen ctrl-d	scrolls down a half a screen

Two other useful commands for moving quickly from one end to the other of a document are G to move to the end of the file and 1G to move to the beginning of the file. If you precede G with a number, you can move to a specific line in the document (e.g. 15G would move you to line 15).

Moving by Searching

One method for moving quickly to a particular spot in your file is to search for specific text. When you are in command mode, type a / followed the text you wish to search for. When you press Return, the cursor will move to the first incidence of that string of text. You can repeat the search by typing n or search in a backwards direction by using N.

Basic Editing

To issue editing commands, you must be in command mode. As mentioned before, commands will be interpreted differently depending upon whether they are issued in lower or upper case. Also, many of the editing commands can be preceded by a number to indicate a repetition of the command.

Deleting (or Cutting) Characters, Words, and Lines

To delete a character, first place your cursor on that character. Then, you may use any of the following commands:

 x	deletes the character under the cursor. X	deletes the character to the left of your cursor. dw	deletes from the character selected to the end of the word. dd	deletes all the current line. D deletes from the current character to the end of the line.

Preceding the command with a number will delete multiple characters. For example, 10x will delete the character selected and the next 9 characters; 10X will delete the 10 characters to the left of the currently selected character. The command 5dw will delete 5 words, while 4dd deletes four lines.

Pasting Text using Put

Often, when you delete or cut text, you may wish to reinsert it in another location of the document. The Put command will paste in the last portion of text that was deleted since deleted text is stored in a buffer. To use this command, place the cursor where you wish the deleted text to appear. Then use p to reinsert the text. If you are inserting a line or paragraph use the lower case p to insert on the line below the cursor or upper case P to place in on the line above the cursor.

Copying Text with Yank

If you wish to make a duplicate copy of existing text, you may use the yank and put commands to accomplish this function. Yank copies the selected text into a buffer and holds it until another yank or deletion occurs. Yank is usually used in combination with a word or line object such as the ones shown below:

 yw	copies a word into a buffer (7yw copies 7 words) yy	copies a line into a buffer (3yy will copy 3 lines)

Once the desired text is yanked, place the cursor in the spot in which you wish to insert the text and then use the put command (p for line below or P for line above) to insert the contents of the buffer.

Replacing or Changing Characters, Words, and Lines

When you are using the following commands to replace text, you will be put temporarily into insert mode so that you can change a character, word, line, or paragraph of text.

 r	replaces the current character with the next character you enter/type.
   	Once you enter the character you are returned to command mode. R	puts you in overtype mode until you hit ESC which will then return
   	you to command mode. cw	changes and replaces the current word with text that you type.  A dollar
   	sign marks the end of the text you're changing.  Pressing ESC when you
   	finish will return you to command mode.

Inserting Text

If you wish to insert new text in a line, first position the cursor to the right of where you wish the inserted text to appear. Type i to get into insert mode and then type in the desired text (note that the text is inserted before the cursor). Press ESC to return to command mode.

Inserting a Blank Line

To insert a blank line below the line your cursor is currently located on, use the o key and then hit ESC to return to the command mode . Use O to insert a line above the line the cursor is located on.

Appending Text

You can use the append command to add text at any place in your file. Append (a) works very much like Insert (i) except that it insert text after the cursor rather than before it. Append is probably used most often for adding text to the end of a line. Simply place your cursor where you wish to append text and press a. Once you’ve finished appending, press ESC to go back to command mode.

Joining Lines

Since vi does not use automatic word wrap, it is not unusual in editing lines to end up with lines that are too short and that might be improved if joined together. To do this, place your cursor on the first line to be joined and type J. As with other commands, you can precede J with a number to join multiple lines (4J joins 4 lines).

Undoing

Be sure to remember this command. When you make a mistake you can undo it. DO NOTmove the cursor from the line where you made the change. Then try using one of the following two commands:

 u	undoes the last change you made anywhere in the file.  Using u again 
   	will "undo the undo". U	undoes all recent changes to the current line.  You can not have moved
	from the line to recover the original line.

Closing and Saving Files

When you edit a file in vi, you are actually editing a copy of the file rather than the original. The following sections describe methods you might use when closing a file, quitting vi, or both.

Quitting and Saving a File

The command ZZ(notice that it is in uppercase) will allow you to quit vi and save the edits made to a file. You will then return to a Unix prompt. Note that you can also use the following commands:

 :w	to save your file but not quit vi (this is good to do periodically in
	case of machine crash!). :q	to quit if you haven't made any edits. :wq	to quit and save edits (basically the same as ZZ).

Quitting without Saving Edits

Sometimes, when you create a mess (when you first start using vi this is easy to do!) you may wish to erase all edits made to the file and either start over or quit. To do this, you can choose from the following two commands:

 :e!	reads the original file back in so that you can start over. :q!	wipes out all edits and allows you to exit from vi.

More about Combining Commands, Objects, and Numbers

Now that you’ve learned some basic vi commands you might wish to expand your skills by trying some fancy combination steps. Some commands are generally used in combination with a text object. We’ve already seen some examples of this. For example, when you use the command dw to delete a word, that combines the delete (d) command with the word (w) text object. When you wish to delete multiple words, you might add a number to this combination. If you wished to delete 2 words you might use 2dw or d2w. Either of these combinations would work. So, as you can see, the general format for a command can be

(number) (command) (text object) or (command) (number) (text object)

You might wish to try out some of the following combinations of commands and objects:

Command Text Object
d (delete)	w (word to the left) y (yank/copy)	b (word to the right or backward) c (change)	e (end of word)
		H (top of the screen)
		L (bottom of the screen)
		M (middle of the screen)
		0 (zero - first character on a line)
		$ (end of a line)
		( (previous sentence)
		) (next sentence)
		[ (previous section)
		] (next section)

Repeating a Command

If you are doing repetitive editing, you may wish to use the same command over and over. vi will allow you to use the dot (.) to repeat the last basic command you issued. If for example, you wished to deleted several lines, you could use dd and then . (dot) in quick succession to delete a few lines.

A Quick Word about Customizing Your vi Environment

There are several options that you can set from within vi that can affect how you use vi. For example, one option allows you to set a right margin that will then force vi to automatically wrap your lines as you type. To do this, you would use a variation of the :set command. The :set command can be used to change various options in vi. In the example just described, you could, while still in vi, type :set wrapmargin=10 to specify that you wish to have a right margin of 10. Another useful option is :set number. This command causes vi to display line numbers in the file you are working on.

Other Options

To view a listing of other options, you could type :set all. To view only those options which are currently in effect, you can type set: by itself. Options that you set while in a vi session will apply during that session only. To make permanent changes to your vi environment, you could edit your .exrc file. However, you should not edit this file unless you know what you are doing!

Useful vi Commands

Cut/Paste Commands:

x 		delete one character (destructive backspace)
dw		delete the current word (Note: ndw deletes n numbered words)
dd 		delete the current line (Note: ndd deletes n numbered lines)
D		delete all content to the right of the cursor
d$		same as above
:u		undo last command
p,P		paste line starting one line below/above current cursor location
J		combine the contents of two lines 
"[a-z]nyy   	yank next n lines into named buffer [a-z] 
"[a-z]p/P	place the contents of selected buffer below/above the current line

Extensions to the Above Commands:

:3,18d		delete lines 3 through 18
16,25m30	move lines 16 through 25 to after line 30
23,29co62	copy specified lines and place after line 62

Cursor Relocation commands:

:[n]		goto line [n]
shift g		place cursor on last line of text
h/l/j/k		move cursor left, right, down and up 
^f/^b		move forward, backward in text, one page
^u/^d		move up, down one half page
$		move to end of line
0		move to beginning of line

Extensions to the Above:

b 		move backwards one word (Note: nb moves back n number of words)
e		move to end of current word
(		move to beginning of curent block
)		move to the end of current block

Searching and Substitution commands:

/ [string]	search forward for string
? [string]	search backwards for string
n		repeat last search
N		repeat search in opposite direction
cw		change the contents of the current word, (use ESC to stop
		replacement mode)
c$		Replace all content to the right of cursor (exit replacement
		mode with ESC)
c0		Replace all content to the left of cursor (exit with ESC)
:1,$s/s1/s2/g	(Yow!) global replacement of string1 with string2 
r 		replace current character with next character typed

Entering the Insert Mode:

i		Begin inserting text at current cursor location 
I		Begin inserting text at the beginning of the current line
a		Begin appending text, one character to the right of current
		cursor location
A		Begin appending text at the end of the current line
o/O		Begin entering text one line below\above current line
ESC		Exit insertion mode and return to command mode

Exiting and Entering VI

ZZ		save file and exit VI
:wq		same as above
:e!		return to last saved version of current file
:q		quit without save, (Note :q! is required if changes have been made)
:w		write without exit (:w! to force write)

Fancy Stuff:

:1,10w file   		write lines 1 through 10 to file newfile
:340,$w >> file		write lines 340 through the end of the file and append
			to file newfile
:sh			escape temporarily to a shell
^d			return from shell to VI
:![command]		execute UNIX command without leaving VI
:r![command]		read output of command into VI
:r[filename]		read filename into VI
:$r newfile		read in newfile and attach at the end of current document
:r !sort file		read in contents of file after it has been passed through
			the UNIX sort
:n			open next file (works with wildcard filenames,
			ex: vi file*)
:^g			list current line number
:set number		show line numbers
:set showinsert		show flag ("I") at bottom of screen when in insert mode
:set all		display current values of VI variables
:set ai			set autoindent; after this enter the insert mode and
			tab, from this point on VI will indent each line to
			this location.  Use ESC to stop the indentations.
^T 			set the autoindent tab one tab stop to the right 
^D 			set the autoindent tab one stop to the left
:set tabstop=n		sets default tab space to number n
>>			shift contents of line one tab stop to the right
<<			shift contents of line one tab stop to the left
___________________________________CCSF___________________________________

http://www.ccsf.edu/Pub/Fac/vi.html

Posted in UNIX-LINUX | Tagged: | Leave a Comment »

Sử dụng bộ soạn thảo vi trong UNIX và Linux

Posted by ZyK on 07/28/2011

Sử dụng bộ soạn thảo vi trong UNIX và Linux

Phần I: Giới thiệu vi:

Có rất nhiều trình soạn thảo trong UNIX cũng như Linux. Nhưng tình soạn thảo vi là trình soạn thảo được nhiều chuyên gia UNIX và Linux sử dụng. Có lẽ một phần là nó chiếm ít tài nguyên, nhưng cũng có thể là nó tồn tại trong UNIX và Linux từ ngày bắt đầu và không có một hệ UNIX hay Linux nào mà không chứa nó như một trình soạn thảo mặc định.

Ngồi vào máy bạn tạo, bạn tạo ra dữ liệu, thư điện tử, danh sách, bản ghi nhớ, ghi chú, báo cáo…vv. Bạn sẽ dùng một bộ soạn thảo(còn gọi là bộ chỉnh sửa, hoặc hiệu chỉnh) văn bản nào đó để làm công việc vừa kể. Linux có sẵn nhiều chương trình xử lý hoặc hiệu chỉnh văn bản để giúp bạn như: nano, cat, touch… Tuy nhiên để đưa các câu lệnh và trình shell vào một tập tin, bạn cần có một bộ soạn thảo (editor) có khả năng lưu trữ công việc của mình vào một tập tin văn bản dưới dạng mã ASCII. Linux giới thiệu bộ soạn thảo văn bản mang tên vi , mà bạn có thể dùng để hiệu chỉnh hầu như tất cả mọi thứ. vi rất hữu ích cho quản trị hệ thống vì chương trình này có sẵn trên mọi nền UNIX. Do đó khi đã quen tay với vi rồi, bạn tha hồ thao tác mọi hệ thống chạy UNIX. Ngoài ra vi cũng có lợi vì chiếm rất ít tài nguyên khi thi hành lệnh. Trong khi một số chương trình khác không thể chạy được vì xung đột phần cứng hoặc là vì hệ thống, vi vẫn hoạt động bình thường.

Hai bộ vi và ex đi kèm với bản phát hành Red Hat thực ra là danh xưng khác nhau của bộ soạn thảo vim (VI iMproved). Hai tên vi và ex gắn liền với vim, do đó khi chạy vi , thực ra bạn đang chạy vim. Mời bạn xem tập tin /usr/share/vim/vim61/doc/vi_diff.txt hay /usr/share/doc/vim-common-6.1/doc/vi_diff.txt để biết tóm tắt những điểm khác nhau giữa vi và vim.

Hình 1.1 Soạn thảo với vi

Linux có những bộ soạn thảo văn bản khác: một chương trình dạng đồ họa để dùng với XFree86 và hai bộ chuẩn không đồ họa mang tên ed và ex. Cả hai đều mang tính line-oriented, nghĩa là bạn chỉ có thể làm việc từng hàng một. Một chương trình khác mang tên emacs cũng có mặt trong các bản phát hành Linux. vi và emacs là những bộ soạn thảo toàn màn hình. Thông tin xuất hiện theo từng màn hình một để bạn có thể chỉnh sửa, thêm theo ngữ cảnh.

Trong bài này không bàn nhiều về ed hay ex, bởi vì bạn sẽ thấy vi rất dễ dùng và có sẵn trên mọi máy chạy UNIX, bao gồm cả Linux.

Muốn hiểu vi, bạn cần tìm hiểu vài nét về vi trong thế giới UNIX. Cho dù nhiều hệ điều hành ngày nay đã có những bộ soạn thảo mạnh mẽ và thân thiện với người sử dụng ví dụ như: Open Offices…, nhưng bạn vẫn nên tìm hiểu về vi bởi vì bất kỳ bản UNIX nào cũng có sẵn bộ vi. Đôi lúc vi là bộ soạn thảo duy nhất sẵn có vào lúc cần thiết, vì vậy bạn cần biết vài thao tác căn bản. Hơn nữa giờ đây với những phiên bản mới (v.6.x) vi đã trở nên thân thiện hơn và đã có nhiều phiên bản chạy trên Windows.

Trước đây UNIX được triển khai trong hoàn cảnh terminal của người sử dụng là máy teletype hoặc một dạng máy in kết quả ra giấy chạy chậm rì. Vào thời đó màn hình chưa được dùng nhiều. Bộ soạn thảo văn bản dùng cho môi trường như thế thường là line-oriented, mà người sử dụng chỉ thấy và chỉ làm việc trên một dòng lệnh duy nhất mà thôi.

Hiện nay các hệ UNIX còn dùng hai bộ soạn thảo line-oriented: ed và ex.

Khi UNIX bắt đầu ra đời, sinh viên đại học được dùng UNIX không mất tiền và cùng góp sức triển khai hệ điều hành này. Đại học California và Berkeley có nhiều đóng góp, trong đó có bộ soạn thảo toàn màn hình, thay thế việc xử lý từng dòng lệnh một. Bộ soạn thảo toàn màn hình này được gọi là vi, viết tắt của chữ “Visual”.

Đã đến lúc chuyển sang loại hình công việc screen-oriented, nhìn cả màn hình thay vì từng dòng một. Người sử dụng làm việc với các terminal nhìn trực tiếp vào màn hình, thay vì làm việc với các thiết bị in ra giấy.

Trong phần này chỉ giới thiệu đôi chút về vi, muốn biết vi hoạt động và sử dụng như thế nào mời các bạn đón đọc tiếp phần II – vi là gì?

II – Vi là gì ?:

Trong phần trước chúng ta đã được giới thiệu về VI và biết được sự ra đời cũng như tầm quan trọng và sự hữu ích của VI. Phần này các bạn sẽ hiểu được sự hoạt động của VI.

Cũng như hàng triệu người khác đang sử dụng vi, bạn sẽ thấy ứng dụng này khởi động nhanh và có thể dùng cho việc đơn giản cũng như phức tạp. Bạn dùng vi để nhập văn bản, chỉnh sửa hoặc xóa bỏ văn bản. Ngoài ra bạn còn tìm kiếm và thay thế, cắt, chép, và dán từng khối văn bản.

Vi cũng giúp bạn cắt xén tỉa gọt văn bản cho đúng ý mình. Bạn tùy nghi di chuyển con trỏ đến bất kỳ chỗ nào trên màn hình cũng như trên phạm vi văn bản đang được xử lý. Bộ soạn thảo vi không phải là phần mềm xử lý văn bản, cũng không là ứng dụng chế bản văn phòng, do đó sẽ không có trình đơn (menu) và cũng không có các tiện ích trợ giúp.

Các ứng dụng xử lý văn bản thường có hai định dạng, một loại trên màn hình và một dùng khi in ra, chẳng hạn như trình bày văn bản dạng in đậm, in nghiêng, hoặc gạch dưới. Vi lại không như thế. Các lệnh khác của Linux có thể thực thi vài điều vừa kể, chẳng hạn như lệnh lp thực hành in và lệnh nroff có khả năng định dạng văn bản. Một vài chương trình xử lý văn bản như TeX và LaTeX, có thể xử lý các câu lệnh nhúng trong văn bản, chẳng hạn như in đậm hoặc gạch dưới.

Bộ soạn thảo vi chạy ở hai chế độ khác nhau:

- Ở chế độ câu lệnh, những gì bạn gõ vào sẽ được hiểu như là câu lệnh ra lệnh cho vi. Lệnh sẽ bảo vi lưu tập tin, thoát khỏi vi, chuyển con trỏ đến các vị trí khác nhau trong tập tin, chỉnh sửa, sắp xếp, xóa bỏ, thay thế và tìm kiếm đoạn văn bản.

– Ở chế độ nhập liệu hoặc còn gọi là nhập văn bản (chế độ INSERT), những gì bạn gõ vào được máy hiểu là nội dung của tập tin mà bạn đang chỉnh sửa. Theo chế độ này, vi hành động như là chiếc máy đánh chữ đơn thuần.

Trong một phiên chỉnh sửa văn bản, bạn có thể chuyển từ chế độ này sang chế độ khác. Bạn phải nhớ mình đang ở chế độ nào và biết cách chuyển đổi. Chương này sẽ giới thiệu tùy chọn showmode giúp bạn biết xem vi đang thuộc chế độ nào. Chỉ cần tập luyện ít lâu là bạn sẽ thấy vi rất tiện lợi để xử lý các tập tin ASCII Linux, đặc biệt là các tập tin cấu hình và shell script.

Để biết được trình soạn thảo vi làm việc như nào và bạn có thể gỡ lỗi (debug error) xảy ra trong quá trình dùng vi thì mời các bạn đón đọc phần tiếp theo: Tìm hiểu tiến trình soạn thảo Vi – Phần III.

III – Tìm hiểu tiến trình soạn thảo và cách sử dụng Vi:

Bạn soạn thảo bằng cách tạo ra văn bản mới hoặc chỉnh sửa văn bản đã có sẵn. Tạo ra văn bản mới tức là viết chữ vào một tập tin được đặt tên theo dạng Linux. Khi chỉnh sửa, bạn dùng tên tập tin có sẵn để gọi bản sao của tập tin ấy ra trình diện tại phiên soạn thảo. Trong cả hai trường hợp, khi sử dụng bộ soạn thảo tức là bạn đã lưu giữ văn bản tại bộ nhớ của máy ở một vùng được gọi là vùng đệm.

Vùng đệm sẽ không thay đổi gì ở nội dung của tập tin cho đến khi bạn ra lệnh lưu nội dung vùng đệm. Chỉ khi nào bạn ra lệnh này xong, máy sẽ ghi những điều chỉnh sửa vào đĩa cứng. Lúc ấy những gì bạn vừa thay đổi mới có hiệu lực thường trực. Do đó nếu sau khi thao tác một lúc, bạn quyết định là không nên thay đổi gì ở nội dung cũ thì cứ để y nguyên như thế. Trong một phiên làm việc, bạn muốn lưu những thay đổi ấy bao nhiêu lần cũng được, và bạn nên ra lệnh lưu một cách đều đặn, phòng khi mất nguồn đột xuất hoặc treo máy. Khi ra lệnh lưu, bạn không phải thoát khỏi bộ soạn thảo. Chương này sẽ chỉ bạn nhiều cách thoát ra khỏi bộ soạn thảo.

Bộ soạn thảo vi được coi là có tính tương tác bởi vì chương trình sẽ giao lưu qua lại với bạn trong phiên làm việc. Vi liên lạc với bạn bằng cách hiển thị tình trạng phiên làm viêc, hoặc giả hiển thị những thông báo lỗi, hoặc đôi lúc trên màn hình không xuất hiện điều gì cả (đúng theo kiểu của Linux). Dòng cuối cùng trên màn hình, gọi là dòng trạng thái, mang những lời nhắn của Linux.

Bạn sử dụng bộ soạn thảo để chỉnh sửa, sắp xếp lại, xóa bỏ, tìm kiếm và thay thế đoạn văn bản. Các thao tác chỉnh sửa như thế được tiến hành ở chế độ soạn thảo. Trong nhiều trường hợp, lệnh chỉ là một ký tự tương ứng với chữ đầu tiên của tên một hành động. Ví dụ chữ I tương ứng với hành động Insert, và r được dùng để replace( thay thế) một ký tự.

Hầu hết các câu lệnh đều diễn ra trên một dòng hoặc nhiều dòng ký tự. Dòng được đánh số từ 1(dòng đầu tiên) cho đến dòng cuối cùng của vùng đệm. Khi bạn thêm hoặc bớt dòng, số hiệu của dòng sẽ tự động chỉnh lý. Số hiệu của dòng tức là địa chỉ của dòng đó trong vùng đệm. Vùng địa chỉ là hai địa chỉ hoặc hai số hiệu của dòng được cách bởi dấu phẩy. Nếu muốn xác định vùng địa chỉ từ dòng thứ ba đến dòng thứ tám vủa vùng đệm, bạn viết 3, 8.

Vị trí con trỏ luôn chỉ bạn biết vị trí hiện hàn của vùng đệm soạn thảo. Một vài lệnh mà bạn gõ ở chế độ ra lệnh sẽ tác động đến ký tự ngay chỗ con trỏ đang nhấp nháy. Nếu bạn không di chuyển con trỏ, những thay đổi sẽ diễn ra tại vị trí ấy. Bộ soạn thảo vi có nhiều lệnh để di chuyển con trỏ tới lui trong vùng đệm soạn thảo. Vi là bộ soạn thảo toàn màn hình. Bạn ra lệnh cho Vi để di chuyển con trỏ đến nhiều nơi trong tập tin, và bạn trực tiếp nhìn thấy những thay đổi diễn ra khi bạn thao tác. Vì thế Vi phải có khả năng di chuyển và chỉnh sửa văn bản trên terminal của chính bạn cũng như ở các loại terminal khác. Vi biết bạn đang dùng terminal nào, và khả năng màn hình của terminal ấy ra sao bằng cách xem xét biến shell mang tên TERM. Linux dùng biến TERM để xác định xem terminal làm được gì, chẳng hạn như gạch dưới, đảo màn hình, xóa màn hình, gán phím chức năng, và khả năng về màu sắc của terminal.

Bạn chỉ cần gõ Vi tại dấu nhắc shell tại dòng lệnh để kích hoạt Vi. Muốn tạo ra hoặc chỉnh sửa một tập tin nào đó, bạn gõ lệnh Vi chung với tên tập tin. Chẳng hạn muốn tạo tập tin abc với Vi, bạn gõ: Vi abc.

Khi Vi được kích hoạt, màn hình terminal sẽ tự động xóa trống, đồng thời một dấu sóng (~) xuất hiện ở phía bên trái mỗi đầu dòng, trừ dòng đầu tiên hay những dòng đã soạn thảo. Dấu sóng báo hiệu dòng trống của vùng đệm. Con trỏ nằm phía ngoài cùng bên trái của dòng đầu tiên. Bạn sẽ trông thấy từ 20 đến 22 dấu sóng ở phía trái màn hình. Nếu màn hình không đúng như thế, bạn nên kiểm tra lại vị trí của TERM. Còn nếu mọi sự diễn ra như mô tả, bạn đã khởi động được Vi, và Vi đang ở chế độ chờ lệnh.Để có thể thao tác hoặc sử dụng Vi một cách hiệu quả nhất, mời các bạn đón đọc phần tiếp theo: IV – Tìm hiểu hai chế độ của Vi và tạo ra tập tin Vi đầu tiên.

IV – Tìm hiểu hai chế độ của Vi và tạo ra tập tin Vi đầu tiên

Vi ( vim ) – the powerful & simple editor for linux

Linux là một hệ điều hành được điều khiển đa phần qua môi trường dòng lệnh ( console ), thêm vào đó, các file cấu hình dịch vụ trên Linux thường được chỉnh sửa bởi trình editor là vi ( hay vim, bản cải thiện của vi ). Nếu bạn là người thường xuyên quản trị server linux qua console, thì vi là trình tiện ích hay sử dụng nhất. Do vậy, học cách sử dụng vi là một yêu cầu tối thiểu và cần thiết cho một người sử dụng linux.

1. Cơ bản

- Sử dụng vi kèm theo tên file(s) muốn edit: vi one.txt two.txt etc.txt

– Có 2 mode: command mode và insert mode. Khi bắt đầu sử dụng lệnh vi, vi mặc định ở command mode. Hoặc ấn Esc để chuyển sang command mode khi người dùng đang ở insert mode.

2. Các lệnh edit cơ bản

- Ấn phím lệnh i hoặc a từ chế độ command mode để chuyển sang insert mode. i để thêm ký tự trước con trỏ, a để thêm ký tự sau con trỏ.

– Sử dụng lệnh h j k l hoặc các phím mũi tên tương ứng để di chuyển con trỏ sang trái, xuống, lên, sang phải.

– Sử dụng x xóa 1 ký tự, dw xóa 1 từ, dd xóa cả 1 dòng.

– Sử dụng số N đi trước phím lệnh để lặp lại N lần tác dụng của lệnh. Ví dụ, 3dw sẽ xóa 3 từ tính từ vị trí con trỏ.

– Sử dụng u ( = undo )để khôi phục lại những thay đổi trước đó.

– Sử dụng ZZ hay hay :wq lưu lại tất cả thay đổi và thoát.

– Sử dụng :q! thoát ra không lưu lại bất kỳ thay đổi nào.

3. Cắt và dán

- yy sao chép dòng hiện tại vào buffer, Nyy sao chép N dòng.

– p ( P ) dán nội dung từ buffer vào dưới ( trên ) dòng hiện tại

4. Nhảy đến hàng hay cột

- Gõ một số N trước ký tự G để đi đến dòng thứ N, vd 23G sẽ nhảy đến dòng 23.

– Gõ một số N trước ký tự | (pipe) để nhảy đến cột thứ N.

5.

Sử dụng . để lặp lại action gần nhất. Ví dụ người dùng gõ i để insert dòng chữ “hello world”, sau đó chuyển sang chế độ command mode bằng phím Esc, nhảy xuống dòng và gõ . , dòng chữ “hello world” sẽ hiện ra.

6. Tìm kiếm

- Sử dụng / (?) đi theo sau là từ muốn tìm để tìm kiếm từ trong phần văn bản sau ( trước ) con trỏ. ví dụ /foobar hay ?foobar.

– Sau khi kết quả tìm kiếm đầu tiên hiện ra, sử dụng n để tìm kiếm tiếp trong phần văn bản còn lại sau con trỏ, N để tìm kiếm ngược trở lại đầu văn bản trước con trỏ.

7. Các lệnh colon ( đi sau dấu : )

- :%s/foo/bar/g tìm sự xuất hiện của “foo” trong toàn bộ file và thay thế bằng “bar”, /foo/bar/g chỉ thay thế ở dòng hiện tại.

– et nu hiển thị số dòng trước mỗi dòng, et nonu để bỏ hiển thị số dòng.

– :1,8d xóa từ dòng 1 cho đến dòng 8 trong file.

– Sử dụng ma để đánh dấu dòng hiện tại là a ( có thể là bất cứ ký tự nào từ a-z ). Sau đó dùng ‘a để nhảy đến dòng đã được dánh dấu là a từ bất cứ đâu. Có thể sử dụng với colon, :’a,’b d xóa tất cả các dòng bắt đầu từ dòng được đánh dấu là a cho đến dòng được đánh dấu là b, hoặc ngược lại.

– :w newfile.txt để save nội dung của file hiện tại vào một file mới là newfile.txt ( tựa “save as” bên Win Word ).

– :8,16 co 32 để copy dòng 8 đến 16 đến điểm sau dòng 32.

– :3,16 m 32 để chuyển rời dòng 8 đến 16 đến điểm sau dòng 32.

– Nếu dùng vi để mở nhiều file ( vi file1 file2 file3 ), có thể sử dụng :n để nhảy đến file tiếp theo và :rew để nhảy quay ngược lại đến file đầu tiên, :args để hiện thị tất cả các file đang được mở.

8. Vi for Smarties

- Sử dụng G để nhảy đến dòng cuối cùng của file.

– Khi xóa nhiều dòng, di chuyển con chuột đến dòng đầu tiên, gõ ma để đánh dấu, sau di đến dòng cuối cùng và gõ d’a để xóa những dòng đó.

– $ để nhảy xuống cuối dòng, :$ để nhảy đến dòng cuối của file.

- 0 để nhảy đến đầu dòng, :0 để nhảy tới dòng đầu tiên của file.

– d$ xóa từ vị trí con trỏ hiện tại đến cuối dòng.

– :10,$ d xóa từ dòng 10 cho đến dòng cuối cùng của file, hoặc ngược lại.

– :10,20 m 0 chuyển rời dòng 10 đến 20 lên trên dòng đầu tiên của file.

9. Shell

- Sử dụng :!command để thi hành lệnh command trong môi trường vi.

Trên đây là giới thiệu cách sử dụng vi một cách cơ bản và đủ dùng cho mọi đối tượng người sử dụng linux. Người dùng muốn tham khảo thêm có thể lên website của vim tại http://vim.wikia.com/wiki/Best_Vim_Tips.

Trích từ Internet

Posted in UNIX-LINUX | Tagged: , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.