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>
Related
As part of learning Haskell, for fun I'm attempting to use Raspberry PI. Having encountered a myriad of issues installing ghci on the PI I've resolved to using just ghc.
So to create, compile & run a new Haskell file :
vi first.hs
i
main = putStrLn "First"
Esc
:w
:q
ghc -o first first.hs
./first
Output is : "First"
I would like to automate the commands :
Esc
:w
:q
ghc -o first first.hs
./first
Can these be added as new command from within vi / vim, something like :
:mycustomcommands
And run from within the vi / vim editor ?
Maybe you could try adding something like this to your vimrc:
function! ExecuteHS()
w
!ghc -o first %
!./first
endfunction
And to use this function you just have to call it like that :call ExecuteHS(). Vim will be put on background during the execution of your file and will then come back on foreground at the end of the execution.
As a bonus you can add the folowing line to your vimrc
nnoremap <key> :call ExecuteHS()<CR>
Replacing <key> with your prefered key combination <Leader>e for example. This way you'll simply have to hit ,e (if you didn't changed your leader key) in normal mode to call the function.
That's probably not the cleanest way to do it but it should work for what you want.
Absolutely in vim, though not necessarily in other vi flavors. See this tutorial on defining custom commands. Put the custom command in your vimrc and it will always be available as :Customcmd or whatever you call it. For one-button access, you can use :remap to assign a hotkey to your custom command or the sequence of built-in commands you want to run. This is a tutorial on keymappings that will give you more information.
I second #statox's referral to https://vi.stackexchange.com :)
I use vim-haskell, which includes a couple nice things. In particular, it includes a file for setting up cabal-install as the compiler, which is a very nice way of working. Dump this in ~/.vim/compiler/cabal-build.vim:
CompilerSet makeprg=cabal\ build
CompilerSet errorformat=
\%W%f:%l:%c:\ Warning:%m,
\%W%f:%l:%c:\ Warning:,
\%E%f:%l:%c:%m,
\%E%f:%l:%c:,
\%C\ \ %#%m,
\%-G%.%#,
\%-G%.%#
And this in ~/.vim/ftplugin/haskell.vim:
compiler cabal-build
(The argument to compiler should match the name of the file you put in ~/.vim/compiler.) Then you can run :make in vim and it will save any changed buffers (assuming autowrite is set) and build your project. When there are errors, it will populate the quick-fix list, which lets you jump to the specific file and line numbers of each error or warning with a key. Read more about this feature with :help quickfix. Once everything is working, you can :!cabal run to run it.
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
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> :!%
Here I got simple task for skilled vimmers. I need to reformat my css file. There are commands to do this:
%s/}/&\r/g
%s/ / /g
retab!
echo "You done did it!"
But I don't want to type these commands every time I need to format my css file (I get it after convert less file by WinLess program). Now I put these commands into cssformat.vim file, and put this file into vim runtime folder. In my vimrc I set:
autocmd Filetype css nmap :so $VIM/vim73/cssformat.vim
It's works, of course. But I wonder how can I do this task better? In the begginig I want to put these commands in my vimrc (to create a simple function), but I don't know how to do this correctly.
p.s. Sorry for my bad English.
Just put the commands from your script into a function:
function! ReformatCss()
" Place your commands here.
endfunction
And move the stuff into your .vimrc. Now you can invoke this via :call ReformatCss().
To top it off and make it even simpler, define your own command:
command! ReformatCss call ReformatCss()
Now you can invoke via :ReformatCss. Voila!
You can learn more at :help usr_40.txt and :help :command. For example, if you only need this for CSS files, you can turn this into a buffer-local command through command -buffer and moving the function and command definition to ~/.vim/ftplugin/css_reformat.vim
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