1

If I have a file a.js like this:

let a1 = [
];

and I press Enter at the end of the first line, a Tab is inserted. I do not want this, so I added a setting:

filetype indent off

but then if I have this:

let a1 = [
   10,
   20
];

I have to manually add tabs for each item. So I added another setting:

set autoindent

These two settings solve the existing problems, but introduce a new problem.

If I move the cursor to the start of one of the indented lines, and press Enter the indentation is removed. Can I change a setting to resolve this issue?

To respond to an answer, I can use these settings:

filetype indent off
set smartindent

and redo the second example. However, after I press Enter, if I then press Up or Down, the indentation is removed, repeating the problem.

2 Answers 2

2

I think the setting set smartindent has the behavior you want.

If you use the following .vimrc on a.js, then you can see what's happening when you create a new line and press <cr> in normal mode while the cursor is over the indentation section of the new line.

I'm using the setting set list to make ends of lines visible (as $) and tabs visible (as ^I).

set list
filetype plugin on
filetype indent off
set smartindent

With this setting on, no tab is inserted in the first example.

let a1 = [$
$
];$

In the second example, a line beginning with some indentation characters is moved in a reasonable way

let a1 = [$
^I10,$
$
^I20$
];$

If you want the leading tab to be preserved in the middle line of the second example, then you can use I or [ ^ and then i ] to move your cursor to the first non-whitespace character.

If you start with the buffer below as a.js

let a1 = [$
^Ia1$
^I20$
];

And then type the command 3GI<cr>, then this will be the result.

let a1 = [$
^Ia1$
^I$
^I20$
];$
3
  • 1
    What do you want to have happen in the second example? Can you be more specific? If you use set list to show the desired before and after state in the second example, it will be more clear to us how you want the indentation to behave. Feb 17, 2020 at 2:19
  • The leading tab will remain if you use I to start editing the line or use ^ to navigate to the first whitespace character and then use i. Feb 17, 2020 at 2:30
  • smartindent will screw with other filetypes heavily. I recommend only setting it for the relevant filetypes.
    – D. Ben Knoble
    Feb 17, 2020 at 14:09
0

After trying the methods in the question, I then tried this:

filetype indent off
function F_indent()
   return indent(prevnonblank(v:lnum - 1))
endfunction
set indentexpr=F_indent()

http://vimdoc.sourceforge.net/htmldoc/eval.html#prevnonblank()

which passes the 2 tests in the question, but I noticed another problem. With the second example, if I put the cursor at the beginning of the last line and Press Enter, it inserts a Newline and a Tab. In addition, throughout these tests I have also noticed that all the previous methods remove indentation on empty lines:

How do I prevent Vim from unindenting empty lines?

So Ideally I wanted whatever I came up with to pass these tests:

  1. whitespace removed only with backspace or delete
  2. if cursor position greater than indent, match indent
  3. if cursor position less or equal to indent, match position

To that end, I came up with this:

filetype indent off
function F_ind()
   let n_ind = indent(line('.'))
   let n_col = col('.') - 1
   if n_col > n_ind
      return "\n" . repeat("\t", n_ind / 3)
   else
      return "\n" . repeat("\t", n_col / 3)
   endif
endfunction
imap <expr> <CR> F_ind()

Note I use softtabstop=3, so adjust the / 3 as needed, or remove if you use hard tabs.

3
  • Take a look at formatoptions and maybe indentkeys and the related options there.
    – D. Ben Knoble
    Feb 18, 2020 at 13:38
  • @D.BenKnoble I dont see how either of those could replace the function I created
    – Zombo
    Feb 18, 2020 at 16:06
  • Not for replacing the function; maybe for understanding whats going on with why indent is being affected ans when.
    – D. Ben Knoble
    Feb 18, 2020 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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