Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Say, I want to run "echo hello" when I press <leader>T

How can I write the script?

Is there any way to do it nicely?

I'm thinking about creating a custom command.

share|improve this question

3 Answers 3

Are you thinking of something like this?

:!echo hello

and reading in the contents of a shell command (in this case the ls commnand):

:r !ls
share|improve this answer

You can do that with this command:

nnoremap <leader>T :!echo hello<cr>

Or, if you don't care about the output:

nnoremap <leader>T :silent !echo hello<cr>

You can put this in your .vimrc so that the mapping is set on startup.

I have some commands that I want to run on the current file. If you want to save any changes first, and then pass the file name as an argument, you can do it like this:

nnoremap <leader>T :update<cr>:!cat '%:p'<cr>

where '%:p' expands to the full path to the file.

share|improve this answer

You can define a command using 'command'. e.g. add this to your .vimrc:

" define command Hello:
command! Hello echo 'hello world'

Then you can run this command in command mode:

:Hello

To see more complicated examples, just run :verbose command COMMAND_NAME to find its definition. e.g. run:

:verbose command NERDTree

It tells me NERDTree command is defined in .../nerdtree/plugin/NERD_tree.vim, then you can find how NERDTree command is defined.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.