0

For example, I want to create a command called Mycmd (the first letter need to be capitalized). Whey I'm in normal mode and type :Mycmd, I hope the following command series can be done:

:set ts=2
:set sw=2
:set sws=2
:set expandtab

I found command syntax seems to be on the right track, for example: [StackOverflow], but I couldn't do it with more than two commands (i.e., :set one thing then :set another thing).

I don't really consider nmap since I want to create my command instead of mapping to a hot key, though nmap allows to execute commands at the same time (e.g., nmap <F7> :set ts=2<enter>:set sw=2<enter>)

3
  • Just curious: Why do you need it, storing these settings in .vimrc file is not enough for you? Commented May 10, 2016 at 21:00
  • Good question, because I'm working on another project which use a separate indent style, not possible to separate based on file names, so that's why I was thinking to create a command so that I can switch to another indent settings. Commented May 10, 2016 at 21:02
  • P.S., it seems one solution is that I make a macro and wire the new command to this macro, but still looking for a neater solution. Commented May 10, 2016 at 21:03

2 Answers 2

3

Just do:

command Mycmd set ts=2 | set sw=2 | set sws=2 | set expandtab

| is used to separate commands inside any Vim script.

Or even simpler:

command Mycmd set ts=2 sw=2 sws=2 expandtab

To change the settings, then show the changes:

command Mycmd set ts=2 sw=2 sws=2 expandtab ts? sw? sws? et?

To map it:

nnoremap <silent> _I :Mycmd<cr>
Sign up to request clarification or add additional context in comments.

Comments

2

What you want is:

command! Mycmd setlocal ts=2 sw=2 sws=2 expandtab

What you (and everyone) need is Editorconfig.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.