showkey
showkey

Reputation: 338

with what can substitute number >> in vim ?

I want to insert 8 blanks at the front of many lines of code, my cursor is in the front of if strftime() ,i want to add 8 blanks in all lines from if strftime() till endif,

13>>   #count how many lines behind the cursor ,and `number >>`  can do.

I don't want to count how many lines behind the cursor ,is there simple way to do that?

enter image description here

Upvotes: 0

Views: 99

Answers (6)

Ingo Karkat
Ingo Karkat

Reputation: 172768

The > command takes a {motion} that selects the range of lines to be indented. Now for Vimscript's if...endif, the matchit.vim plugin (that ships with Vim, see :help matchit) adds cycling through those. As you have else clauses in between, you need to use the reverse g% motion to cycle backwards to the final endif.

So, the shortest command to indent is >g%.

Upvotes: 0

sumit kumar
sumit kumar

Reputation: 652

Open file in visual mode by pressing v

now select the block of code you want to modify

press > to insert a tab at the beginning of each line of selected block. and you have done.

Upvotes: 0

user539810
user539810

Reputation:

Another way using marks:

  1. Position your cursor at the beginning of the first line where you want to insert the blanks, perhaps by typing /if (strftime/ in command mode. Now type m followed by any letter to set the mark with that letter as a reference. For example, I typed m followed by a to set mark 'a'.

  2. Position your cursor at the beginning of the last line where you want to insert blanks. Now set a mark using a different letter (I used mark 'b'.)

Now as your question is unclear about whether you want to shift the lines by inserting 8 blanks at the start or whether you actually want to shift the line using the shift command 8 times, I'll answer with both:

  • Shift by shiftwidth 8 times:

    :'a,'b>>>>>>>>
    

    The quantity of > characters is the number of times to shift (8 in this case), and 'a and 'b are the mark names preceded by the ' character.

  • Shift by inserting 8 leading spaces: type

    :'a,'bs/^/        /
    

    Yes, the spaces must be typed out fully.

Upvotes: 0

Alex Brown
Alex Brown

Reputation: 42942

Here's how to do it simply without having to estimate the lines.

  1. Start the cursor on the first or last line to indent
  2. put VI in visual mode using CTRL-V
  3. move the cursor to the other end of the block to indent
  4. hit >

enter image description here

Upvotes: 1

poiu2000
poiu2000

Reputation: 980

This is what I would do in such cases:

  1. put your cursor at the beginning of the start line of the block you want to select , then Ctrl + v (or Ctrl + Q in GVim) to get into visual mode
  2. use j to move the cursor down till the end line of the block
  3. I (capital) get to edit mode
  4. insert two tabs
  5. press ESC to apply the change to all the previous selected lines

I tried another option it also works for me:

  1. :set nu to display the line numbers
  2. :34,46>> then the lines in the range between line 34 and 46 would do the >> change, you can change the two line numbers to yours in your case.

Upvotes: 1

Ben
Ben

Reputation: 8905

Using >> with a count, if you turn on relative line numbering (:set relativenumber), you can get the count of lines very easily.

Or, use > instead of >>, which takes a motion command afterward. If there is a blank line after your block, just use >} to indent up to the blank line. Or it could be a search, like >/endif<Enter>.

Or visually select what you want and press > once.

Upvotes: 1

Related Questions