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.

Community
  • 1
  • 1
Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
  • I m looking for a way to make this permanent , I tried ":command cmd cmd_alias' but it fails – Vihaan Verma May 23 '12 at 12:56
  • 1
    @VihaanVerma: familiar with vimrc? Look it up if you're not. – Chris Morgan May 23 '12 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. – Chris Morgan May 23 '12 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. – cJ Zougloub Jun 03 '12 at 04:34

2 Answers2

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>
Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    `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. – Chris Morgan May 23 '12 at 13:13
  • 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 – Vihaan Verma May 23 '12 at 13:26
  • 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 `` as an example. You can use any key or combination of keys but be smart with your choice. – romainl May 23 '12 at 14:30
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.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Xavier T.
  • 40,509
  • 10
  • 68
  • 97
  • I m guessing above command depends on file extension ".waf' .The wscript files in my case they dont have a ".waf" extension . – Vihaan Verma May 23 '12 at 13:01
  • This also gets me wondering if vim stores a history of commands . – Vihaan Verma May 23 '12 at 13:04
  • Just change the `*.waf` part in Xavier's answer to suit your needs. – romainl May 23 '12 at 13:07
  • 2
    @VihaanVerma : You can access command history by typing `q:`, then up/down arrow or j/q to select an item. – Xavier T. May 23 '12 at 13:11
  • @XavierT. Awesome :D , thanks it allows searching too. – Vihaan Verma May 23 '12 at 13:13
  • 1
    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`? – Chris Morgan May 23 '12 at 13:36
  • yeah it works even better and exactly as I wanted :) – Vihaan Verma May 23 '12 at 18:06