call vi from within vim (useful for svn commit) - linux

I am using RedHat EL 5. I use gvim 7.1 compiled using GTK. What I want is to be able to do an svn commit (which uses vi/vim) from within gvim. Currently the only problem is that I get output which is garbled.
For example, calling :!vi produces this from within gvim:
[7;1H~
[8;1H~
[9;1H~
[10;1H~
[11;1H~
[12;1H~
[13;1H~
[14;1H~
[15;1H~
[16;1H~
[
17;1H~
[18;1H~
[19;1H~
[20;1H~
[21;1H~
[22;1H~
[23;1H~
[24;1H~
[25;1H~
[26;1H~
[27;1H~
[28;1H~
[29;1H~
[30;1H~
[31
;1H~
[32;1H~
[33;1H~
[34;1H~
[35;1H~
[36;1H~
[37;1H~
[38;1H~
[39;1H~
[15;42HVIM - Vi IMproved[17;43Hversion
7.0.237[18;39Hby Bram Moolenaar et al.[19;29HVim is open source and freely distributable[21;36HHe
lp poor children in Uganda![22;28Htype :help iccf<Enter> for information [24;28Htype :q<En
ter> to exit [25;28Htype :help<Enter> or <F1> for on-line help[26;28Htyp
e :help version7<Enter> for version info[1;1H
How do I configure vi/vim/gvim to solve this problem and thereby enable my svn commits to look proper when called from gvim.
Thank you,
Nachum

Don't use vi as the command, use gvim -f instead.
The problem is that vi (or vim in a console) requires a terminal that can do stuff like move the cursor around, etc. gvim's pty is a very basic ASCII-only terminal.
Plain old vim doesn't have this issue because it just pipes the subporcess directly to your terminal, hence all of the escape sequences still work.
You can use gvim -f instead, so that a new gvim window will pop up for your commit message. (the -f prevents backgrounding) This isn't exactly what you asked for (since you get a new window) but it's the closest you can get to what you asked for without adding full terminal support to vim.

I use VCSCommand, a nice VCS wrapper that works with SVN, GIT and others. :VCSCommit or ,cc opens a new window under the current one, lets you type your message and does the actual commit on write. Sure that's one more plugin in your setup but the conveniance may be worth it. It is for me.

Related

Prevent gVim from returning control to command line (when called from Stata)

When I call gVim from Stata with shell (or equivalently with !) Stata doesn't wait for the command to finish and continues on with the .do file. I usually specify a short sleep and everything works great (discussed on the Stata mailing list here).
But sometimes the gVim call is lengthy and the length is unknown a priori. For example. I use gVim's argdo to remove headers from a folder of text files.
!gvim -c "argdo 1,3d | update" *sheet*.txt
Is there any way that I can force gVim to not return control to Stata? Or are my best options to complete this step outside the .do file or with a pause/lengthy sleep? Thanks!
Oh, I'm on Win 8 (64 bit) with gVim 7.3.
I think you would need to make this call a Stata command or the equivalent thereof.
That is, try running this separately from a do-file editor window or as wrapped up in a separate do-file.
I realise that is not an attractive solution, but in principle it seems the only one.
(sleep solutions I dislike as fudges, but I guess no one likes them on principle.)

Is there a good way to start programs from gvim without losing focus?

Vim has a built-in way for executing programs, namely :!start foo for Windows and :!bar & for Unix. However, the problem I have with those is that they steal focus from Vim. I'd like to launch my build & test script, and while its working and showing output in its command window never have to manually switch back to GVim.
I have thought of 3 possible solutions:
Use Auto-It/AutoHotkey to set focus back to Gvim. Unfortunately that only works in Windows and it's brittle.
Have another process watching my sourcecode, and let that process automatically launch the script when the sourecode changes. (Another process creating a new window does not steal focus). This is a bit gimmicky, as you need to hold off on writing files until you want to start the script, and sometimes write a useless change if you want to re-test even though nothing in the source changed.
Use a client-server architecture, where Vim sends the command over a socket or something similar. This is actually my preferred solution, if there is any existing cross-platform way to do it. (I would prefer not having an extra GVim instance running for this though.)
So what would be a good way to do this? I have tried AsyncCommander but unfortunately it also steals focus.
When I'm not using a nice build system like sbt that watches source code and re-builds/re-tests for me, I tend to come up with hacks like this:
:map <F9> :!touch and-go<CR><CR>
Here hitting F9 updates a file. Pick any key binding you like.
(and of course you could do the same with imap for vim's insert mode as well.)
Then all one needs is a shell script that watches this file and executes the
command that builds/tests your project. On unix, I'd just do:
while stat -c "%Y" and-go; do sleep 1; done |
stdbuf -i0 -o0 uniq -c |
while read l; do
echo build...; # Your build/test execution commands here.
done
Running that in a separate window will be neat, because your vim/gvim window will retain it's focus throughout, while you just keep hitting F9 (or whatever) and glancing at this other window to see how your build went.
If you're up to writing a windows batch script (or powershell, I don't know) version of that as well, you should be good to go.
I think this should work:
:silent! !cmd /c start /b dir
Also, start.exe does have an option to start minimized instead of with the shared/background console.
Of course, dir is to be replaced :)

Vim - indent and syntax highlight broken after recover file

I was editing a file with vim. I started another vim to edit the same file, it said that the /tmp/file.swp exits, press "R" to recover it. I chose to recover. But after that, syntax highlight didn't work on that file (other files still ok). I tried :syntax on and some commands but not worked.
You may try to use the following command to specify the type type. For example for c file:
:set filetype=c
I just solve it, in a simple way.
I find the folder which ~/.vim/view/ and remove the record about the py file which i just recovered...
cd ~/.vim/view && ls -alh | grep filename_1 | awk '{print $9}' | xargs rm
You can change that as you like ... just a demo.
:)
I had to recover a vim buffer after a sudden crash. Whilst the file was correctly recovered, I could not say the same for the syntax highlighting. (I am pretty sure this is not language-dependent, but for completeness sake my file was a python script.)
To my knowledge this issue is often due to either sessions, as stated in this similar question, or syntax highlighting which was incorrectly loaded. However vim views also affect how the file is displayed in the editor. This is especially true for those, like me, who tend to automatise the loading of previously stored views. Following this hunch I deleted my stored view file and solved the problem.
Please note this solution obviously comes at a cost: the file will be opened with a fresh view, i.e. no previous layouts (cursor position, opened/closed fols, etc.) will be remembered by vim. This is pretty obvious but I thought I'd mention it nonetheless.
Might need to set the file type for the correct syntax:
" C syntax:
:set ft=c
" Java syntax:
:set ft=java
" Python syntax:
:set ft=py
" ... etc.
You can re-run the buffer open process to run all autocommands again by typing :e % which edits the current file. This will make all bufread* auto commands run and you'll get everything back. I believe they are deliberately disabled in case there's something bad in the recovered file and custom autocommands may do something bad with a corrupted file.

Use Vim to "colourize" files or input streams

This may be an odd question, but still. I use cat to display a file in bash (KDE Konsole),
cat foobar.rb
Now, I would like to use Vim to colourize that foobar.rb file according to what you would get when you start foobar.rb in Vim. Edit: But only for display purpose, on the terminal.
I am not sure this is possible, but I thought it would be neat if I could use Vim for that.
I really just want colourized keywords, and Vim has the perfect colour definitions.
So I thought combining this would be great.
Is this possible in Vim out of the box though?
One approach would be to use a library such as Pygments, which is a general purpose syntax highlighter. You could write a wrapper called ccat or something that would apply syntax highlighting to an input file and write to stdout.
If you want to page up and down in a highlighted file, you can use less with the -R switch, which passes control characters through to the terminal directly, preserving colours. So:
ccat file.rb | less -R
But at that point, you're pretty much at the capabilities of view.
I'm not sure if I understand your question correctly, but if you are only looking for a command that will give you a read-only view of the input file (like cat) but with coloured keywords, use view. view is an alternative way to start vim in read-only mode, so you have all syntax highlighting possibilities. From the vim man page:
view Start in read-only mode. You will be protected from writing
the files. Can also be done with the "-R" argument.
gvim gview
The GUI version. Starts a new window. Can also be done with
the "-g" argument.
evim eview
The GUI version in easy mode. Starts a new window. Can also
be done with the "-y" argument.
rvim rview rgvim rgview
Like the above, but with restrictions. It will not be possi-
ble to start shell commands, or suspend Vim. Can also be
done with the "-Z" argument.
I have always seen view on systems that have vim installed.
Closest is the less script that comes with vim:
cat myfile | vim -u /usr/share/vim/vim72/macros/less.vim -
Note the - argument to vim. You may need to change the vim72 to your version (and the whole path if you have it installed elsewhere)
Now, this isn't exactly what you want, because its behaviour is less-like, in that you have to press keys to make it scroll down or complete. However, they are briefer than usual vim. For example, space to scroll down; and q to quit (not :q).
You want a cat-like version; me too. But there doesn't seem to be one.
EDIT uh, there's also a vimpager project, that includes vimcat - exactly what you want. But it doesn't come with vim, and I haven't tried it yet.
vim.org: http://www.vim.org/scripts/script.php?script_id=1723
github: https://github.com/rkitover/vimpager

Vim: map GNU Screen command

Apologies if this has been asked before, but I feel overwhelmed with the Vim docs and I can't seem to figure this out.
I want to map the F5 key in Vim to accomplish the following actions:
Yank text from the visual selection in Vim.
Execute the yanked portion of the text in another GNU Screen session named ipython.
The second portion could be achieved by issuing the following command line argument (via :!), if only I was able to find a way to paste the register content between the double quotes of that line (but I can't figure out how):
map <F5> :!screen -x ipython -X stuff "[REGISTER 0 CONTENT]"<CR><CR>
Any help would be much appreciated!
For the second part, you can use
:execute "!screen -x ipython -X stuff " . shellescape(#0)
Here I copy my previous answer:
Maybe one of these two plugins is what you need:
slime is screen-based.
tslime is a tmux-based version of slime.
Given the nature of your question - I encourage you to take a look at vim-ipython. Using this plugin, you can send lines or whole files for IPython to execute, and also get back object introspection and word completions in Vim, like what you get with: object?<enter> and object.<tab> in IPython.
If you follow that github link, there are a few screencasts demonstrating what you can do with it. It requires IPython 0.11 or later, though, with ZeroMQ enabled.

Resources