Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vi question 3

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
consider the following script outline:

some statements
some statements

if [[something]]
then {
statement
statement
statement
}
fi


with vi, how could I indent(or tab over) the three statements within the if conditional without doing it line by line?

Thanks for any help.
 
If you know the line numbers of the statements (eg if they're on lines 6,7 & 8) then do:

:6,8s/^/<TAB>/

(^ means beginning of the line)

or put the cursor on the first line & do:

:.,.+2s/^/<TAB>/

(current line to current line+2)

HTH, Chris

 
Hi peteDuke,

within vi use the following ex command (type : when you are in command mode):

3,5s/^/ / (where you hit the tab key after the second /; you can configure the tablength as well with the ex command &quot;set tabstop=...&quot;. &quot;set all&quot; gives you all options)

HTH

mrjazz [pc2]
 
or if there are too many lines to count easily, got to the first line and mark it wih the letter q, say, by entering

mq

then got to the last line and enter

'q,.s/^/<tab>/ CaKiwi
 
or.........

assumming you need to indent 3 line......
place the cursor on the FIRST line to indent and say:

3>> vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Wow, my wrists thank all of you!!!
That's what I love about UNIX. Always more than one way to do it.

I now recall Chris' way of doing it using ex (while in school), but I really like vlad' usage. Thanks to all!!
 
Hi,

Some Quick Guide for vi editor to share. Hopes it helps.

n = number
c = character
(cr) = Return
del = delete

Cursor Movement Through Text:

l = character right h = character left
j = down a line k = up a line
w = word right b = word left
0 = beginning of line $ = end of line
( = beginning of sentence ) = end of sentence
{ = beginning of paragraph } = end paragraph
[[ = beginning of section ]] = end section
G = end of file nG = move to line n
- = beginning of next line up + or cr = start, next line down

Cursor Movement Through File:

nH = to top line down offset
nL = to bottom line up offset
M = to middle line
CTRL d = scroll down
CTRL u = scroll up
CTRL f = scroll forward 1 screen
CTRL b = scroll back 1 screen
CTRL e = scroll forward 1 line
CTRL y = scroll back 1 line
CTRL E = scroll forward,leave cursor
CTRL Y = scroll back,leave cursor

Text Operations:

d( = delete, cursor to start(sentence)
d) = delete, cursor to end(sentence)
x or dSPACE = delete a character
db = delete previous word
dw = delete a word
d0 = delete, cursor to start(line)
dd = delete a line
D = delete, cursor to end(line)
nJ = join n lines
i(cr) or a(cr) = split a line
. = repeat last command
u = undo last command
U = undo all commands to this line

Text Block Moving in Command Mode:

Marking and Returning:
mx = mark current line (x=some letter a-t)
`x = return to position prior to present position
'x = return to space of line marked x
&quot; = return to position prior to commands. /,?, or G
d`x or d&quot;x = del from here to mark x

Placing in buffer (unnamed & named)
y = yank from here to endpoint, if # given include lines
nyw = yank n words
nyy or nY = yank n lines
&quot;anY = yank n lines, place in named buffer a, (a thru t)

Retrieving from buffer
p = put buffer contents after/below cursor
P = put buffer contents before/above cursor
&quot;ap = put buffer a 's contents after cursor (a -t)
&quot;np = recover the last 1-9 deletions of text
p = recover last deleted text, place after cursor
P = recover last deleted text, place before cursor

Buffer types
% = current buffer
# = alternate buffer
1 - 9 = buffers holding last 9 yanks/deletions
a - z = buffers holding explicit yanks/deletions
A - Z = buffers holding explicit yanks/deletions;
contents appended to
:m,nco# = copy lines m thru n after line #
:m,nm# = move lines m thru n after line #
>L = shift lines right 1 shiftwidth (23 line max,
cursor line and below)
<L = shift lines left 1 shiftwidth (23 line max,
cursor line and below)
>> = shift 1 line right 1 shiftwidth
#>> = shift # lines right 1 shiftwidth
<< = shift 1 line left 1 shiftwidth
#<< = shift # lines left 1 shiftwidth

Insert Mode or Text Mode from Command Mode:

r = replace a single character
R = replace; text overwrite from here to <esc>
i = insert before cursor
I = insert at front of line
a = append after cursor
A = append at end of line
o = open a line below
O = open a line above
s = substitute a character
S = substitute text with text
c = change from here to <esc>
cc= change # of lines
C = change from here to rest of line; if # given, then # following
lines also
CTRL t = shift right 1 shiftwidth (ai set)
CTRL d = shift left 1 shiftwidth (ai set)
CTRL v = quote(esc) next special character

Adding Text, Command Mode to Text Mode:

ncw = changes n words after cursor
C = change from cursor to end of line
ncc = changes n lines beginning with cursor linen

Search Commands from Command Mode:

CTRL g = find current line status
/word = find next occurrence of word, forward search
/ = find last requested searched for word
// or n = find next occurrence of the last searched for word
?word = find previous occurrence of word, backward search
N = search backwards for previous occurrence of last searched word
fc = find character c on current line
Fc = find previous c on current line
; = go to next/previous character on same line

Search patterns:

^ = first word of line $ = end of line
. = any one character .* = any characters
\< = beginning of word \> = end of word
\ = next char literally [a-z] = any character in range a-z
[str] = any chars in string [^str] = any char not in string

Global Substitution from Command Mode:

:g/s1/p = prints all lines with string &quot;s1&quot;
:g/^/s//string = prepend to each line
:g/$/s//string = append to each line
:g/s1/s//s2/ = sub 1st occurrence &quot;s1&quot; with &quot;s2&quot; on all lines
:g/s1/s//s2/g = sub all occurrences, all lines
:g/s1/s//s2/gc = &quot; &quot; &quot; / &quot; &quot; , interactively
:g/\(ab\)\(cd\)/s//\2\1/g = swap patterns using numeric
position variable,all
:g/\(ab\)\(.\)/s//\2\1/g = swap patterns using numeric
position variable, all, without naming 2nd
variable ( . = any character)

File Manipulation from Command Mode:

!}fmt = format this paragraph
:w filename = writes contents to specified file
:w ! spell | fmt = To just see spelling errors:
:$r !spell % | fmt = To append spelling errors to buffer
:w !lpr = print the version currently in the edit buffer
:r filename = reads contents of filename into buffer after cursor
:r !cmd = read output of a command into buffer after cursor
:!cmd = execute ULTRIX commands in vi/ex mode
:!! = execute last shell cmd
:!lpr % = print it without leaving the editor
:pre } use this when OUT of file system space and can't write
:w /var/tmp} file normally, or look for owner id in
/usr/preserve or /var/tmp

