Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:43:28+00:00 2026-06-02T19:43:28+00:00

Recently I was in need of a faster way to format similar code lines

  • 0

Recently I was in need of a faster way to format similar code lines according a common character (usually =). For example, I want to format this:

myVar = getMyVar();
myLongerVar = getMyLongerVar();
myYetLongerVar = getMyYetLongerVar();

into that:

myVar          = getMyVar();
myLongerVar    = getMyLongerVar();
myYetLongerVar = getMyYetLongerVar();

then I wrote the following mappings:

" query used to find the common character. In this case i'm setting it to "find the ="
let g:defformatquery = "f="

" set current line as having the longer size till the common character
nnoremap <Leader>gm 0
            \:execute "normal " . g:defformatquery<CR>
            \:let b:epos = getpos(".")[2]<CR>

" format current line according to the position acquired above
nnoremap <Leader>g= 0
            \:execute "normal " . g:defformatquery<CR>hvgeld
            \:execute "normal " . (b:epos - getpos(".")[2]) . "i "<CR>

To use them I have to perform these steps (assuming , is my <Leader>):

  1. position the cursor in the line with the longer text before the = sign (the third line in the example provided, myYetLongerVar)
  2. press: ,gm
  3. for each line I want to format, position the cursor there and press ,g=

Although this works, the process is kinda slow.
I want to create a function that would format the entire selected area at once. Then I could just create one map for the function.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:43:29+00:00Added an answer on June 2, 2026 at 7:43 pm

    You should try the Align plugin.

    For example, to align some selected lines (selected with v or CTRL-v) according to the = sign, you just type:

    :Align =
    

    Or to align from line 34 to 39:

    :34,39Align =
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently inherited some Java code and need to integrate it into a
I was recently going through some code and considering whether I need to be
I've been making a lot of changes recently and I need a way to
Recently I need to make a chart of employees' workload.Any recommendation? Thanks.
I've recently had the need to use the managers compiler argument, because the project
I've got a problem about onbeforeunload recently that I need to pop up a
I've been working on a spring security recently and I need to know how
I need to gain access to the list of most recently used programs and
I recently started a project in Symfony2 and I need to run some methods
Recently I discovered, that if I need to see if variable is even (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.