vim autocommand event for write all buffers :wa - vim

I'm currently using the following mapping to, essentially copy over any files written in my dev env to my local server via executing a script. It works fine for individual files. However, I have a habit of doing :wa to save all buffers open:
au BufWritePost /path/to/dev/* silent !$HOME/bin/somescript.sh %:p
Any suggestions for how I could rewrite this to be a conditional like:
if one file
exec script to copy just that file # like I already have
if :wa
# here I'd probably exec a script to just copy recursively
EDIT
Possible solution per ZyX's solution:
au BufWritePost /Users/rlevin/programming/sugar/Mango/sidecar/* silent !$HOME/bin/sugarbuild.sh %:p
" If we do :wa<CR> we check if command type is ':' and if command itself was
" 'wa'. If so, we call the command WA which calls BuildSidecarIfInProject.
" This checks if we're actually within the project's directory
cnoreabbrev <expr> wa ((getcmdtype() is# ':' && getcmdline() is# 'wa')?('WA'):('wa'))
command! WA :call BuildSidecarIfInProject()
function! BuildSidecarIfInProject()
if fnamemodify('.', ':p')[:44] is# '/Users/rlevin/programming/sugar/Mango/sidecar'
exec ":!$HOME/bin/toffeebuild.sh"
endif
endfunction

There is no way to determine number of files saved, but you can remap/abbreviate wa:
command WA # command that executes a script to just copy recursively
cnoreabbrev <expr> wa ((getcmdtype() is# ':' && getcmdline() is# 'wa')?('WA'):('wa'))

Some smart guy once said, "Premature optimization is the root of all evil." If you really need on-the-fly backup/deployment to your server, why don't you just run the recursive version every time, or possibly bound to a hotkey? I.e. don't treat the single-file case specially. For example, rsync is pretty good at avoiding unnecessary copying.

Related

How to execute a command from cnoremap?

I want to map <M-o> to quickly look up buflist from command-mode.
Here's an example normal-mode mapping:
nnoremap <M-o> :ls<CR>
Here's an attempt to do the same for command mode using execute():
cnoremap <M-o> <C-r>=execute(':ls')<CR>
This however simply appends whitespace-separated :ls results to the command line, with no side effect of the :ls table actually showing.
Is there a way to execute a command without leaving command-mode?
Update:
Closest I've come to cracking this using Vimscript is the following:
function! RunCmdFromCmdline(cmd)
let l:cmd_line = getcmdline()
echo execute(a:cmd)
return l:cmd_line
endfunction
cnoremap <M-o> <C-\>eRunCmdFromCmdline(':ls')<CR>
The echo makes it so the results of a:cmd do pop on the screen, however in invisible font. I tried playing with :redraw <bar> in different combinations with no success.
Exiting from the command mode from within a <C-r>= or <C-\>e prompt does not seem possible.
You can save the contents of the current command line in a register, then run ls and paste the contents of the register:
cnoremap <esc>o <home>let #x="<end>"<cr>:ls<cr>:<c-r>x
So if command line looks like this:
:echo 'some'
type <M-o> to get:
:ls
1 %a + "filename" line 1
:echo 'some'
This works. It uses global variables, but arguably this is an acceptable use for them, since there's only a single command-line you can be in at a time.
function! SaveCmdline()
let g:save_cmdline = getcmdline()
let g:save_cmdpos = getcmdpos()
return ''
endfunction
function! RestoreCmdline()
call setcmdpos(g:save_cmdpos)
return g:save_cmdline
endfunction
cnoremap <M-o> <C-\>eSaveCmdline()<CR>ls<CR>:<C-\>eRestoreCmdline()<CR>
This works with whatever quotes or unfinished syntax you have in your command-line, restores the full command-line and the cursor to the position it was before running the command, doesn't mess with any registers (only touches the two global variables) and doesn't mess with command-line history either (see q: command for history.)
It does have one tiny bug, in that cnoremap actually also works when you're typing a search expression (after / or ?) among a few other sub-modes (see getcmdtype() for a list of those.) If you use the mapping there, it will search for "ls" and then restore your search term as an Ex command, oops...
You can probably use getcmdtype() to detect that and possibly only perform the actions while you're typing an Ex command and not in the other modes.
UPDATE: This fixes the last part, only executing the command while in the Ex command-line, not in search or other context. It also makes the interjected command parameterizable.
function! CmdlineExecute(cmd)
if getcmdtype() != ':'
return ''
endif
return "\<C-\>eSaveCmdline()\r".a:cmd.
\ "\r:\<C-\>eRestoreCmdline()\r"
endfunction
cnoremap <expr> <M-o> CmdlineExecute('ls')
(Note this still needs the definition of the two functions above.)

Taglist does not get updated dynamically

I am a beginner with VIMscript. While coding, I need to update the tags and cscope database so I can jump and search newly added code (functions, macros etc.)
My .vimrc file has following code:
function UpdateTags()
silent! execute (":!rm -rf tags cscope.files cscope.out")
silent! execute (":!ctags -R . *.c *.h *.hpp *.cpp --tag-relative=yes ./ 2>/dev/null")
silent! execute (":!cscope -b -R") | redraw!
normal == :cs reset<CR><CR>
normal == :TlistUpdate<CR>
endfunction
nnoremap <silent> <C-k> :call UpdateTags()<CR>
I see that the tags and cscope.out file is updated. However, i am unable to resolve these few things:
The screen flickers twice (I have put redraw only once in the function)
The taglist does not get updated. If I execute :TlistUpdate command again manually, then I see the new tags.
Following code is working:
function UpdateTags()
call system ("rm -rf tags cscope.files cscope.out")
call system ("ctags -R . *.c *.h *.hpp *.cpp --tag-relative=yes ./ 2>/dev/null")
call system ("cscope -b -R")
silent cscope reset
TlistUpdate
endfunction
sytem()
Swap execute with system. This has two benefits:
The screen doesn't blink and need redrawing, because of how system works
You should be able to use silent instead of silent!—the latter hides any errors
Use Ex (colon) commands as commands
normal == how you pretend the user ran == from normal mode. (You can avoid maps using normal!.)
To run, e.g., :cscope reset or :TlistUpdate, you just run them:
function! UpdateTags() abort
" ...
cscope reset
TlistUpdate
" ...
endfunction

Vim: Vimscript to call netrw command

This is a followup (but a distinct question) to this question, which I'll reiterate here for completion.
I have a Vim mapping to start searching (ack-grep with ack.vim plugin) for a pattern from the directory that is the current directory (so the result after :pwd). This mapping works when I'm looking at a buffer.
I want to use the same mapping while I'm in netrw. But, I want to change the current directory (:pwd) to the directory netrw is showing me, so the search will be started from the directory I'm looking at. I know I can do this with the netrw c command. How do I give the c command from within a function?
I've tried:
function! StartAckSearch()
" If we're in netrw change the current directory to the directory we're
" viewing
if &ft ==# 'netrw'
echo 'in netrw'
c
endif
endfunction
nnoremap <Leader>a :call StartAckSearch()<CR>
And:
I've tried:
function! StartAckSearch()
" If we're in netrw change the current directory to the directory we're
" viewing
if &ft ==# 'netrw'
echo 'in netrw'
execute 'c'
endif
endfunction
nnoremap <Leader>a :call StartAckSearch()<CR>
But they both don't work.
Question
How do I call a netrw command using Vimscript? (If my question can be rephrased to be clearer, please go ahead)
I think you can use norm c to call it.
Another way is exe 'norm c'

Create file inside new directory in vim in one step?

While in vim I want to create a new file called blog_spec.rb inside [working directory]/spec/models/, but the directory doesn't exist yet?
What's the fastest way to create the directory and start editing the file? Any oneliners?
:!mkdir -p spec/models
:w spec/models/blog_spec.rb
If you encounter this often it may be worthwhile to add a command for it.
command -nargs=1 E execute('silent! !mkdir -p "$(dirname "<args>")"') <Bar> e <args>
If you add that line in your .vimrc file, you can simply use it like this:
:E spec/models/blog_spec.rb
Edit This will only work on Linux/Mac, not Windows.
Try the following command:
function s:MKDir(...)
if !a:0
\|| stridx('`+', a:1[0])!=-1
\|| a:1=~#'\v\\#<![ *?[%#]'
\|| isdirectory(a:1)
\|| filereadable(a:1)
\|| isdirectory(fnamemodify(a:1, ':p:h'))
return
endif
return mkdir(fnamemodify(a:1, ':p:h'), 'p')
endfunction
command -bang -bar -nargs=? -complete=file E :call s:MKDir(<f-args>) | e<bang> <args>
This command is intended to be a replacement for built-in :e.
Conditions in which mkdir is not run (in order):
Command is run without arguments
Command is run with `generate filename` or `=generate_filename()` backticks filename generators or with +command/++opt switches.
Command contains more then one argument or has unescaped special characters.
Argument is a directory.
Argument is an existing file.
Argument is a file in an existing directory.
In last three cases nothing should be done, second and third cases are not impossible to handle, just more complicated.
The above is ready for adding a cnoreabbrev:
cnoreabbrev <expr> e ((getcmdtype() is# ':' && getcmdline() is# 'e')?'E':'e')
-complete=file spoils things: it add not only completion, but also arguments processing (thus checking for ` expansion and special characters presence does not make sense) and forbids having multiple “filenames” (thus no ++opt).
-bar makes you unable to use `="String"` because " now starts a comment. Without -bar it is not a :e emulation because you can’t do E file | another command.
Another version:
function s:MKDir(...)
if !a:0
\|| isdirectory(a:1)
\|| filereadable(a:1)
\|| isdirectory(fnamemodify(a:1, ':p:h'))
return
endif
return mkdir(fnamemodify(a:1, ':p:h'), 'p')
endfunction
command -bang -bar -nargs=? -complete=file E :call s:MKDir(<f-args>) | e<bang> <args>
I generally find that the parent directories don't already exist only after trying to save the file.
This code will prompt you to create the directory with :w, or just do it with :w!:
augroup vimrc-auto-mkdir
autocmd!
autocmd BufWritePre * call s:auto_mkdir(expand('<afile>:p:h'), v:cmdbang)
function! s:auto_mkdir(dir, force)
if !isdirectory(a:dir)
\ && (a:force
\ || input("'" . a:dir . "' does not exist. Create? [y/N]") =~? '^y\%[es]$')
call mkdir(iconv(a:dir, &encoding, &termencoding), 'p')
endif
endfunction
augroup END

How to execute file I'm editing in Vi(m)

How to execute file that I'm editing in Vi(m) and get output in split window (like in SciTE)?
Of course I could execute it like that:
:!scriptname
But is it posible to avoid writing script name and how to get output in split window instead just bottom of the screen?
There is the make command. It runs the command set in the makeprg option. Use % as a placeholder for the current file name. For example, if you were editing a python script:
:set makeprg=python\ %
Yes, you need to escape the space. After this you can simply run:
:make
If you wish, you can set the autowrite option and it will save automatically before running the makeprg:
:set autowrite
This solves the execute part. Don't know any way of getting that output into a split window that doesn't involve redirection to file.
To access the current buffer's filename, use %. To get it into a variable you can use the expand() function. To open a new window with a new buffer, use :new or :vnew. To pipe the output from a command into the current buffer, use :.! . Putting it all together:
:let f=expand("%")|vnew|execute '.!ruby "' . f . '"'
obviously replacing ruby with whatever command you want. I used execute so I could surround the filename with quotation marks, so it'll work if the filename has spaces in it.
Vim has ! ("bang") command which executes shell command directly from VIM window. Moreover it allows launching sequence of commands that are connected with pipe and read stdout.
For example:
! node %
is equivalent to opening command prompt window and launching commands:
cd my_current_directory
node my_current_file
See "Vim tips: Working with external commands" for details.
I have a shortcut for that in my vimrc:
nmap <F6> :w<CR>:silent !chmod 755 %<CR>:silent !./% > .tmp.xyz<CR>
\ :tabnew<CR>:r .tmp.xyz<CR>:silent !rm .tmp.xyz<CR>:redraw!<CR>
This writes the current buffer, makes the current file executable (unix only), executes it (unix only) and redirects the output to .tmp.xyz, then creates a new tab, reads the file and then deletes it.
Breaking it down:
:w<CR> write current buffer
:silent !chmod 755 %<CR> make file executable
:silent !./% > .tmp.xyz<CR> execute file, redirect output
:tabnew<CR> new tab
:r .tmp.xyz<CR> read file in new tab
:silent !rm .tmp.xyz<CR> remove file
:redraw!<CR> in terminal mode, vim get scrambled
this fixes it
For Shell script I've used
:set makeprg=%
:make
Vim 8 has an interactive terminal built in. To run the current bash script in a split pane:
:terminal bash %
or for short
:ter bash %
% expands to the current file name.
From :help terminal:
The terminal feature is optional, use this to check if your Vim has it:
echo has('terminal')
If the result is "1" you have it.
I use a slightly more intrusive mechanism through maps:
map ;e :w<CR>:exe ":!python " . getreg("%") . "" <CR>
Just makes it so I don't have to save, then go. Just go.
You can use vim's plugin bexec. To my knowledge the latest version is 0.5.
Then:
$ mkdir -p ~/.vim/plugin
$ mv bexec-0.5.vba ~/.vim/plugin
$ vim ~/.vim/plugin/bexec-0.5.vba
Inside vim itself while editing the .vba file do:
:so %
Some output will show up letting you know that bexec.vim has been written as well as documentation, etc..
Now, you can test it by opening your (whatever language script that has an #! interpreter working properly) in vim and run
:Bexec
Note: I wanted the split to be vertical rather than horizontal, so I did:
$ grep -i -n split ~/.vim/plugin/bexec.vim | grep -i hor
102: let bexec_splitdir = "hor" " hor|ver
261: exec {"ver":"vsp", "hor":"sp"}[g:bexec_splitdir]
and changed the value of from "hor" to "ver"..
I know it's an old question, but I hope this can help someone out there. I have been running in the same issue while taking Coursera's Startup Engineering course where professor Palaji uses Emacs and I don't like Emacs..
Just use colon and exclamatory mark as shown below
:!< script_name>
Based on #SethKriticos and #Cyril answers I now use the following:
function! Setup_ExecNDisplay()
execute "w"
execute "silent !chmod +x %:p"
let n=expand('%:t')
execute "silent !%:p 2>&1 | tee ~/.vim/output_".n
" I prefer vsplit
"execute "split ~/.vim/output_".n
execute "vsplit ~/.vim/output_".n
execute "redraw!"
set autoread
endfunction
function! ExecNDisplay()
execute "w"
let n=expand('%:t')
execute "silent !%:p 2>&1 | tee ~/.vim/output_".n
" I use set autoread
"execute "1 . 'wincmd e'"
endfunction
:nmap <F9> :call Setup_ExecNDisplay()<CR>
:nmap <F2> :call ExecNDisplay()<CR>
Use F9 to setup the new window and F2 to execute your script and tee to your output file.
I also added the script name to the output file name, so that you can use this for multiple scripts at the same time.
In your .vimrc you can paste this function
function! s:ExecuteInShell(command)
let command = join(map(split(a:command), 'expand(v:val)'))
let winnr = bufwinnr('^' . command . '$')
silent! execute ':w'
silent! execute winnr < 0 ? 'vnew ' . fnameescape(command) : winnr . 'wincmd w'
setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
silent! execute 'silent %!'. command
silent! redraw
silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
silent! execute 'wincmd w'
" echo 'Shell command ' . command . ' executed.'
endfunction
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)
cabbrev shell Shell
After that, in vim run command :shell python ~/p.py as example. And you will get the output in splitted window.
+ After changes in p.py as example you will run the same command again, this function will not create new window again, it will display the result in the previous(same) splitted window.
#xorpaul
I was looking for this script (python/Windows) for quite some time. As there is no "tee" in Windows I changed it to:
function! Setup_ExecNDisplay()
execute "w"
let n=expand('%:t')
execute "silent ! python % > d:\\temp\\output_".n ." 2>&1"
execute "vsplit d:\\temp\\output_".n
execute "redraw!"
set autoread
endfunction
function! ExecNDisplay()
execute "w"
let n=expand('%:t')
execute "silent ! python % > d:\\temp\\output_".n . " 2>&1"
endfunction
:nmap <F9> :call Setup_ExecNDisplay()<CR>
:nmap <F2> :call ExecNDisplay()<CR>
I'd recommend the plugin quickrun. It's fast and simple to configure. Here's a little demonstration:

Resources