park-junha
park-junha

Reputation: 21

How to bind a key to custom tabstop in vimrc file?

I have the following in my .vimrc file:

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

I want to bind a key (for example, Ctrl-Tab) to change the above values from 4 to 2. What's the best way to do this?

Upvotes: 0

Views: 55

Answers (2)

Matt
Matt

Reputation: 15091

What's the best way to do this?

nnoremap <silent><C-Tab> :let &ts = (&ts == 4) ? 2 : 4<CR>

Still to be noted that mapping C-Tab only works under GUI.

Upvotes: 2

kosayoda
kosayoda

Reputation: 398

You can do it using a ternary conditional based on the current value of tabstop.

Here's an example binding it to Leader+t in normal mode:

nnoremap <leader>t :exec &tabstop == 2 ? "set tabstop=4" : "set tabstop=2"<CR>

Do note that you can't bind things to bind to Ctrl+Tab in the terminal, as Tab is already a control key. You can however, in gVim.

Upvotes: 2

Related Questions