.vimrc to behave like Textmate? - textmate

I am making the switch to VIM from Textmate and I wanted to know what should be included in my .vimrc to get the similar behavior for:
auto-close on brackets, paranthesis, etc.
auto tabbing after a bracket
when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line
pressing <tab> on for will generate a common 'for' use
Thanks.

The first thing to be aware is that you can't turn Vim into TextMate. The second, is that you shouldn't even try.
Instead, focus your efforts on learning Vim, progressively, and grow/shrink your collection of settings and plugins as you need. Using Janus or some other "distro" will only occult Vim behind other people's arbitrary choices.
That said,
"auto-close on brackets, paranthesis, etc."
can be done in a "dumb" way by adding this line to your ~/.vimrc:
inoremap ( ()<Left>
inoremap is for mapping in insert mode (note the i)
( is the key you want to press, that's your trigger
()<Left> means input a pair of parenthesis then go back one character, between the parenthesis
If you need a "smarter" way, there are a many plugins to choose from.
You might want to try surround which was in and of itself a very compelling reason for me to switch from TextMate.
"when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line"
can be done in many ways as well, for example:
inoremap <C-Return> <CR><CR><C-o>k<Tab>
inoremap, again
<C-Return> means Ctrl+Return, that's your trigger
<CR><CR> means 2 carriage returns to push the closing bracket 2 lines below
<C-o> to leave insert mode only for one command
k to go up one line
<Tab> to put the insertion cursor at the right place
Mappings in Vim can be just that: shortcuts for sequences of keypresses or more serious scripts.
"pressing <Tab> on for will generate a common 'for' use"
can be done with a dedicated plugin like SnipMate. There are others.

auto-close on brackets, paranthesis, etc.
There are a number of plugins for this functionality: autoclose, closepairs, simplepairs
auto tabbing after a bracket
By this, I believe you mean automatic indentation. This is a default Vim behavior when you're working on a supported filetype. If you're just starting on a file and haven't saved it, you can manually set the filetype with :set ft=<whatever filetype you want> and you'll get indentation and syntax highlighting.
when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line
This will be taken care of with the above auto-close plugins.
pressing tab on for will generate a common 'for' use
These are called "snippets" in TextMate. Snipmate is a commonly used Vim plugin for snippets. Other people prefer xptemplate.
If you're moving from TextMate to Vim, I would try installing Janus and thoroughly reading the documentation.
Janus is a great starter distribution of plugins and mappings for Vim, gVim, and MacVim. It features sane defaults and aims to provide a minimal working environment using the most popular plug-ins and the most common mappings. It was developed and is maintained by Carl Lerche and former TextMate user Yehuda Katz.
My own personal experience/advice is that you can really drive yourself crazy trying to mimic every feature of your favorite editor exactly as it was. Each feature you're trying to replicate is probably just a quirk you became used to through muscle memory. You'll be much more productive if you just start "accepting" Vim, learning it, and re-train your muscle memory for it.

Related

Terminals that allow basic text editing of commands

Is there a terminal application, particularly for Mac, that allows commands' text to be edited as in a word processor?
For example, in most terminals I know of, a user can't use ALT+BACKSPACE, can't remove more than one character at a time, can't cut, copy, or paste easily (if at all), can't use CMD+ARROW to go from one end of a long command to another quickly, etc., etc.
P.S. Is there a reason why editing text commands in a terminal is so (universally) cluncky?
In bash, you can
move to the beginning of the line with Ctrl-A
move to the end of the line with Ctrl-E
delete a whole word with Esc and then Backspace
These and other routines come from Emacs. If you learn the basics of how to use that, then the shell will become very intuitive.
As for copy and paste, these are handled by the terminal rather than the shell. In many environments, just highlight the text with your mouse to copy, and then paste with either the middle button or right button of your mouse. In the Mac terminal, you can use the common copy and paste routines.

Smart indent effect on completion (intelliSense) sessions

I am writing a Visual Studio extension which provides intelliSense for a certain content type.
The problem that I am facing now is the effect of "Auto Indent" that Visual Studio provides on empty lines when user types a character.
Here a completion session started on an empty line (over virtual spaces):
Notice the tab symbols on the other lines and no tab on the line with caret on it.
Now when use starts typing, VS automatically and correctly adds necessary tab characters to the line:
Now the problem is those Added tabs apparently become part of the user input and as a result CurrentSession.SelectedCompletionSet.SelectBestMatch() or Filter() method cannot find the current item which starts with "C" here (thinking user has typed \t\tC instead).
If I start the session on anywhere else which does not require auto indent everything works fine.
Any idea?
Edit (more information): I used a code flow very similar to:
Ook here
vsLua here
vsClojure here
In Lua and Clojure you wouldn't face this problem because they never provide intelliSense on virtual spaces (meaning they always start after a certain set of characters) and if you start after a character virtual spaces are already turned into real spaces.
Ook on the other had has the same problem.
Revised Answer:
Ah, I see. I interpreted your question thinking that you were referring to completion triggering via typing, not from the explicit command. If you enable "show whitespace" for the C# editor, you can see what we do here: when you trigger the "show completion" command, we explicitly realize the whitespace so you're no longer floating around in virtual space. You should probably do this as well. (Alternatively, you could detect the scenario and fix it up on the first typing by adjusting your ApplicableTo span, but that's probably not worth the trouble.)
You can get the whitespace that should be inserted from IEditorOperations. So MEF import an IEditorOperationsFactoryService, and then do:
var editorOperations = editorOperationsFactoryService.GetEditorOperations(textView);
var whitespace = editorOperations.GetWhitespaceForVirtualSpace(textView.Caret.Position.VirtualBufferPosition);
if (whitespace.Length != 0)
{
textView.TextBuffer.Insert(textView.Caret.Position.BufferPosition, whitespace);
}
(Funny aside: as I answered this, I was curious to see how we handled this in the Roslyn C# and VB editors. The answer was "not", but filtering still worked by pure luck later in the code.)
Original Answer:
I suspect by your description of the problem that you are implementing your completion like this: you know a character is about to be typed (either via a keyboard filter or IOleCommandTarget) and you trigger an ICompletionSession, where the tracking span is an empty span on the current caret position.
The best approach to fixing this is to not trigger the session before the key is pressed and goes into the editor, but rather after it. This is what we do in the Roslyn implementation for C# and VB completion. Then, when you are in your AugmentCompletionSession call and creating your CompletionSet, compute the "applicable to" span which consists of the non-whitespace characters around your caret. The easiest way to compute this might just be to call GetWordExtent from the text structure navigator.
This allows for other scenarios to work right. Consider scenarios where the user types C, presses escape, and then continues to type your identifier. If you want to trigger completion again, you'd have to do the math to ensure that the "C" is counted as part of your span anyways.

Keyboard-only column block selection in GVim Win32, or why does Ctrl-Q not emulate Ctrl-V when mswin.vim is included?

I want to be able to select columnar blocks using only the keyboard when I use GVim on Windows, but I do not seem to be able to do so when using gvim (7.2) on Windows XP. Edit: For convenience, it is preferable to make the selection with arrow keys as is commonly done in other versions of vi/vim.
In a terminal, when using vim, to define a block, one may select columnar blocks by pressing Ctrl-V and by conveniently moving the cursor with the arrow keys.
When using GVim on mswin, Ctrl-V is mapped to a paste operation. Every reference found on this topic mentions that on mswin, Ctrl-Q is set to act the same way that Ctrl-V works on other platforms, but this does not work for me.
After research indicated that the Ctrl-Q behavior is implemented through the inclusion of mswin.vim, it seemed that perhaps mswin.vim was not being loaded by GVim. mswin.vim is apparently loaded via _vimrc. It seemed that perhaps mswin.vim was not loading, so perhaps _vimrc was not setup correctly, but...
C:\Program Files\Vim\_vimrc exists contains:
source $VIMRUNTIME/mswin.vim
behave mswin
C:\Program Files\Vim\vim72\mswin.vim exists and contains:
" Use CTRL-Q to do what CTRL-V used to do
noremap <C-Q> <C-V>
C:\Program Files\Vim\_vimrc should load since Edit | Settings Window in the GVim graphical menu loads this file for editing, and it is clear that C:\Program Files\Vim\vim72\mswin.vim is loading because one can insert a syntax error in that file and GVim will complain about it when it starts up (it was backed it up for testing and restored to prevent accidental corruption).
When Ctrl-Q is pressed, the cursor changes in the same way that it changes when you block select text using the mouse, but any cursor movement at this point causes the cursor to change back to a normal cursor. It does not matter whether one continues to hold Ctrl, Ctrl-Q, or not.
Edit: Ctrl-Q enters block selection mode as indicated by the cursor change since other cursor movement commands extend the block, however, the original intent of this question was to learn how to use the arrow keys (in the same manner that they are able to be used in other implementations of the editor) though this was not explicitly stated in the original, unanswered revisions of the question.
It is possible to visually select columnar blocks by pressing Alt while holding down the left-mouse button, but Alt during arrow key motion after pressing Ctrl-Q also does not work.
Research also seemed to indicate that pressing v in command-mode would enter visual-mode, and that perhaps this was relevant, but using this does not seem to help the situation.
C:\Program Files\Vim\vimfiles does not contain anything except an empty directory structure. C:\Documents and Settings\username does not contain vim configuration files, nor does C:\Documents and Settings\username\My Documents. Other C:\Documents and Settings\username locations, where application-specific or local settings are commonly stored, also appear not to have vim settings. The same is true for %HOMEDRIVE% and for the environment variables.
One way to visually select column blocks in GVim on Win32 using only the keyboard is to press Ctrl-Q, release it, then press and hold down the Shift key while using the arrow keys to select the column block.
Why Ctrl-Q columnar selection behaves this way is not known, but it does not really matter since the goal was to select column blocks using only the keyboard. In Vim, Shift is not used when using Ctrl-V to select blocks.
:help mswin does not mention this nuance of the CTRL-V alternative.
The best thing to do is to remove these lines from your vimrc. They are totally unecessary and change too many basic Vim features.
The noremap <C-Q> <C-V> in mswin.vim remaps the original command to Ctrl + Q.
If mswin.vim is included, after pressing Ctrl + Q, the indicated mode should switch to Visual Block, and any movement (e.g. j, l, w) extends the visual selection. If that doesn't work for you, you may have something interfering.
Are those movement commands itself remapped, or is there an autocmd on CursorMoved?! Try disabling your plugins (vim --noplugin) and most parts of your .vimrc.
Note that cursor keys do not normally constitute proper movement (and their use in Vim is frowned upon).
What you perceive as the right (and only) way to extend the selection (with shifted cursor keys) is just a consequence of :set selectmode=key, as done by :behave mswin.

How do I switch tabs based on incremental search in vim?

For example, let's say I have three tabs open in vim:
1: nice_program.c
2: something_fun.h
3: super_script.sh
So if I hit some magic modifier key, and then type 'n' and hit enter I change tab to tab 1. Likewise, typing 'su' instead will navigate me to tab 3 instead.
Is such behavior possible? There are so many vim extensions, and I dont really get the whole vim extension lingo.
BTW, I am using gVim on XP and MacVim on OS X. Preferably the solution will work on both...
EDIT:
Note that I only want the incremental search to search across the names of the open tabs. That is, it's not supposed to actually search inside the tabs themselves.
Also, I never use buffers, it's tabs that I want this working for.
From the wording of the question it seems that you take the idea of tabs in
Vim not the way it is supposed to be taken by design of this feature. A Vim
tab page is not a form of a buffer or a window, it is a window layout
container, instead. No wonder there is no built-in way for switching to a tab
by the name of a buffer that is active (or the only one in its tab page, or
special in some other way). Semantically, that is switching to a buffer, not
a tab (but tab could be switched in order to show a buffer, if it is
necessary).
To switch to a buffer by its name use the :sbuffer command (:sb, for
short). It is not necessary to type the whole buffer name each time, since
the command has auto-completion. Usually one have to type only few letters of
a name to uniquely identify a buffer (the same way as you described
incremental search in the question).
By default, Vim open the requested buffer displacing one in the current
window. This behavior is governed by the switchbuf option. One of the
choices (called usetab) provided by that option allows to switch to a window
in another tab page if that window contains the buffer to edit. This is
exactly what suits your manner of work with tab pages.
To summarize, change the switching behavior as follows
:set switchbuf=usetab
and use the :sb command to open a buffer by typing a few letters of its name
and using Tab-completion.
I use this snippet I picked up in vim wiki to switch between open buffers (mapped to F5):
" switch between numbered buffers
:nnoremap <F5> :buffers<CR>:buffer<Space>
(put in your .vimrc file or whichever dotfile you use).
As for incremental search across open buffers, whenever I look up something using either /[something] or with */# on current word, it's automagically also highlighted in other buffers/tabs. Then I can switch buffers and hit n or N to move between matches in the currently viewed buffer. That's already baked into Vim.
Hope that helps.
The :set switchbuf=usetab solution given by ib never worked for me for whatever reason (even without loading plugins or my .vimrc) but :tab drop name-of-file works just the way you want (I found it on the Vim wiki).
Make it a custom mapping to save a few keystrokes with nnoremap <leader>t :tab drop.
Also I second ib's comment on the right and wrong way to use tabs in Vim.

Automatically insert closing bracket in Xcode

is there a way to have Xcode append a closing bracket ")" when I type the opening one "("? I know that it does this for those {}. I really miss Textmate :( A generic way to define which chars should be automatically inserted when the opening ones are entered would be kickass.
In Xcode 3.1 go to Preferences->Indentation
Check Syntax-ware indenting to on
and check Automatically insert closing "}"
and choose the characters you want to be inserted when the opening one is.
Also why not use Textmate nothing stops you and Xcode will notice if a file is edited externally
Have a look under Edit->Insert Text Macro->Objective C->Bracket Expression. This will insert a matched pair of brackets (or if you have something highlighted, put brackets around it). You could bind this to [ as a keyboard shortcut.
You can follow the instructions here to create one for parentheses. I tried but couldn't make it work.
http://cocoawithlove.com/2008/06/hidden-xcode-build-debug-and-template.html#textmacros
Personally, I drag the classes folder over to Textmate and edit there. I switch back to Xcode when I need to type in some long method name, or to build. I've been building a library of snippets for some of the common things I do in Textmate to make life easier there.

Resources