Quantcast

Command aliases?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Command aliases?

Duane Johnson
I'm a total newbie to Vim.  I'd like to map a new command-line command (":vimrc") to open up my vimrc file (":e ~/.vimrc").

I tried something with cmap in my vimrc but caused infinite recursion by mistake (I have no idea if cmap is the right command to try):

cmap vimrc e ~/.vimrc

I also found this plugin [1] but I'd first assume that what I'm trying to do is fairly basic, and probably does not require a plugin.

Any help would be appreciated.

Thanks,
Duane Johnson

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Command aliases?

John Beckett-2
Duane Johnson wrote:
> cmap vimrc e ~/.vimrc

I just use ':e $MYVIMRC' with command completion:
http://vim.wikia.com/wiki/Open_vimrc_file

You need 'cnoremap' rather than 'cmap' (and $MYVIMRC). However,
the mapping applies wherever you might type 'vimrc' in the
command line and I generally find that irritating.

You could just have a simple mapping:

:nnoremap <F8> :e $MYVIMRC<CR>

so you press F8 in normal mode.

For an actual command alias, you need to muck about, for example
(although I would not bother):
http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev

John

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

Gary Johnson-4
In reply to this post by Duane Johnson
On 2010-04-12, Duane Johnson wrote:

> I'm a total newbie to Vim.  I'd like to map a new command-line command
> (":vimrc") to open up my vimrc file (":e ~/.vimrc").
>
> I tried something with cmap in my vimrc but caused infinite recursion by
> mistake (I have no idea if cmap is the right command to try):
>
>
>     cmap vimrc e ~/.vimrc
>
>
> I also found this plugin [1] but I'd first assume that what I'm trying to do is
> fairly basic, and probably does not require a plugin.
>
> Any help would be appreciated.

The conventional way to define your own commands is to use :command.
See

    :help 40.2

Your command would be defined like this:

    :command Vimrc e ~/.vimrc

User-defined commands must start with an upper-case letter.  There
are ways around this limitation, but since you're a newbie, I think
it's best to stick with the basics for now.

The reason your cmap caused an infinite recursion is because the lhs
of the mapping was part of the rhs.  When Vim discovered the "vimrc"
in the rhs it knew it had a mapping with that lhs and executed it.
To avoid that problem, use cnoremap instead of cmap.  See

    :help 40.1

and search for "^REMAPPING".

Regards,
Gary

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

Matt Wozniski-2
In reply to this post by Duane Johnson
On Mon, Apr 12, 2010 at 6:23 PM, Duane Johnson wrote:
> I'm a total newbie to Vim.  I'd like to map a new command-line command
> (":vimrc") to open up my vimrc file (":e ~/.vimrc").

As an addition to the other answers you've gotten - is this really
worth spending time optimizing?  Especially when all you want to safe
yourself is 5 keystrokes (e space tilde slash dot)?  I'd have to
imagine you could have typed e ~/. at least 100 times in the time it
took you to write your email.  So, this thread might be useful as a
learning exercise about mappings and user commands, or just for the
fun of playing with a programmable text editor, but I doubt there's
any real-world utility to it.

~Matt

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

scott-268
In reply to this post by Duane Johnson
On Monday 12 April 2010 17:23:02 Duane Johnson wrote:

> I'm a total newbie to Vim.  I'd like to map a new command-line
>  command (":vimrc") to open up my vimrc file (":e ~/.vimrc").

you are correct in thinking you don't really need a plugin for
this, unless of course you are interested in it mainly as a
learning exercise

here's an alternative method for editing your vimrc:  put

nnoremap <Leader>e :call EditTry("~/.vimrc")<CR>

function! EditTry(what)
    if &modified
        execute "split " a:what
    else
        execute "edit " a:what
    endif
endfunction

in your .vimrc -- if you have not otherwise defined <Leader>,
you can subsequently use

    \e

in normal mode to edit your vimrc in a split window if you
have modified your current buffer, or in the same window if
your changes are saved

see

    :h <Leader>

for more info on how the Leader commands work

one of the fun things about vim is there are usually dozens of
ways of performing the same task -- you get to pick the one
that makes the most sense to you

--
sc

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

Ralf-32
In reply to this post by Duane Johnson
Open your .vimrc, press mV (uppercase!), close it. The next time press
'V to open .vimrc.

see :h mark

hth

Ralf

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

Ben Fritz


On Apr 13, 12:37 am, Ralf <[hidden email]> wrote:
> Open your .vimrc, press mV (uppercase!), close it. The next time press
> 'V to open .vimrc.
>
> see :h mark
>

Oh, hey...I never thought of using uppercase marks as a sort of file
bookmark/shortcut for commonly accessed files! That's pretty clever.

The true beauty of this solution is that the marks are (by default)
saved in your .viminfo file, so that every time you launch Vim you
will automatically load any of these shortcuts.

See :help viminfo and :help 'viminfo' for details.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

Duane Johnson
In reply to this post by Ralf-32
Wow, that's great!  Thanks, and to others who have responded (Matt, Gary, John, sc).

I'm already starting to appreciate Vim.

Regards,
Duane

On Apr 13, 2010, at 12:37 AM, Ralf wrote:

Open your .vimrc, press mV (uppercase!), close it. The next time press
'V to open .vimrc.

see :h mark

hth

Ralf

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Command aliases?

stosss
On Tuesday, April 13, 2010, Duane Johnson <[hidden email]> wrote:
> Wow, that's great!  Thanks, and to others who have responded (Matt, Gary, John, sc).
> I'm already starting to appreciate Vim.
> Regards,Duane
> On Apr 13, 2010, at 12:37 AM, Ralf wrote:
> Open your .vimrc, press mV (uppercase!), close it. The next time press
> 'V to open .vimrc.
>
> see :h mark
The V tip was cool.

--
If we can but prevent the government from wasting the labours of the
people, under the pretence of taking care of them, they must become
happy. - Thomas Jefferson

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Loading...