Can I call multiple function on Vim startup? - vim

I frequently send files to Vim from Visual Studio. I have it set up as an external tool with the following parameter:
"+call cursor($(CurLine), $(CurCol))"
However, I also want to be able to call my own function as well. When I'm editing a file from VS I want the window to be large, so I expected to be able to do something like this:
"+call cursor($(CurLine), $(CurCol)); +call Embiggen()"
However, that doesn't work. I've tried a few variations (e.g. , call Embiggen(), etc).
Obviously I could write my own PlaceCursorAndEmbiggen function, but I don't really want to do that. Is there any way to call multiple functions on Vim startup?

Eureka!
Simply pass two strings:
"+call cursor($(CurLine), $(CurCol));" "+call Embiggen()"

Maybe the solution would have been easier to find had you used the alternative, more commonplace syntax: -c "cmd" instead of "+cmd". According to :help -c, you can pass up to 10 of these.

These exact commands can be combined into one using pipe symbol:
"+call cursor($(CurLine), $(CurCol)|call Embiggen()"
. There are much more that can be combined this way, but some like :normal can’t, use #Ingo Karkat’s or your own answer for them. If you are short* on +commands and still don’t want to create a .vim file you can use either :execute
vim -c "execute 'normal! 1' | execute 'normal! 2'"
or (bash/zsh) -S with process substitution:
vim -S <(echo '
normal! 1
normal! 2
')
. Though most of time it is better to just create a .vim file.
* You can pass up to 10 + or -c (they are equivalent and they are not counted separately) and 10 other --cmd, though letter is less useful.

Related

vimscript augment rtp with directory above result of system command

I'm trying to modify my vimrc to include a directory
let g:mydir = system('which someExecutable')
execute "set rtp+=" . g:mydir
The problem is that which someExecutable returns something like
/aDir/a/b.
I need g:mydir set to /aDir/, so two dirs above b.
Is there an easy way to do this in vimscript?
You're looking for fnamemodify(path, ":h")
If you version of vim is recent enough, you can even use exepath('someExecutable') instead of system('which someexecutable'). Which gives:
fnamemodify(exepath('someExecutable'), ":h")
PS: don't forget to escape what must be escaped if you use exe "set rtp+=....

Is it possible to access vim's command-line arguments in vimscript?

I found the answer to this question while writing it, so I've broadened it a little. I wanted to access the --servername argument, in order to create dynamic settings in my .vimrc file.
Through vim's help, I found the v:servername variable, and my script is working. However, now I'm curious if it's possible to access any arbitrary command-line argument. For example, if I wanted to know if vim was in Lisp mode (-l) or Debugging mode (-D), how would I do it? There seems to be no corresponding v: variable for them.
Here are the variables I found by autocompleting :help v:<Tab>
Is there a general way to access command-line arguments from vimscript?
Strangely, I think the answer may be "No, there is no direct way to access startup options specified on the command line".
The :args command and argv() can be used to access the filename(s) specified on startup, but that's not what you want.
I see on Vim's forums that someone offered this solution to get the startup command line on Linux:
:exe '!tr "\0" " " </proc/' . getpid() . '/cmdline'
I assume there's analogous command on Windows. . . .
You can look over that forum thread here:
http://groups.google.com/group/vim_use/browse_thread/thread/43773f27cdc10265/ad17ae8180c0fb6e?show_docid=ad17ae8180c0fb6e
My googling indicates that this feature has been proposed but never implemented. However I did come up with a bit of a kludge that nevertheless works:
:echo split( system( "ps -o command= -p " . getpid() ) )
# => [ 'vim', ... arguments ... ]
(Tested on OS X Lion.)
The getpid() function gets Vim's PID, then we call ps externally with options to return nothing but the "command" value for process, then use split() to split the command into a list.

Efficient way to refactor a class/method/string within a directory using vim

So far, I have been manually refactoring code by using the find-and-replace operation
%s:/stringiwanttoreplace/newstring/g
in vim.
But this is a slow and laborious process if I have stringiwanttoreplace in many files inside a specific directory.
My current/typical slow and laborious process involves a grep:-
grep -rn "stringiwanttoreplace" .
in my terminal to reveal all the locations/filenames where stringiwanttoreplace are; and now that I know which files contain stringiwanttoreplace, I will open each file one-by-one to perform the find-and-replace operation in each file.
Is there a more efficient workflow (in vim) to get this done?
CLARIFICATION: I would prefer a vim-based solution instead of a bash script/one-liner.
Here's the full sequence of commands that I would use:
/stringiwanttoreplace
:vimgrep /<c-r>// **
:Qargs
:argdo %s//newstring/g
:argdo update
In the first line, we search for the target pattern. That populates the last search pattern register (:help quote/), which means that we won't have to type it out in full again.
The :vimgrep command searches the entire project for the specified pattern. Type <c-r>/ as ctlr+r followed by / - this inserts the contents of the last search pattern register onto the command line. The first and last / symbols are delimiters for the search field. The trailing ** tells Vim to look inside every file and directory below the current directory.
At this point, the quickfix list will be populated with search matches from all matching files. :Qargs is a custom command, which populates the argument list with all of the files listed in the quickfix list. Here's the implementation:
command! -nargs=0 -bar Qargs execute 'args ' . QuickfixFilenames()
function! QuickfixFilenames()
" Building a hash ensures we get each buffer only once
let buffer_numbers = {}
for quickfix_item in getqflist()
let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr'])
endfor
return join(values(buffer_numbers))
endfunction
Add that to your vimrc file.
Having run :Qargs, our argument list should now contain all of the files that include our target string. So we can run the substitution command with :argdo, to execute the command in each file. We can leave the search field of the substitution command blank, and it will automatically use the most recent search pattern. If you want, you could include the c flag when you run the substitution command, then you'll be prompted for confirmation.
Finally, the :argdo update command saves each file that was changed.
As #Peter Rincker pointed out, you should ensure that Vim's 'hidden' option is enabled, otherwise it will raise an error when you try to switch to another buffer before writing any changes to the active buffer.
Also, note that the last 3 commands can be executed in a single command line, by separating them with a pipe character.
:Qargs | argdo %s//replacement/gc | update
The :Qargs command is pinched from this answer (by me), which in turn was inspired by this answer by DrAl. A very similar solution was posted by #ib, which suggests to me that Vim should really implement something like :quickfixdo natively.
If you really want to do it in Vim you can follow the suggestions here.
You can call this from within Vim (:!find ...) but you don't need to:
find . -type f | xargs sed -i 's/stringiwanttoreplace/newstring/g'
Fine-tune the file selection with the dozens of parameters described in
man find
(e.g., replace only in HTML files: -name \*.html)
This solution will try to attempt the replacement in all files. You can filter that through grep before, but that is just doing twice the work for no gain.
By the way: sed uses almost the same syntax for regular expressions as Vim (stemming from the same history).
You could open all the files and type
:bufdo :s/stringiwanttoreplace/newstring/g
It performs the search/replace in all your buffers.
You don't need vim to do this, you can use command line tools. Using sed in a loop on the list of files to do this for you automatically. Something like this:
for each in `grep -l "stringiwanttoreplace" *` ;
do
cat $each | sed -e "s/stringiwanttoreplace/newstring/g" > $each
; done
vim7 has recursive grep built-in
:vimgrep /pattern/[j][g] file file1 file2 ... fileN
the result will be shown in a quickfix-window (:help quickfix)
to do the search recursively use the **-wildcard like
**/*.c to search through the current folder and recursively through all subdirectories.

Vim verbosefile with timestamps

I have a number of very long and complex Vim configuration files, but these seem to be causing some unpredictable performance slowdowns.
Is there a way of producing a logfile of Vim's internal activity (in the style of verbosefile) which also includes timestamps for each function call to allow me to debug these issues. Alternatively, are there any other tools to help me in this task?
One thing you can try is invoking vim with --startuptime some_file. This will dump the steps performed during startup, along with timestamps. Try :help --startuptime for some more information.
Unfortunately, this shows you timestamps for files, not functions. If you want to see how long a specific function takes, you could put a call to it in a file under the plugin directory, so it's executed on boot. This won't be possible in all cases and it won't give you a lot of granularity, but it might help you narrow down the problem. Someone else might have a better idea, but I'd start by trying something like this.
EDIT:
I just found two things that may help with this problem. One of them is reltime(). Here's an example from vim's help:
let start = reltime()
call MyFunction()
echo reltimestr(reltime(start))
All it does is show the time a function takes to execute. Combined with, for example, Decho, this could provide a simple log of said functions.
The second one should be a lot more powerfule, the :profile command. It lets you profile a piece of vimscript and place the results in a file. :help profile should tell you all you need to know. Unfortunately, it's only available with the +profile feature, which seems to be enabled only in the "huge" feature set. If your vim doesn't have it, you'll need to recompile. If that's a problem, or you don't want to bother, you might be able to achieve good results with reltime alone.
If --startuptime does not fit your needs, you may use the following command:
vim --cmd 'profile start profile.log' \
--cmd 'profile func *' \
--cmd 'profile file *' \
-c 'profile pause' \
-c 'qa!'
(replace --cmd with -c and add -c 'Command that causes slowdown' if problem occurs not at startup). You will get all timings in the profile.log. Table with function timings will be present at the end of the file, to get per-script timings, use the following
script:
" Open profile.log file in vim first
let timings=[]
g/^SCRIPT/call add(timings, [getline('.')[len('SCRIPT '):], matchstr(getline(line('.')+1), '^Sourced \zs\d\+')]+map(getline(line('.')+2, line('.')+3), 'matchstr(v:val, ''\d\+\.\d\+$'')'))
enew
call setline('.', ['count total (s) self (s) script']+map(copy(timings), 'printf("%5u %9s %8s %s", v:val[1], v:val[2], v:val[3], v:val[0])'))
This will open a new file containing just the same table as at the end of profile.log, but 1) for scripts, not for functions, 2) unsorted.
Better late than never? I was searching to see if I could get vim to include some timestamps in the &verbosefile, as it causes vim to append to the file with no indication of when the appending started.
I adapted the solution from ZyX's answer here and after trying some things out, found that a simple echo "foo\n" will append foo and a newline to the &verbosefile:
let s:VerboseLS = ' **** V'.'E'.'RB'.'OSE LO'.'GG'.'ING S'.'TA'.'RTED ****'
let s:VerboseLE = ' **** V'.'E'.'RB'.'OSE LO'.'GG'.'ING E'.'ND'.'ED ****'
function! ToggleVerbose()
let l:timestamp = strftime("%c")
if !&verbose
let l:msg = "\n\n" . l:timestamp . s:VerboseLS . "\n\n"
set verbosefile=~/.vim/log/verbose.log
set verbose=100
echo l:msg
else
let l:msg = "\n" . l:timestamp . s:VerboseLE . "\n"
echo l:msg
set verbose=0
set verbosefile=
endif
endfunction
nnoremap <silent> <Leader>vl :call ToggleVerbose()<CR>
Now I get nice banners in my log:
Sun Feb 17 14:18:02 2013 **** VERBOSE LOGGING STARTED ****
...[snip]...
Sun Feb 17 14:18:08 2013 **** VERBOSE LOGGING ENDED ****
I defined those banners the way I did so that if I source that script file while verbose logging is on so if I search the log for "VERBOSE LOGGING" or similar I won't match on them.
So, you could adapt this method to use reltime if your vim has('reltime') for sub-second precision and maybe make a timing wrapper to call that echos the start/end times for function calls (although you'd have to refactor when you call things inline without the call command).
Ooh, I just had an idea... I wonder if &verbosefile can be set to a named pipe that goes to util that prepends time stamps... Let's see...

How to do search & replace with ack in vim?

I am using the Ack plugin in Vim, which helps me to quickly search for strings in my project. However, sometimes I want to replace all or some occurrences of the found strings. You can do some kind of global search and replace using the Vim arglist like this (source)
:
:args app/views/*/*
:argdo %s/, :expire.*)/)/ge | update
But instead of using args, I would prefer to do a search via Ack and then do the replace in all files that have been found. Is there a way to do it similar to the argdo command?
I've decided to use ack and perl to solve this problem outside of Vim so I could use the more powerful Perl regular expressions instead of the GNU subset. You could map this to a key stroke in your .vimrc.
ack -l 'pattern' | xargs perl -pi -E 's/pattern/replacement/g'
Explanation
ack
ack is an awesome command line tool that is a mix of grep, find, and full Perl regular expressions (not just the GNU subset). It's written in pure Perl, it's fast, it has match highlighting, it works on Windows and it's friendlier to programmers than the traditional command line tools. Install it on Ubuntu with sudo apt-get install ack-grep.
xargs
xargs is an old Unix command line tool. It reads items from standard input and executes the command specified followed by the items read for standard input. So basically the list of files generated by ack are being appended to the end of the perl -pi -E 's/pattern/replacement/g' command.
perl -pi -E
Perl is a programming language.
The -p option causes Perl to create a loop around your program which iterates over filename arguments.
The -i option causes Perl to edit the file in place. You can modify this to create backups.
The -E option causes Perl to execute the one line of code specified as the program. In our case the program is just a Perl regex substitution.
For more information on Perl command line options, see perldoc perlrun. For more information on Perl, see http://www.perl.org/.
Now, Vim has this new command cdo that will run the given command to each line of the quickfix list.
So you can use
:Ack pattern
:cdo s/pattern/newpattern/g
I don't believe there's a built in way of doing this, but it should be easy to make one.
What you need to do is create a command that calls a custom function. The function should then use the getqflist() function to get all of the entries in the quickfix list and exe to do the dirty work. Be careful what you pass as an argument!
" Define a command to make it easier to use
command! -nargs=+ QFDo call QFDo(<q-args>)
" Function that does the work
function! QFDo(command)
" Create a dictionary so that we can
" get the list of buffers rather than the
" list of lines in buffers (easy way
" to get unique entries)
let buffer_numbers = {}
" For each entry, use the buffer number as
" a dictionary key (won't get repeats)
for fixlist_entry in getqflist()
let buffer_numbers[fixlist_entry['bufnr']] = 1
endfor
" Make it into a list as it seems cleaner
let buffer_number_list = keys(buffer_numbers)
" For each buffer
for num in buffer_number_list
" Select the buffer
exe 'buffer' num
" Run the command that's passed as an argument
exe a:command
" Save if necessary
update
endfor
endfunction
You could using ack by this way
:args `ack -l User app/`
:argdo %s/, :expire.*)/)/ge | update
Or use ag
:args `ag -l User app/`
:argdo %s/, :expire.*)/)/gec | w
I use MacVim (activated with mvim in a shell). I pipe the results of ack to mvim:
mvim -f $(ack -l $#)
Then in MacVim, I search/replace using bufdo:
:bufdo %s/SEARCH/REPLACE/gce | update
Omit the c option if confirmation is not needed.

Resources