Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - Indent multiple lines with tab

I want to be able to highlight lines I want to indent/reverse-indent and indent with tab and shift-tab respectively.

" for command mode reverse tab
nmap <S-Tab> <<
" for insert mode reverse tab
imap <S-Tab> <Esc><<i
" for command mode multiple line reverse tab(doesn't work)
nmap <Tab> i<
" for insert mode multiple line reverse tab(doesn't work)
imap <Tab> <

" for command mode tab
nmap <Tab> >>
" for command mode multiple line tab(doesn't work)
nmap <Tab> i>
" for insert mode multiple line tab(doesn't work)
imap <Tab> >

This is the last thing I need before I'm willing to use vim as my primary editor.

like image 598
deadghost Avatar asked Sep 17 '11 03:09

deadghost


People also ask

How do you indent multiple lines?

If you prefer using [spacebar] to indent your code rather than using [tab], you can select multiple lines by holding the [alt] key and clicking on the beginning of each line you want to indent. Then, you can press [spacebar] and all the selected lines will be affected. omg it moves!

How do I indent a tab in Vim?

In normal mode, press Tab or Shift-Tab to adjust the indent on the current line and position the cursor on the first nonblank character; in insert mode, press Shift-Tab to unindent; in visual mode, press Tab or Shift-Tab to adjust the indent on selected lines.

How would you indent a block of text 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 . )


1 Answers

Quick start

In visual mode,

  • < will shift selected lines leftwards and
  • > rightwards.

How to mark text in visual mode

To select and highlight your text, you need to start using visual mode, (I usually do this by hitting v, or if coming from insert mode: Escv) and select your text using the standard motions (such as h, j, k and l) .

If you like, map other keys (not recommended)

If you do not want to use the default shortcuts < and >, create mappings for use in visual mode. You need :vmap:

:vmap <Tab> >
:vmap <S-Tab> <

which would mean pressing

  • Tab for "increase indent" and
  • ShiftTab for "decrease indent"

But why do you need to create a mapping for this?

Sometimes it's better just to learn the Vim (or even Vi) keys (and < and > are easy anyway), and then you can use any installation, not just the one with your .vimrc.

For instance, you've put:

imap <Tab> <

This is a bad idea. This will make it harder for you to insert a tab-character into your text (even ctrl+i won't work as I'd expect), and every time you type tab you'll insert a <.

Happy editing!

like image 90
Johnsyweb Avatar answered Sep 19 '22 09:09

Johnsyweb