I read lot posts, lso few here on SO but nohing usefull, what I want to do simply is to set $M to open vimrc while running vim (coding) so simply when I type :edit $M vimrc file is open and I can edit it and as I understand it correctly when I type :source $M vim writes changes and it returns to the actual file I was editing (coding). I run on linux fullpath to vimrc is : /etc/vimrc
If you don't like the (better) answer above, here's the answer to your actual question...
Vim uses environment variables freely and happily. If you run vim like this:
$ M=~/.vimrc vim
then you will be able to do the
:e $M
:source $M
that you were wanting to do.
If you do this either on the command line (for this session) or in your .bashrc:
export M=~/.vimrc
then that environment variable will be available when you run vim in other ways as well.
Incidentally, I'm not familiar with your expectation of how :source will work. I would expect that you would have to :w to save, then run your :source $M and then do :e # to get back to your previous file you were editing. Easier would be to open a new window or tab, probably, but all that is personal preference.
From http://learnvimscriptthehardway.stevelosh.com/chapters/07.html
:nnoremap <leader>ev :vsplit $MYVIMRC<cr>
:nnoremap <leader>sv :source $MYVIMRC<cr>
Then (assuming - is your leader) you can just type
-ev
to edit your .vimrc and just type
-sv
to source it.
(Obviously those mapping lines need to be put in your .vimrc themselves so that they are available moving forward.)
That's not quite the answer to your question, but hopefully even more direct that what you were hoping for...
(Check out http://learnvimscriptthehardway.stevelosh.com/chapters/06.html to see how to define if you haven't done that before.)
so I find out how to set leader let mapleader = ","
nmap <leader>v :tabedit $MYVIMRC<CR>
http://vimcasts.org/episodes/updating-your-vimrc-file-on-the-fly/
but problem is wtih let $MYVIMRC=/etc/vimrc what could be wrong on that?? I have my vimrc just there in /etc
In addition to Peter's answers, you might simply create a link:
$ ln -s /etc/vimrc ~/.vimrc
Related
I am trying to personalize my ~/.vimrc file.
Here is what I want: when the file name opened with vi is titi45.tex, and when I press <space> in normal mode, I want that the command make toto45
is executed. And if the file opened with vi is called titi65.tex, I want that
make toto65 is executed, and so on.
I tried to use a command
au FileType tex nmap <space> :w<CR>:!make<CR><CR>
in my .vimrc but I don't know how to match the file name and use the number.
Can you help me?
Mathieu
You are looking for :make %<. BTW why don't you compile within vim? Avoid :!make. Prefer :make, and check the help related to the quickfix mode (:h quickfix).
Your mapping would then be:
nnoremap <buffer> <silent> <space> :update<cr>:make %<<cr>
(the <buffer> part is very important, it makes sure your mapping won't leak to other filetypes; the other are not critical here, but good practices)
EDIT: Sorry I missed the exact requirement.
Then, you'll have to transform the target name. You'll to play with substitute() and :exe. But your substitution won't be a simple substitution. It looks like a rotating substitution. Solutions for this kind of substitution have been described over there: How do I substitute from a list of strings in VIM?
And IIIRC, there exist a plugin that does similar things.
In your case, I guess I would use a dictionary to define how names are substituted:
let k_subs = { 'toto': 'titi', 'titi': 'toto' }
nnoremap <buffer> <silent> <space> :update<cr>:exe 'make '.substitute(expand('%'), join(keys(k_subs), '\|'), '\=k_subs[submatch(0)]', '')cr>
NB: I haven't tested it.
If you want to get rid of the extension, it'd better be done in expand() argument.
Hum...
finally i use an additionnal script
#!/bin/bash
maRegex='source_(enonce|corrige)([0-9]+)$'
if [[ "${1}" =~ $maRegex ]]
then
commande="make enonce${BASH_REMATCH[2]}"
else
commande="make plouf"
fi
echo commande de compilation lancée: $commande
$commande
This script in launched by vimrc.
Here's my bind:
nnoremap <Leader>L :so $MYVIMRC<CR>:so ~/.vim/after/plugin/*.vim<CR>
It worked great right up until I added a second configuration file in the plugin folder. Now I get E77: too many file names from the :so[urce] command.
I found this which doesn't really make it obvious how to do it from an command string like in a keybind.
How do I write a loop in a keybind? Must a function be declared?
P.S. the reason I even have any scripts in .vim/after/plugin/ is because there are certain config commands for certain plugins that must be run after their initializations are run, and plugin load scripts run after vimrc. (so they cant just go in the vimrc).
:source takes only one argument but you can use the :runtime command:
runtime! after/plugin/*.vim
which is almost exactly the second example given under :help :runtime.
You can also chain the commands together, and it's not as nice as romainl's answer but you can break out into terminal and run source there.
nnoremap <Leader>L :so $MYVIMRC|:!source `find ~/.vim/after/plugin/ -name "*.vim"`<CR>
I have 2 .vimrc configuration files ~/.vimrc and ~/.vimsqlrc.
Is there a way I can source either of them (switch from one to another) while I have some files already opened?
As an extension, how do I turn off the loading of vimrc (i.e, don't use any vimrc) while I have files open?
Your ~/.vimrc is read and executed only once. If you want to nullify it with another file, you'll have to change the value of every single option and unmap every single mapping in, of course, both files. This sounds like a very bad and unnecessarily complex idea.
If you want another environment, just use another environment:
$ vim <-- starts Vim normally, reading ~/.vimrc
$ vim -u ~/.vimsqlrc <-- starts Vim using your alternative vimrc
$ vim -u NONE <-- starts Vim without any vimrc
$ vim -u NORC <-- starts Vim without any vimrc, but with plugins
but I'm afraid you'll have to stop and restart Vim for that.
Anyway, your question has a very strong XY problem smell. Do you want to have specific settings for *.sql files?
If that's your goal, you can put your settings in ~/.vim/after/ftplugin/sql.vim like this:
setlocal autoindent
nnoremap <buffer> <F6> :echo "F6"<CR>
Using setlocal for options and <buffer> for mappings ensures that your settings are only applied for *.sql files.
If I have a file with a shebang line (e.g. #!/bin/bash) open in Vim and the file has execute permissions (i.e. chmod +x) I know I can type this to execute it without leaving the editor:
:! %:p
: for command mode
! to run a shell command
% to refer to the file in the current buffer
:p to use the full path of the current file
Is there a shorter shortcut for this frequent task?
e.g. there is a ZZ shortcut for :wq, etc.
:!%:p
,without the spaces, is shorter.
If you want an even shorter shortcut, you can create a custom mapping:
nnoremap <F9> :!%:p
or the more "mnemonic":
nnoremap <leader>r :!%:p
If you haven't set permissions you can run:
:! sh %
None of the previous answers work if your filename/directory path has spaces in it. Simple fix.
:!"%:p"
After you've executed that once, a short :!! will repeat it.
When starting vi, specify file path explicitly, like this "vi ./blablabla"
vi ./yourscript.pl
Then start with !%
The other variant is to invoke the vi command like this
!./%
You can add a key mapping to your .vimrc
map <F5> :!%
I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins