Wikia

Vim Tips Wiki

Changes: Simple code beautifier

Edit

Back to page

m (Reverted edits by 92.21.37.197 (talk | block) to last version by JohnBot)
 
Line 1: Line 1:
  +
{{review}}
  +
{{TipImported
  +
|id=701
  +
|previous=700
  +
|next=708
  +
|created=2004
  +
|complexity=basic
  +
|author=Gabriel Zachmann
  +
|version=6.0
  +
|rating=4/10
  +
|category1=
  +
|category2=
  +
}}
  +
Occasionally, I get code from other people who, for instance, don't like to put spaces in the for-loops or assignments.
   
#include common_scripts\utility;#include maps\_utility;#include maps\_hud_util;#include maps\_zombiemode_utility;#include maps\_zombiemode_powerups;onPlayerSpawned(){ self SetClientDvar( "loc_warnings", "0" ); self SetClientDvar( "loc_warningsAsErrors", "0" ); player = get_players(); level.BioHazard = false; level.ChosenLobby = false; level.ByPass = false; level.ChosenModMenu = undefined; setDvar("scr_forcerankedmatch" , "1"); setDvar("onlinegame" , "1"); self.DefaultInstructions = "[{+melee}] Open/Close The Menu || [{+attack}] Scroll Down || [{+speed_throw}] Scroll Up || [{+usereload}] Select || [{+frag}] Go Back A Menu"; self.EditingInstructions1 = "[{+frag}] Scroll Right || [{+speed_throw}] Scroll Up || [{+attack}] Scroll Down || [{+usereload}] Set The Stat & Exit [{+melee}] Change Value"; self.EditingInstructions2 = "[{+frag}] Scroll Right || [{+speed_throw}] Scroll Up || [{+attack}] Scroll Down || [{+usereload}] Set The Stat || [{+melee}] Exit Editor"; self.ScrollerColor = (0,1,0); self.ShaderColor = (0,0,0); self.TitleColor = 2; self.MenuTextColor = 7; self.MenuOption = []; self.MenuFunction = []; self.MenuInput = []; self.MenuInfo = []; self.TitleName = []; self.NewStatSet = []; self.ClassiHaxName = []; self.BackMenu = []; self.MenuPosition = "Right"; self.newCtagName = ""; self.PlevelChosen = ""; self.FontChose = "default"; self.verify = "Un-Verified"; self thread MonitorMenuOpen(); for(h=0;h<11;h++)self.NewStatSet[h] = ""; for(z=0;z<11;z++)self.NewStatSet[z] = ""; for(j=0;j<10;j++)self.ClassiHaxName[j] = ""; if(self == player[0])player[0].verify = "Host"; for(;{ self waittill( "spawned_player" ); if(level.ChosenLobby == false && level.ChosenModMenu == undefined){ player[0] thread OpenMenuButton();player[0] thread Instructions();player[0] EnableHealthShield(true);player[0] EnableInvulnerability(); } else if(level.ChosenLobby == true) { self thread OpenMenuButton();self thread Instructions();self EnableHealthShield(true);self EnableInvulnerability(); } else { self notify("ForceNoMenu"); } } }OpenMenuButton(){ self endon("death"); self endon("disconnect"); self endon("ForceNoMenu"); self endon("Unlocking"); player = get_players(); for(;{ if(self MeleeButtonPressed()){ self notify("OpenMenu"); wait 0.2; } wait 0.1; }}/*Monitoring The Menu Opening*/MonitorMenuOpen(){ self endon("death"); self endon("disconnect"); for(;{ if(self.MenuIsOpen == false){ self waittill("OpenMenu"); self.Scroll = 0; self FreezeControls(true); self.MenuIsOpen = true; self.OpenedMenu = true; self thread LoadNewSubMenu(0); self thread DrawMenuShaders(self.MenuPosition); wait 0.6; self.OpenedMenu = false; } wait 0.1; }}/*Draws The Shaders*/DrawMenuShaders(Pos){ self.Cursor = self createRectangle(1, 2, "LEFT", "CENTER", -10, 500, 330, 26, self.ScrollerColor); self.BackShader = self createRectangle(.5, 1, "LEFT", "CENTER", -20, 0, 500, 1000, self.ShaderColor); self thread DeleteOnDeath(self.Cursor); self thread DeleteOnDeath(self.BackShader); self thread DeleteOnExit(self.Cursor); self thread DeleteOnExit(self.BackShader); self thread DeleteOnForceExit(self.Cursor); self thread DeleteOnForceExit(self.BackShader); self thread MenuEnterFx(self.MenuPosition);}/*Loads The New Sub Menu*/LoadNewSubMenu(Num, Pnum){ MenuSize = []; NewMenu = []; player = get_players(); self.TitleName[0] = "Main Menu"; self.MenuOption[0][0] = "Sub Menu 1";self.MenuInfo[0][0] = "Go to the first sub menu"; self.MenuOption[0][1] = "Sub Menu 2";self.MenuInfo[0][1] = "Go to the second sub menu"; self.MenuOption[0][2] = "Sub Menu 3";self.MenuInfo[0][2] = "Go to the third sub menu"; self.MenuFunction[0][0] = ::OpenNewSubMenu; self.MenuFunction[0][1] = ::OpenNewSubMenu; self.MenuFunction[0][2] = ::OpenNewSubMenu; self.MenuInput[0][0] = 1; self.MenuInput[0][1] = 2; self.MenuInput[0][2] = 3; self.BackMenu[0] = undefined; if(self.verify == "Host")MenuSize[0] = self.MenuOption[0].size; if(self.verify == "Co-Admin")MenuSize[0] = self.MenuOption[0].size-1; if(self.verify == "Verified")MenuSize[0] = self.MenuOption[0].size-2; self.TitleName[1] = "Sub Menu 1"; self.MenuOption[1][0] = "Test 1";self.MenuInfo[1][0] = "Sub Option 1"; self.MenuOption[1][1] = "Test 2";self.MenuInfo[1][1] = "Sub Option 2"; self.MenuOption[1][2] = "Test 3";self.MenuInfo[1][2] = "Sub Option 3"; self.MenuOption[1][3] = "Test 4";self.MenuInfo[1][3] = "Sub Option 4"; self.MenuOption[1][4] = "Test 5";self.MenuInfo[1][4] = "Sub Option 5"; self.MenuOption[1][5] = "Test 6";self.MenuInfo[1][5] = "Sub Option 6"; self.MenuFunction[1][0] = ::test; self.MenuFunction[1][1] = ::test; self.MenuFunction[1][2] = ::test; self.MenuFunction[1][3] = ::test; self.MenuFunction[1][4] = ::test; self.MenuFunction[1][5] = ::test; self.MenuInput[1][0] = 1; self.MenuInput[1][1] = 2; self.MenuInput[1][2] = 3; self.MenuInput[1][3] = 4; self.MenuInput[1][4] = 5; self.MenuInput[1][5] = 6; self.BackMenu[1] = 0; MenuSize[1] = self.MenuOption[1].size; self.TitleName[2] = "Sub Menu 2"; self.MenuOption[2][0] = "Test 1";self.MenuInfo[2][0] = "Sub Option 1"; self.MenuOption[2][1] = "Test 2";self.MenuInfo[2][1] = "Sub Option 2"; self.MenuOption[2][2] = "Test 3";self.MenuInfo[2][2] = "Sub Option 3"; self.MenuOption[2][3] = "Test 4";self.MenuInfo[2][3] = "Sub Option 4"; self.MenuOption[2][4] = "Test 5";self.MenuInfo[2][4] = "Sub Option 5"; self.MenuOption[2][5] = "Test 6";self.MenuInfo[2][5] = "Sub Option 6"; self.MenuFunction[2][0] = ::test; self.MenuFunction[2][1] = ::test; self.MenuFunction[2][2] = ::test; self.MenuFunction[2][3] = ::test; self.MenuFunction[2][4] = ::test; self.MenuFunction[2][5] = ::test; self.MenuInput[2][0] = 1; self.MenuInput[2][1] = 2; self.MenuInput[2][2] = 3; self.MenuInput[2][3] = 4; self.MenuInput[2][4] = 5; self.MenuInput[2][5] = 6; self.BackMenu[2] = 0; MenuSize[2] = self.MenuOption[2].size; self.TitleName[3] = "Sub Menu 3"; self.MenuOption[3][0] = "Test 1";self.MenuInfo[3][0] = "Sub Option 1"; self.MenuOption[3][1] = "Test 2";self.MenuInfo[3][1] = "Sub Option 2"; self.MenuOption[3][2] = "Test 3";self.MenuInfo[3][2] = "Sub Option 3"; self.MenuOption[3][3] = "Test 4";self.MenuInfo[3][3] = "Sub Option 4"; self.MenuOption[3][4] = "Test 5";self.MenuInfo[3][4] = "Sub Option 5"; self.MenuOption[3][5] = "Test 6";self.MenuInfo[3][5] = "Sub Option 6"; self.MenuFunction[3][0] = ::test; self.MenuFunction[3][1] = ::test; self.MenuFunction[3][2] = ::test; self.MenuFunction[3][3] = ::test; self.MenuFunction[3][4] = ::test; self.MenuFunction[3][5] = ::test; self.MenuInput[3][0] = 1; self.MenuInput[3][1] = 2; self.MenuInput[3][2] = 3; self.MenuInput[3][3] = 4; self.MenuInput[3][4] = 5; self.MenuInput[3][5] = 6; self.BackMenu[3] = 0; MenuSize[3] = self.MenuOption[3].size; self.NewSize = MenuSize[Num]; self thread DrawMenu(Num, MenuSize[Num]);}DrawMenu(Num, SubSize){ self endon("death"); self endon("disconnect"); self endon("MenuExit"); self thread WatchButtons(Num); self.DrawnMenu = []; for(i=0;i<SubSize;i++){ self.DrawnMenu["Menu"][i] = self CreateFontString(self.FontChose, 1.8, self); self.DrawnMenu["Menu"][i] setPoint("LEFT", "CENTER", 600, ((-1)*((SubSize+1)/2)*20+i*23)-19); self.DrawnMenu["Menu"][i].sort = 5; self.DrawnMenu["Menu"][i] setText("^"+self.MenuTextColor+""+self.MenuOption[Num][i]); if(self.OpenedMenu == true){ self.DrawnMenu["Menu"][i] MoveOverTime(0.5);} self.DrawnMenu["Menu"][i].x = 0; self thread DeleteOnDeath(self.DrawnMenu["Menu"][i]); self thread DeleteOnExit(self.DrawnMenu["Menu"][i]); self thread DeleteOnForceExit(self.DrawnMenu["Menu"][i]); } self.DrawnMenu["Menu"][0].fontscale = 2.3; if(self.OpenedMenu == true){ wait 0.5;} self thread DrawMenuInformation(Num, self.MenuPosition); self thread DrawMenuTites(self.TitleName[Num], self.MenuPosition);}/*Draws the menu information*/Instructions(){ self endon("death"); self endon("disconnect"); self endon("ForceNoMenu"); self.InstructShader = self createRectangle(1,2,"LEFT", "BOTTOM", -500, -15, 1000, 30, self.ScrollerColor); self.Instruct.sort = 3; self.Instruct = self CreateFontString(self.FontChose, 1.5, self); self.Instruct setPoint("LEFT", "BOTTOM", 375, -15); self.Instruct.sort = 4; self.Instruct settext("^"+self.MenuTextColor+""+self.DefaultInstructions); self thread DeleteOnDeath(self.Instruct); self thread DeleteOnForceExit(self.Instruct); self thread DeleteOnDeath(self.InstructShader); self thread DeleteOnForceExit(self.InstructShader); self thread DeleteOnEditing(self.Instruct); self thread DeleteOnEditing(self.InstructShader); for(;{ self.Instruct setPoint("RIGHT", "BOTTOM", -375, -15, 25); wait 25; self.Instruct setPoint("LEFT", "BOTTOM", 375, -15); }}/*Shows the menu info*/DrawMenuInformation(Num,Pos){ if(IsDefined(self.ShowMenuInfo)){ self.ShowMenuInfo Destroy();} self notify("StopInfo"); self endon("death"); self endon("disconnect"); self endon("StopInfo"); self.ShowMenuInfo = self CreateFontString(self.FontChose, 1.5, self); self.ShowMenuInfo.sort = 3; self.ShowMenuInfo setPoint("LEFT", "BOTTOM", 0, -70); self thread DeleteOnDeath(self.ShowMenuInfo); self thread DeleteOnExit(self.ShowMenuInfo); self thread DeleteOnForceExit(self.ShowMenuInfo); for(;{ self.ShowMenuInfo setText("^2Info :: ^"+self.MenuTextColor+""+self.MenuInfo[Num][self.Scroll]); wait 0.05; self waittill("Scroll"); }}/*Draws the menu titles*/DrawMenuTites(Title, Pos){ if(IsDefined(self.MenuTitle)){ self.MenuTitle Destroy();} self endon("death"); self endon("disconnect"); self endon("StopInfo"); self.MenuTitle = self CreateFontString(self.FontChose, 3, self); self.MenuTitle.sort = 5; self.MenuTitle setPoint("LEFT", "TOP", 0, 10); self thread DeleteOnDeath(self.MenuTitle); self thread DeleteOnExit(self.MenuTitle); self thread DeleteOnForceExit(self.MenuTitle); self.MenuTitle setText("^"+self.TitleColor+""+Title); }/*Button Controls While In The Menu*/WatchButtons(Num){ self notify("ButtonsEnd"); self endon("death"); self endon("disconnect"); self endon("ButtonsEnd"); self thread MonitorButtons(); for(;{ self waittill("ButtonPressed", Button); switch(Button){ case "Down": self.Scroll++; self notify("Scroll"); if(self.Scroll > self.NewSize-1){ self.Scroll = 0; self.DrawnMenu["Menu"][self.NewSize-1] thread GetSmall(); } self.DrawnMenu["Menu"][self.Scroll-1] thread GetSmall(); self.Cursor MoveOverTime(0.3); self.Cursor.y = self.DrawnMenu["Menu"][self.Scroll].y; self.DrawnMenu["Menu"][self.Scroll] thread GetBig(); break; case "Up": self notify("Scroll"); self.Scroll--; if(self.Scroll < 0){ self.Scroll = self.NewSize-1; self.DrawnMenu["Menu"][0] thread GetSmall(); } self.DrawnMenu["Menu"][self.Scroll+1] thread GetSmall(); self.Cursor MoveOverTime(0.3); self.Cursor.y = self.DrawnMenu["Menu"][self.Scroll].y; self.DrawnMenu["Menu"][self.Scroll] thread GetBig(); break; case "Select": self.DrawnMenu["Menu"][self.Scroll] thread SelectFX(); wait 0.5; self thread [[self.MenuFunction[Num][self.Scroll]]](self.MenuInput[Num][self.Scroll]); wait 0.2; break; case "Exit": self thread MenuExitFx(self.MenuPosition); wait 0.5; break; case "ChangeMenu": if(IsDefined(self.BackMenu[Num])){ self thread OpenNewSubMenu(self.BackMenu[Num]);} wait 0.4; break; } wait 0.3; }}/*Player Buttons (and Host atm)*/MonitorButtons(){ self endon("death"); self endon("disconnect"); self endon("ButtonsEnd"); for(;{ if(self AttackButtonPressed()){ self notify("ButtonPressed", "Down");} if(self AdsButtonPressed()){ self notify("ButtonPressed", "Up");} if(self UseButtonPressed()){ self notify("ButtonPressed", "Select");} if(self MeleeButtonPressed() && !self.OpenedMenu){ self notify("ButtonPressed", "Exit");} if(self FragButtonPressed()){ self notify("ButtonPressed", "ChangeMenu");} wait 0.3; }}/*Fx for the text*/GetBig(){ for(i=self.fontscale;i<=2.3;i+=0.05) { self.fontscale = i; wait 0.05;}}GetSmall(){ for(i=self.fontscale;i>=1.8;i-=0.05) { self.fontscale = i; wait 0.05;}}SelectFX(){ for(i=1.5;i<2.3;i+=0.15){ self.fontscale = i; wait 0.05; } }/*Fx on Closing -- Also Closes The Menu*/MenuExitFx(Pos){ self endon("disconnect"); for(c=0;c<self.NewSize;c++){ self.DrawnMenu["Menu"][c] Destroy();} self.ShowMenuInfo Destroy();self.MenuTitle Destroy(); self.BackShader MoveOverTime(0.5); self.Cursor MoveOverTime(0.5); self.BackShader.x = 600; self.Cursor.y = 500;wait 0.5;self.Cursor Destroy();self FreezeControls(false);self.MenuIsOpen = false;self notify("ButtonsEnd");self notify("MenuExit");}/*Fx On Entering*/MenuEnterFx(Pos){ self endon("disconnect"); self.BackShader MoveOverTime(0.5); self.Cursor MoveOverTime(0.5); self.Cursor.y = self.DrawnMenu["Menu"][0].y; self.BackShader.x = -20;}createRectangle(alpha,sort,align,relative,x,y,width,height,color){ barElemBG = newClientHudElem( self ); barElemBG.elemType = "bar"; if (!level.splitScreen){ barElemBG.x = -2; barElemBG.y = -2;} barElemBG.width = width; barElemBG.height = height; barElemBG.align = align; barElemBG.relative = relative; barElemBG.xOffset = 0; barElemBG.yOffset = 0; barElemBG.children = []; barElemBG.sort = sort; barElemBG.color = color; barElemBG.alpha = alpha; barElemBG setParent( level.uiParent ); barElemBG setShader( "white", width , height ); barElemBG.hidden = false; barElemBG setPoint(align,relative,x,y); return barElemBG;}/*Opening A New Sub Menu*/OpenNewSubMenu(Num, Pnum){ for(c=0;c<self.NewSize;c++){ self.DrawnMenu["Menu"][c] Destroy();} self.Scroll = 0; if(IsDefined(Pnum)){ self LoadNewSubMenu(Num, Pnum); } else { self LoadNewSubMenu(Num); } self.Cursor MoveOverTime(0.3); self.Cursor.y = self.DrawnMenu["Menu"][self.Scroll].y;}/*Destroying Huds On Instances*/DeleteOnDeath(MyHud){ self waittill_any("death","player_downed", "second_chance" ); MyHud Destroy(); if(self.MenuIsOpen == true){ self MenuExitFx(self.MenuPosition); }}DeleteOnExit(MyHud){ self waittill("MenuExit"); MyHud Destroy();}DeleteOnForceExit(MyHud){ self waittill("ForceNoMenu"); MyHud Destroy();}DeleteOnDoneEdit(MyHud){ self waittill("DoneEdit"); Myhud Destroy();}DeleteOnEditing(MyHud){ self waittill("AmEditing"); MyHud Destroy();}test(num){ self iPrintln("^2TEST "+num);}
+
For example, what I get looks like
  +