To Exit:

Insert Mode to Command Mode: ESC
To save text and QUIT: ESC then ZZ
ESC then :wq (cr)
ESC then :x (cr)
To rename file: ESC then :f (cr)
To save text only: ESC then :w (cr)
To overwrite file ESC then :w! (cr)
To abort text: ESC then :q! (cr)
To edit next file in queue: ESC then ZZ then :n

File recovery after editor, system, or disk full crash, return to directory where file was opened and enter:

vi -r file

To start vi at line #x:

vi +x filename

To start vi at string, first occurrence:
vi +/string file

The following options can be setup in an .exrc file, or for each vi session (only):

noautoindent nonumber
autoprint open
noautowrite nooptimize
nobeautify paragraphs=IPLPPPQPP LIpplpipbp
directory=/tmp prompt
noedcompatible noreadonly
noerrorbells redraw
hardtabs=8 remap
ignorecase report=5
nolisp scroll=11
nolist sections=NHSHH HUnhsh
magic shell=/bin/csh
mesg shiftwidth=8
nomodeline noshowmatch
noslowopen tags=tags /usr/lib/tags
tabstop=8 taglength=0
term=vt100 noterse
ttytype=vt100 timeout
window=23 warn
wrapscan wrapmargin=1
nosourceany nowriteany


Thanks

Mok
 
a lazy way to to do it.....

lines like this

if(something()){
fn1();
fn2();
fn3();
fn4();
}

Put the cursor on the if() line. Press the following keys.

$>%<<$%<<

Looks like greek, I know, try it......

Then, stick it in a macro and attach it to a funtion key, you don't have to tell it how many lines to indent.

Mok123 -- That's an excellent quick reference, why don't you turn it into a Tek-Tips FAQ?
Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Hi Mike,

Thanks for the advice. I've include the &quot;vi editor quick reference&quot; in the FAQ.

cheers,

Mok

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top