3

I'm trying to use indent size as variable, but it doesn't work.

let indent=4
set tabstop=&indent
set softtabstop=&indent
set shiftwidth=&indent
set expandtab

I tried both &indent and %(&indent) - just guessing, but it still broken.

8
  • @jamessan Hmm. No, it doesn't look as duplicate (or similar) from my point of view.
    – james
    Feb 2, 2019 at 0:17
  • They're both asking how to use variables for set commands. Although you're trying to modify a different option, the mechanism is the same.
    – jamessan
    Feb 2, 2019 at 0:24
  • James, welcome to Vi and Vim! Consider taking the tour to get acclimated with the site. As things currently stand, I agree with @jamessan that your question is a duplicate of the other. Could you possible edit and clarify how it is different, or doesn't solve your problem?
    – D. Ben Knoble
    Feb 2, 2019 at 0:41
  • They may both be asking how to use variables for set commands, but I can't get it to work following those instructions either. Specifically, every attempt returns E521: Number required after =: tabstop=$indent, which appears to not be a check that would be done for options not requiring a number.
    – Ed Grimm
    Feb 2, 2019 at 5:03
  • 1
    :let &tabstop=indent $ is used for environment variables. & is for settings. You don't need either for vimscript variables.
    – B Layer
    Feb 2, 2019 at 5:14

2 Answers 2

5

Most is already in the comments, but to summarize:

let indent=4
let &tabstop=indent
let &softtabstop=indent
let &shiftwidth=indent
set expandtab

If you want to set a setting (like tabstop) to the value of a variable, you have to Vim script expressions instead of set-expressions. In that case, settings are given with a leading &. indent is a Vim script variable so NO & is needed.

See it as namespaces. The leading & tells that the following name is from the namespace "Vim settings". Like a leading $ tells, that the following name is from the namespace "environment variables".

So the following two lines leads to identical results.

set tabstop=4
let &tabstop=4

You could also do:

set tabstop=4
let &softtabstop=&tabstop
let &shiftwidth=&tabstop
set expandtab

Another way to achieve the same (also NOT recommended in this scenario) is:

let indent=4
execute "set tabstop=" . indent
execute "set softtabstop=" . indent
execute "set shiftwidth=" . indent
set expandtab
1
  • how can i bind a command like "let indent=4" with some keys, basically i want to bind the command "let &lines=line("$")+4" to some binding. Jan 10, 2022 at 10:12
3

If you want to have all tabstop related settings follow each other, it is best to use:

set tabstop=4      " set to specific value here, that you want to use
set shiftwidth=0   " follow tabstop setting
set softtabstop=-1 " follow shiftwidth setting

That will also work for the vartabfeature (since patch 8.1.542).

1
  • Upvoted, but it was required to choose only one answer to accept, and I choose the one which was more detailed.
    – james
    Feb 2, 2019 at 9:58

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