I'm frequently running the following bash command from within vim:
! cordova emulate
What are my options for reducing the number of keystrokes? Please describe how to implement as well.
Note that I'd prefer NOT to create a bash alias for this command.
Bash aliases don't work in Vim by default anyway.
You could add this mapping in ~/.vimrc:
nnoremap <F5> :!cordova emulate<CR>
Related
I am using vimwiki, a plugin for Vim, on a daily basis; and I am wondering if I can trigger its <Plug> mapping "<Plug>VimwikiMakeDiaryNote" through terminal/command-line arguments? I have found the particular command is defined as follows:
command! -count=1 VimwikiMakeDiaryNote
\ call vimwiki#diary#make_note(v:count1)
The <Plug> mapping is defined as:
if !hasmapto('<Plug>VimwikiMakeDiaryNote')
exe 'nmap <silent><unique> '.g:vimwiki_map_prefix.'<Leader>w <Plug>VimwikiMakeDiaryNote'
endif
nnoremap <unique><script> <Plug>VimwikiMakeDiaryNote :VimwikiMakeDiaryNote<CR>
Is there a way to trigger the command/mapping VimwikiMakeDiaryNote through terminal/command-line, by passing something as arguments for Vim?
Thanks a lot!
All the best,
-Linfeng
As the <Plug>-mapping simply invokes the eponymous command, it's easiest to invoke the command directly. You can execute arbitrary Ex commands with the -c {command} or +{command} option:
$ vim +VimwikiMakeDiaryNote
Adoption on Windows & Linux OS
On Windows: use AutoHotKey
Press Win+i shall either start Gvim, or loop through all existing instances of Gvim.
#i::
IfWinExist ahk_class Vim
groupactivate, VIM, r
else
run C:\Vim\vim80\gvim.exe, +VimwikiMakeDiaryNote, max
return
+#i:: run C:\Vim\vim80\gvim.exe +VimwikiMakeDiaryNote, , max
On Linux
Added the following entry to ~/.bashrc
alias v='vim +VimwikiMakeDiaryNote'
Perhaps, the easiest way is to test an environment variable, e.g.:
if !empty($VIM_WIKI)
" Your mappings...
endif
The mappings will be applied, if VIM_WIKI environment variable is not empty:
VIM_WIKI=1 vim file
I have a custom .vimrc file which I use across many different machines.
Some machines are less powerful than others, and it would be nice to be able to load a "stripped-down" version of the .vimrc file. However, I'd like to maintain a single .vimrc to avoid fragmentation.
Ideally, I'd like to pass arguments to my .vimrc from the command line. If the "minimal" option is selected, the .vimrc would skip over loading the most resource-intensive plugins.
Does anyone know the best/cleanest way to do this?
Thanks!
Edit: The slower machine I'm talking about is a Raspberry Pi over SSH. Vim itself isn't slow, although I have several plugins including NERDTree and Syntastic that take lots of time to load on the Pi's limited CPU. By taking out most plugins for my "minimal" configuration, I cut down the loading time from 3.5 seconds to 0.5 seconds.
You can use the --cmd switch to execute a command before any VIMRC is loaded.
So on your less powerful machines you could alias vim to something like vim --cmd 'let weak=1' and then in your $VIMRC you can say:
if exists('weak')
echo "poor machine"
endif
take a look at source:
source /foo/bar/another_vimrc
Your 'heavy' vimrc can just source the basic vimrc and add what you want. This is also really handy for project/machine specific abbreviations, ctags, etc.
This will not keep a single vimrc file, but for the sake of others who have the same question (as stated at the top of the page):
$ vim -u ~/.vim-min.vim
Note that this will suppress loading both the system vimrc file (if any) and your personal vimrc file.
:help -u
:help startup
(See Step 3 of the second reference.)
From the excellent answers above, I got this working:
~/.vimrc :
if exists('FLAG') " 'FLAG' passed from ~/.bashrc 'vimm' alias
set textwidth=150 " (launches vim in expanded terminal window)
set lines=58
else
set textwidth=79
set lines=40
endif
~/.bashrc :
# Needed to combine following two lines for ~/.vimrc use:
# alias vimm='konsole --geometry 1900x1040+0+0 -e "bash -c \"vim\""; exit'
# vim --cmd 'let FLAG=1'
str="'let FLAG=1'"
alias vimm='konsole --geometry 1900x1040+0+0 -e "bash -c \"vim --cmd $str\""; exit'
Now, 'vim' (normal use) launches Vim in a normal-sized terminal, whereas 'vimm' launches Vim in a larger terminal (alternate settings).
'Konsole' is the terminal that I use in Arch Linux.
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 am writing some map commands that run external commands. For example, I may have the following map command to compile the working project.
nnoremap <F5> :!mvn compile test<CR>
However, when vim switches to a shell, it's not clear what command is running. Is there a way for the command to show up on the shell, short of echoing it? It seems tedious to need to write the following each time, but it would do what I want.
nnoremap <F5> :!echo "mvn compile test"<CR>:!mvn compile test<CR>
If your external command processor is a UNIX style shell, it has an echo feature, and you need only to pass the x option to it:
map <F5> :!sh -xc 'mvn compile test'<CR>
I can use :shell to enter current dir and run commands. bash_completion work fine.
But how get bash_completion working in :! command or :r ! command case?
Note. I am Emacs user and Emacs M-! command don't allow such things (also M-x shell, you need M-x term to get bash_completion).
It is not possible. What #Prince Goulash is referring to is a simple filename completion done by vim itself. If you know how to force bash to pipe completion variants somewhere you can create a completion function for custom command which may in turn call built-in :!, but redefining completion for built-in commands is not possible.
I personally do not know how to do this with bash and know that trying to do so in zsh results in dirty hacks with redefining zsh built-in commands (like compadd), using pexpect (python), Expect (perl), expect (tcl), zsh/zpty or something similar to run shell in interactive mode and inventing zsh->vim IPC mechanism (in opposite direction it is much simpler) (I once started working on a similar module for zsh, but stopped as there were too much problems on this way).
Have you got
set nocompatible
in your .vimrc? This automatically enables ex-mode completion for :!. To see the list of possible completions you could also use
set wildmenu
and investigate the wildmode options.