How to create custom vim commands for existing commands? - vim

I'm trying to put the following in .vimrc
:command Clip set mouse=v set nonumber set norelativenumber
To prepare for copying into (mouse)clipboard.
Error:
Unknown option set
Is there an alternative way to alias a list of vim commands?

Found the solution:
You should specify set just once.
:command Clip set mouse=v nonumber norelativenumber

Related

Set working in file but not in .vimrc

When I put set scroll=10 in my .vimrc it does not work, but it does work when I enter the command in command mode.
I have many other commands in my .vimrc, what reason could there be for this set to not be getting through? It is the last line in my .vimrc.
:verbose set scroll?
should tell you which plugin script has last modified that option. If you don't find the culprit this way, you could workaround by setting it at the last possible moment by putting this in your ~/.vimrc
:autocmd VimEnter * set scroll=10

How to apply VIMRC to my python file?

I'm using Ubuntu13.10 My $HOME is /home/mario.
I execute vim in the terminal and used :e $MYVIMRC to set up my own preference.
I've test it inside that VIMRC file by typing :so $MYVIMRC, it works!
But later when I changed my directory to /home/mario/LPTH and use vim ex45.py to open my python exercises.
Then I found I can't apply it to my python file by typing :so $MYVIMRC or :source $MYVIMRC
I even restart my computer, still can't work.
What happend? How can I apply the VIMRC to my python docuemnt?
Thanks for your help.
---------------------------addtional information part 1---------------------------------
Yesterday when I raised up this question, I've already set several configurations. such as:
set number
set expandtab
set tabstop=8
set shiftwidth=4
set softtabstop=4
set autoindent
:syntax on
I can apply my configurations for this MYVIMRC file. which shows code numbers,for example. But when I open my python files in ~/LPTH. nothing happens. I checked this morning, still the same.
--------------------addtional information part2--------------------------
Obstacle hacked!!!!!
Previously: I followed tutor from http://vim.wikia.com/wiki/Vimrc,my execution is:
cd ~ ---> vim ---> :e $MYVIMRC ---> add my setting.
And it caused the problem.
This morning: I did the following executions.
cd ~ ---> vim .vimrc ---> add my setting.
And it works. Just that simple.
Thank you guys for your help
You don't (need to) apply your .vimrc to a file. The configuration and customization commands that you put in there are automatically executed by Vim on startup. You should only put general stuff like :set listchars=... backupdir=... in there, and any general :mappings.
Some settings are specific to a programming language like Python; Vim has buffer-local options (like :setlocal expandtab) and mappings; these can be set by filetype plugins. Some people define those via :autocmd FileType python setlocal ... directly in their ~/.vimrc, but it's better to just configure :filetype plugin on there and then place the specific options in ~/.vim/after/ftplugin/python.vim, for example.
As pointed out already you dont need to apply your .vimrc to a file. You just need to have your .vimrc file in your $HOME directory, it will automatically be executed by vim.
This is the .vimrc i would reccomend for Python
set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smartindent
set smarttab
set retab
set autowrite
set autochdir
filetype indent on
filetype on
filetype plugin on

Trying to set makeprg in vim for yuml filetypes, works manually, not working in vimrc

I'm trying to get the following entry working in my .vimrc:
autocmd FileType yuml set makeprg=yuml\ -i\ %\ -o\ %<\.pdf\ -f\ pdf\ &\ sleep\ 1\;\ evince\ %<.pdf\ &
It doesn't work and whenever I type :make, it uses some default options from the yuml utility.
However when I type:
set makeprg=yuml\ -i\ %\ -o\ %<\.pdf\ -f\ pdf\ &\ sleep\ 1\;\ evince\ %<.pdf\ &
manually in vim, the command does work.
What am I doing wrong here?
Thanks,
Martin
Edit: I forgot to explicitly define the yuml filetype. So what I needed to do was add the following line to the .vimrc file:
au BufRead,BufNewFile *.yuml set filetype=yuml
I don't know what exactly is wrong, but in a scripted autocmd, I would skip the escaping issue and use :let instead:
:autocmd FileType yuml let &makeprg = 'yuml -i % -o %<.pdf -f pdf & sleep 1; evince %<.pdf &'
Also, check that the filetype is correctly set with :set ft?

How can I prevent swap/backup files in VIM for one specific file?

I've got this password file, and when editing this in vim I want to make sure vim does not create a swap/backup file. How to set this?
I have seen this:
How to prevent vim from creating (and leaving) temporary files?
But I do not want this for all files, just for a specific one. Is this possible?
If your file can contain comments you can set the nobackup and noswapfile by adding a modeline to the start or end of the file.
Something like this:
# vim: set nobackup noswapfile:
You could use autocmd to set nobackup for that filename or filenames matching a specific pattern/extension:
autocmd BufRead,BufNewFile passwordfilename set nobackup
autocmd BufRead,BufNewFile passwordfilename set noswapfile

Set vim colorscheme in modeline

I'm trying to set my colorscheme in the modeline. I tried it a few ways, but none of them works. I get an "Unknown option: colorscheme" error.
# vim: colorscheme dark_foo:set ft=foo:
How can it be set in the modeline?
The modeline consists of option settings. There is no "colorscheme" option. colorscheme is just a command that essentially executes "runtime colors/foo".
You can get an approximation of what you're asking for by creating an autocmd that sets your colorscheme and is triggered by some option that is actually settable in your modeline.
detailing answer of Laurence, example: force js highlighting:
first line of file you want to get the intended highlighting: # vim: ft=javascript
If you want you can then deviate from the default colorscheme via .vimrc:
autocmd FileType javascript colorscheme badwolf

Resources