开发者

How do I use variables in my .vimrc?

开发者 https://www.devze.com 2022-12-20 07:01 出处:网络
I have a small problem with \"tab size\" and different project, some like 2 or 4 and the Linux kernel like 8 spaces per tab.

I have a small problem with "tab size" and different project, some like 2 or 4 and the Linux kernel like 8 spaces per tab.

And this is not a big problem since I can just change a couple of settings in my .vimrc

set tabstop=4
set shiftwidth=4
set softtabstop=4

But that 开发者_C百科is 3 lines I need to change...

It would be nice to have one line with a variable with the number 2,4 or 8.

A little bit like

let l:tabsize=4
set tabstop=l:tabsize
set shiftwidth=l:tabsize
set softtabstop=l:tabsize

But this don't work...

Do you know how to fix this?

Thanks Johan


Update: This solves my little problem.

let tabsize = 4
execute "set tabstop=".tabsize
execute "set shiftwidth=".tabsize
execute "set softtabstop=".tabsize


you can't use variables on the rhs in the .vimrc.

try :help feature-list for more info. for unix vs windows for example (not sure what your projects are):

if has("unix")
    " do stuff for Unix
elseif has("win32")
    " do stuff for Windows
endif

might work, or another example is

execute "set path=".g:desktop_path

If g:desktop_path contains spaces, you will have to escape those, either in the original setting of g:desktop_path or when setting 'path', e.g.,

execute "set path=".escape(g:desktop_path, ' ')

See

:help let-option
:help execute
:help escape()


This is working:

let my_sw = 20
let &sw = my_sw

Now you can figure how to fix your code


As this topic was brought to life again here are my few bits:

" In the vimrc
set softtabstop=-1 " Make 'softtabstop' follow 'shiftwidth'
set shiftwidth=0   " Make 'shiftwidth' follow 'tabstop'

" Somewhere else
let &tabstop=l:tabsize " Assign 'tabstop' a value of local tabsize variable
" or, typed manually
set ts=4

. And please forget about execute 'set option='.var. let &option=var is available since at least vim-7.0.


This solution doesn't use local variables, but it will get you the result you want using just your .vimrc file. Just add the code below to your .vimrc file and add more project-specific options (even mappings) to the corresponding functions below. (Remember to change the globbing paths in the autocmd! lines to the appropriate folder name.)

autocmd! BufReadPost,BufNewFile */myProject/** call <SID>MyProjectOptions()
autocmd! BufReadPost,BufNewFile */linux-kernel/** call <SID>LinuxKernelOptions()

function! <SID>MyProjectOptions()
    " everything in this function only applies to myProject files
    setlocal tabstop=4
    ...
endfunction

function! <SID>LinuxKernelOptions()
    " everything in this function only applies to linux kernel files
    setlocal tabstop=8
    ...
endfunction


Here's a one-liner that toggles colorcolumn on/off when you hit leader+c:

nnoremap <Leader>c :execute "set colorcolumn=" . (&cc == "+1" ? "0" : "+1")<CR>


I've been troubled by this problem for a very long time, too. This is especially painful when I need to work with other people's code concurrently, which forces me to change the indent size back and forth frequently. I used to have something similar to OP's solution, but that still requires updating the .vimrc file every time. Later, I learned I can just define this function that I could call from the editor:

function! SetTabSize(size)
    execute "set tabstop=".a:size
    execute "set shiftwidth=".a:size
    execute "set softtabstop=".a:size
endfunction
command! -nargs=1 Sts call SetTabSize(<f-args>)

In the editor, if you want to change the current indent size to 4, just do:

:Sts 4

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号