Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one align code (braces, parens etc) in vi?

How do you prettify / align / format code in vi? What is the command?

I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.

x

like image 573
Xofo Avatar asked Apr 06 '10 18:04

Xofo


People also ask

How do I move multiple lines in vi?

Steps. Press “<SHIFT> + v” to enter VISUAL LINE mode. Select the text you wish to indent but using either the cursor keys or the “j” and “k” keys. To indent press “<SHIFT> + dot” (> character).


1 Answers

These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...

Automatic Indentation

To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:

:filetype indent on 

You can disable auto-indentation with

:filetype indent off 

Automatically adjusting/correcting indentation

In general, ={motion} will align code to an indentation level.

  • == align the current line
  • =i{ align the inner block
  • =% align to the matching parenthesis/bracket under the cursor
  • =14j or 14== align the next 14 lines
  • =G align to the end of the file
  • vG= same thing, align to the end of the file (but using visual mode)
  • vjjj= align four lines (using visual mode)

Manual indentation

If vim is not guessing the indentation level correctly, there are two ways to change it:

  • If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
  • If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D

Aligning equals signs, etc

If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294

Adjusting indentation/tab sizes

If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)

 :set expandtab 

To set how many spaces equal a tab, I usually do this:

 :set expandtab softtabstop=3 tabstop=3 shiftwidth=3 
  • tabstop - how many columns a tab counts for (affects display of existing tab characters)
  • shiftwidth - controls reindentation size with << and >>, among other commands.
  • softtabstop - how much space to insert when you press the tab key
  • expandtab - expand tab keys to spaces

But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:

function! Ktabs(tabsize)     execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize     "set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize endfunction noremap <leader><Tab> :call Ktabs(3)<Left> 

If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:

:retab 
like image 89
Kimball Robinson Avatar answered Oct 16 '22 09:10

Kimball Robinson