December 3, 2009

How to remove trailing ^M (carriage return) from within VI?

Filed under: Linux, Vi(m) — Administrator @ 4:30 pm


:%s/^M//g

or


:set ff=unix

October 18, 2005

Vi Tips 10

Filed under: *NIX, Vi(m) — Administrator @ 2:41 pm

Moving between screens:

Press the ESC key to enter command mode before using these commands.

forward one screen ^F (Ctrl+f)

backward one screen ^B (Ctrl+b)

forward half screen (down) ^D (Ctrl+d)

backward half screen (up) ^U (Ctrl+u)

Vi Tips 9

Filed under: *NIX, Vi(m) — Administrator @ 2:28 pm

1. Want to put numbers for each line?

:set number

2. Want to get rid of the numbers in front of the lines?

:set nonu

4. Want to find out what is the line number of the line your cursor is on?

Put your cursor on the line and press “Ctrl+g”

5. Don’t want the numbers in front of each line but want to see line number for each line for once while you are in the file?

:%nu

October 17, 2005

Vi Tips 8

Filed under: *NIX, Vi(m) — Administrator @ 5:56 pm

Vim has many more features than Vi, but most of them are disabled by default.
To start using more features you have to create a “vimrc” file.

1. Start editing the “vimrc” file, this depends on your system:

:edit ~/.vimrc for Unix

:edit $VIM/_vimrc for MS-Windows

2. Now read the example “vimrc” file text:

:read $VIMRUNTIME/vimrc_example.vim

3. Write the file with:

:write

The next time you start Vim it will use syntax highlighting.
You can add all your preferred settings to this “vimrc” file.

Vi Tips 7

Filed under: *NIX, Vi(m) — Administrator @ 5:52 pm

You can find help on just about any subject, by giving an argument to the
“:help” command. Try these (don’t forget pressing ):

:help w
:help c_ :help insert-index
:help user-manual

Vi Tips 6

Filed under: *NIX, Vi(m) — Administrator @ 5:50 pm

1. Typing o opens a line BELOW the cursor and places the cursor on the open
line in Insert mode.
Typing a capital O opens the line ABOVE the line the cursor is on.

2. Type an a to insert text AFTER the character the cursor is on.
Typing a capital A automatically appends text to the end of the line.

3. Typing a capital R enters Replace mode until is pressed to exit.

4. Typing “:set xxx” sets the option “xxx”
“:set ic” to “Ignore case” while searching.
“:set hls is” to set the ‘hlsearch’ and ‘incsearch’ options.
“:nohlsearch” to remove the highlighting of matches.

Vi Tips 5

Filed under: *NIX, Vi(m) — Administrator @ 5:44 pm

1. :!command executes an external command.

Some useful examples are:
(MS-DOS) (Unix)
:!dir :!ls - shows a directory listing.
:!del FILENAME :!rm FILENAME - removes file FILENAME.

2. :w FILENAME writes the current Vim file to disk with name FILENAME.

3. :#,#w FILENAME saves the lines # through # in file FILENAME.

4. :r FILENAME retrieves disk file FILENAME and inserts it into the
current file following the cursor position.

Vi Tips 4

Filed under: *NIX, Vi(m) — Administrator @ 5:43 pm

1. Ctrl-g displays your location in the file and the file status.
Shift-G moves to the end of the file. A line number followed
by Shift-G moves to that line number.

2. Typing / followed by a phrase searches FORWARD for the phrase.
Typing ? followed by a phrase searches BACKWARD for the phrase.
After a search type n to find the next occurrence in the same direction
or Shift-N to search in the opposite direction.

3. Typing % while the cursor is on a (,),[,],{, or } locates its
matching pair.

4. To substitute new for the first old on a line type :s/old/new
To substitute new for all ‘old’s on a line type :s/old/new/g
To substitute phrases between two line #’s type :#,#s/old/new/g
To substitute all occurrences in the file type :%s/old/new/g
To ask for confirmation each time add ‘c’ :%s/old/new/gc

Vi Tips 3

Filed under: *NIX, Vi(m) — Administrator @ 5:42 pm

1. To replace text that has already been deleted, type p . This Puts the
deleted text AFTER the cursor (if a line was deleted it will go on the
line below the cursor).

2. To replace the character under the cursor, type r and then the
character which will replace the original.

3. The change command allows you to change the specified object from the
cursor to the end of the object. eg. Type cw to change from the
cursor to the end of the word, c$ to change to the end of a line.

4. The format for change is:

[number] c object OR c [number] object

Vi Tips 2

Filed under: *NIX, Vi(m) — Administrator @ 5:41 pm

1. To delete from the cursor to the end of a word type: dw

2. To delete from the cursor to the end of a line type: d$

3. To delete a whole line type: dd

4. The format for a command in Normal mode is:

[number] command object OR command [number] object
where:
number - is how many times to repeat the command
command - is what to do, such as d for delete
object - is what the command should act upon, such as w (word),
$ (to the end of line), etc.

5. To undo previous actions, type: u (lowercase u)
To undo all the changes on a line type: U (capital U)
To undo the undo’s type: CTRL-R

Vi Tips 1

Filed under: *NIX, Vi(m) — Administrator @ 5:40 pm

1. The cursor is moved using either the arrow keys or the hjkl keys.
h (left) j (down) k (up) l (right)

2. To enter Vim (from the % prompt) type: vim FILENAME

3. To exit Vim type: :q! to trash all changes.
OR type: :wq to save the changes.

4. To delete a character under the cursor in Normal mode type: x

5. To insert text at the cursor while in Normal mode type:
i type in text

Proudly powered by wordpress - Theme by neuro