5

I am seeing this in Vim 7.3. I have autoindent on. Sometimes, when I move some code to the next line, the indentation of the line it was originally on changes, and then the newly created line is set to match that indentation. I don't mind that the new line indentation matches the previous one, since that is what autoindent is for, but why is the line I was on randomly changing its indentation?

Here is an example. This is how the text is formatted before I make changes.

    $sql= "  
        SELECT *
        FROM temp_events                                                                                                                                                             
        ORDER BY
            Reader, Date, Time";

I want each ORDER BY column to be on its own line, so I move the cursor to the space in between "Reader, " and "Date", and press rEnter to replace the space with a new line character. This is the result:

    $sql= "        
        SELECT *   
        FROM temp_events
        ORDER BY   
        Reader,
        Date, Time";

As you can see, instead of just making the new line match the indentation of the one above, it first changed the indentation of the line with "Reader" and then made the new line match this now incorrect indentation.

Here is what I was hoping would happen:

    $sql= "        
        SELECT *   
        FROM temp_events
        ORDER BY
            Reader,
            Date, Time";

Next, I would also move Time to its line.

5

I would start by checking if you have filetype indent ON, you can check with command:

:filetype

if you get filetype indent ON in the response, then it means vim has loaded scripts which define indent behaviour.
e.g. When I run :scriptnames from inside a mysql file,Vim tells me it has run these scripts: (amongst alot of other results, including the plugin and syntax files for SQL).

 /usr/share/vim/vim74/indent/sql.vim                                                                                                               
 /usr/share/vim/vim74/indent/sqlanywhere.vim 

sqlanywhere.vim is the default fallback file for sql indenting and is likely responsible for setting your SQL indent behaviour.
You could try unsetting the SQL indent script completely with:

:filetype indent off

(note: to apply the updated config you must reload the buffer afterwards with :e).

Otherwise, if you don't want to lose the entire config you could try looking in

/usr/share/vim/vim74/indent/sqlanywhere.vim

for example it sets indentexpr=GetSQLIndent()

from :h indentexpr

Expression which is evaluated to obtain the proper indent for a line.
It is used when a new line is created, for the = operator and
in Insert mode as specified with the 'indentkeys' option.

I would try :set indentexpr=""

I know this doesn't solve your problem exactly, but it might give you a start

  • filetype detection, plugin, and indent are all on. When I run :scriptnames, sql.vim is loaded, but sqlanywhere.vim is not, which seems significant. – still_dreaming_1 Nov 20 '15 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.