5

Possible Duplicate:
Aliasing a command in vim

So I have to edit waf wscript files a lot . Everytime I execute this command to set the filetype

set filetype=python

is there a way to set up an small alias for the above command ? SO that I can just go in EX mode and type "py" which does the same thing.

4
  • I m looking for a way to make this permanent , I tried ":command cmd cmd_alias' but it fails Commented May 23, 2012 at 12:56
  • 1
    @VihaanVerma: familiar with vimrc? Look it up if you're not. Commented May 23, 2012 at 13:41
  • 1
    This is not a duplicate of the suggested question. It has some similarities but it is quite different, especially in the optimal solution of an autocmd, which is answering the intent rather than the question as worded. Commented May 23, 2012 at 14:29
  • The custom is to add the "#!/usr/bin/env python" shebang at the beginning of all wscript (you will find it on any waf example, with the following line indicating the file encoding), which has the nice effect of making smart editors recognize the file as a Python file. Commented Jun 3, 2012 at 4:34

2 Answers 2

7

You don't want go in Ex mode. What you want to go in is Command-line mode.

command! Py set filetype=python

Does exactly what you want: you type :Py<CR> to change the filetype to python.

You can also make it faster with a normal mode mapping:

nnoremap <F11> :set filetype=python<CR>
Sign up to request clarification or add additional context in comments.

3 Comments

command! Py :exec('set filetype=python')!? That should just be command! Py set filetype=python. In your command, the : and the parentheses are ignored; it's using the :execute command to achieve the same thing... it's not a function.
But this alias gets erased as soon as one exits the file. I wanna avoid F11 key so have put the command "command! Py set filetype=python" and it works! :D
You must put those lines (or the one you prefer) in your ~/.vimrc. That's how you make all your settings stick between sessions. I've used <F11> as an example. You can use any key or combination of keys but be smart with your choice.
6

If I have understood correctly your question, the following added to your .vimrc shoud work

autocmd BufRead,BufNewFile *.waf set filetype=python

If it is a particular filename like wscript, this works too:

autocmd BufRead,BufNewFile wscript set filetype=python

If you can't rely on an extension or filename you can add a modeline at the top or bottom of your file

# vim: set filetype=python :

See :help modeline for more information.

But it is kinda ugly because you have to modify the file, and if your are working in a team, it can be problematic.

7 Comments

I m guessing above command depends on file extension ".waf' .The wscript files in my case they dont have a ".waf" extension .
This also gets me wondering if vim stores a history of commands .
Just change the *.waf part in Xavier's answer to suit your needs.
@VihaanVerma : You can access command history by typing q:, then up/down arrow or j/q to select an item.
Note that it's not just *.waf which you can do. If it has a common filename like wscript, or a common path, you can do it here, or if there is some identifiable string in it you can do it with autocmd, too. This is a much better solution than having a command to do it manually. I think in your case you just want au BufRead,BufNewFile wscript setf python?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.