Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim, how can I shift a block of code to right?

Tags:

vim

I am using the Vim editor. Here is my situation:

1111111111111 2222222222222 3333333333333 4444444444444 

Above is the original code, I want to make them like below. What should I do to shift them all to the right?

    1111111111111     2222222222222     3333333333333     4444444444444 
like image 227
Yongwei Xing Avatar asked Aug 02 '10 07:08

Yongwei Xing


People also ask

How do I move a block of text in Vim?

In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up. After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.

How do I indent a block of code in Vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . )

How do I move text in Vim?

Put the cursor anywhere in the first line. Press V then jj to visually select the three lines. Press > to indent (shift text one ' shiftwidth ' to the right), or press < to shift left. Press . to repeat the indent, or u to undo if you have shifted too far.

What does shift j do in Vim?

Shift + J removes the line change character from the current line, so by pressing "J" at any place in the line you can combine the current line and the next line in the way you want. GJ. GJ.


2 Answers

In command mode, you can use >> to indent a single line. 4>> will indent the current and next three lines.

If you don't know how many lines in advance (it may be quite large), you can use ranges. Go to the first line of the range and enter ma to place marker A. Then go to the last line and enter >'a to indent from here to marker A. You can do all sorts of wonderful things with ranges.

How they're indented depends on a couple of things like your shiftwidth settings. I always have my shiftwidth and tabstop settings the same to avoid problems:

:set ts=4 sw=4 

(for example).

like image 85
paxdiablo Avatar answered Oct 10 '22 09:10

paxdiablo


If you've already selected the four lines in visual mode: > will shift them shiftwidth to the right. After they are shifted, the visual selection will be gone, but you can indent again via . (repeat last command).

If you are normal mode, with your cursor anywhere on the first line:

  • >> will indent that line,
  • 4>> will indent all four lines,
  • >3j will do the same thing in a different way (indent from this line to three lines down),
  • >} will indent all of the lines until the end of the paragraph (i.e. to the first empty line, see :help object-motions), and
  • >ap will indent all of the lines for a p-aragraph (see :help text-objects), even if your cursor isn't on the first line.

Again, you can repeat these commands via . for deeper indentation levels (or you can set shiftwidth appropriately).

If your file is nicely composed of "paragraphs" (and most of my code and prose is), I think you'll find the ap text-object to be the most common way to work on blocks of text like this. You can also use text-objects to speed up visual selection.

like image 37
nicholas a. evans Avatar answered Oct 10 '22 09:10

nicholas a. evans