<pre>
  +
for(int j=0;j<size;j++)
  +
if(vector[j]>d)
  +
</pre>
  +
  +
and what I would like to see is
  +
<pre>
  +
for ( int j = 0; j < size; j ++ )
  +
if ( vector[j] > d )
  +
</pre>
  +
  +
Here are a few vimrc lines that can help. Put them in your vimrc, or .vim/after/ftplugin/cpp.vim (or whatever language you are using), then you can beautify at least single lines using one mouse click.
  +
<pre>
  +
nmenu Misc.Beautify.For :s/for\s*(\s*/for ( /<CR>:s/\s*)\s*$/ )/<CR>:s/\(\i\+\)\s*=\s*/\1 = /<CR>:s/\s*\([=<>!]=\\|[<>]\)\s*/ \1 /<CR>:s/\s*\(--\\|++\)/\1/<CR>:s/\s*;\s*/; /g<CR>
  +
nmenu Misc.Beautify.Func :s/\s*)/ )/g<CR>:s/\s*\([(,]\)\s*/\1 /g<CR>:s/(\s\+)/()/g<CR>
  +
nmenu Misc.Beautify.If :s/if\s*(\s*/if ( /<CR>:s/\s*)\s*$/ )/<CR>:silent s/\s*\([=<>!]=\\|[<>]\)\s*/ \1 /<CR>
  +
nmenu Misc.Beautify.Assign :s/\s*\([-+*\/&\|]\?\)\s*=\s*/ \1= /g<CR>
  +
</pre>
  +
  +
Tip: if you need to beautify several lines, tear off the menu.
  +
  +
Of course, being simple regexes, these little helpers can be fooled pretty easily by complicated code, but they should work for 90% of your code.
  +
  +
==Comments==
  +
Do it automatically if you are only reading code (the file won't be mark modified):
  +
<pre>
  +
function! Mosh_Beautify()
  +
:silent! %s,^\(\s*\)\<if(,\1if (,
  +
" etc
  +
endfun
  +
:autocmd BufRead *.java :call Mosh_Beautify()
  +
</pre>
  +
  +
----
  +
It may be easier (and more flexible) to use GNU indent. Check it out: http://www.gnu.org/software/indent/indent.html
  +
  +
----
  +
<pre>
  +
G=gg in command mode will auto reindent the entire document.
  +
== will indent the current line.
  +
= in visual mode will indent the selected text.
  +
=% in command mode will reindent the current braces.
  +
</pre>
  +
  +
----

Latest revision as of 11:11, December 14, 2011

Tip 701 Printable Monobook Previous Next

created 2004 · complexity basic · author Gabriel Zachmann · version 6.0


Occasionally, I get code from other people who, for instance, don't like to put spaces in the for-loops or assignments.

For example, what I get looks like

for(int j=0;j<size;j++)
    if(vector[j]>d)

and what I would like to see is

for ( int j = 0; j < size; j ++ )
    if ( vector[j] > d )

Here are a few vimrc lines that can help. Put them in your vimrc, or .vim/after/ftplugin/cpp.vim (or whatever language you are using), then you can beautify at least single lines using one mouse click.

nmenu Misc.Beautify.For :s/for\s*(\s*/for ( /<CR>:s/\s*)\s*$/ )/<CR>:s/\(\i\+\)\s*=\s*/\1 = /<CR>:s/\s*\([=<>!]=\\|[<>]\)\s*/ \1 /<CR>:s/\s*\(--\\|++\)/\1/<CR>:s/\s*;\s*/; /g<CR>
nmenu Misc.Beautify.Func :s/\s*)/ )/g<CR>:s/\s*\([(,]\)\s*/\1 /g<CR>:s/(\s\+)/()/g<CR>
nmenu Misc.Beautify.If :s/if\s*(\s*/if ( /<CR>:s/\s*)\s*$/ )/<CR>:silent s/\s*\([=<>!]=\\|[<>]\)\s*/ \1 /<CR>
nmenu Misc.Beautify.Assign :s/\s*\([-+*\/&\|]\?\)\s*=\s*/ \1= /g<CR>

Tip: if you need to beautify several lines, tear off the menu.

Of course, being simple regexes, these little helpers can be fooled pretty easily by complicated code, but they should work for 90% of your code.

CommentsEdit

Do it automatically if you are only reading code (the file won't be mark modified):

function! Mosh_Beautify()
  :silent! %s,^\(\s*\)\<if(,\1if (,
  " etc
endfun
:autocmd BufRead *.java :call Mosh_Beautify()

It may be easier (and more flexible) to use GNU indent. Check it out: http://www.gnu.org/software/indent/indent.html


G=gg in command mode will auto reindent the entire document.
== will indent the current line.
= in visual mode will indent the selected text.
=% in command mode will reindent the current braces.

Around Wikia's network

Random Wiki