vim autocomplete on certain keywords [duplicate] - vim

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Autocomplete method structure in Vim
So I have smart autocomplete features on my vim, where it autocompletes '{' with '}' on a new line and redirects control to next line just like eclipse. So typing '{' and pressing enter gives me
{
|
}
Where '|' shows my cursor position. I want something eclipse style for ruby where as soon as write 'def' it should result in
def |
end
The problem is this should only take effect when 'def' is on the beginning of the line, and not otherwise.

there seems to be a plugin named endwise. I have never tried but it sound like what you need.

For this I would also recommend a plugin. General plugins that expand text (but usually the expansion happens only you press and additional key like tab are snipmate or my favorite ultisnips.
With these plugins with the proper snippets you would type def<tab> and it would expand to what you like. I know that ultisnips gives you the option of expanding only in the beginning of line, not sure about snipmate.
Just for fun if you don't want to use a plugin you can define the following in your .vimrc and it will acomplish what you want:
imap <expr> def (getpos(".")[2] == 1) ? "def\<CR>end\<UP> ":"def"

Related

insert something in front of and in end of some word

I have many many source code files, and have to do many same work like this
find some phrase(totally random, have to be done by human eyes)
for example asdf in this line printf("asdf")
and fdsa asdf asdf in this line "* fdsa asdf asdf |"
insert ${' in front of the phrase, and '} in end of the phrase
so printf("asdf") becomes printf("${'asdf'}")
I'm currently using vim to do the edit, is there any plugin can let me, for example, move the cursor onto asdf and press ctrl+shift+i to automatically insert ${' and '} ?
Also I'm open to switch to any other editor which has such capability.
You don't need a plugin for that. A simple mapping would be enough:
xnoremap <key> c${'<C-r>"'}<Esc>
See :h key-notation for <key>.
The :substitute command would have been a great solution as well:
:%s/asdf/${&}/g
And if you want to confirm each replacement before doing it, add the c flag -> %s/asdf/${&}/gc
In addition to the great answers using built-in commands, there's one "famous" plugin for this purpose: the surround plugin. It comes with many mappings for common stuff, and also is customizable.
To define a ysv (you surround variable) mapping, just put the following into your ~/.vimrc:
let g:surround_118 = "${\r}"
(118 is the code point for v, see :help surround)

How to avoid ; repeat command in vim highlighting text?

Like most, i have enabled hlsearch in vimrc.
so, all my text searches are highlighted.
But, when i use 'f' command to move to a letter and use ';' command to repeat, vim highlights all the occurrences of the character overriding my previous text search.
how can i make vim just move to the character without interfering with my text search keyword?
You've likely found a bug in the ft_improved plugin; you should report that to the plugin's issue tracker.
Alternatively, you can also check out an alternative; Extended-FT and Fan,FingTastic are two. Also, using vims f command over multiple lines has some light-weight implementations in the answers.

vim word complete, how to make the first one always as default

I am using several word complete plugins in vim (word_complete.vim, autocomplpop, omnicppcomplete-0.41). So far so good. It will pop up menus to let you choose which word to use, while I am typing the first characters
When I only type 2 characters, vim will set the first word in the popup menu as the default one, then you can directly press enter to use that word.
But the problem is usually 2 characters are not enough to narrow down the words to be complete. I need to type more. After my typing more than 2 characters, the default chosen word will disappear, then I have to use CTRL-N or CTRL-P to choose the word, although it is the first one in the popup menu.
Below is shows what I have:
The first is when I only type 2 characters
But after the third character is typed in, it appears as:
although "airline_detect_whitespace" is what i what to choose, I still need to type CTRL-N to choose it.
I am asking is there a way to configure the way vim chooses its default matcher?
for example, I want to type 5 characters before the default chosen word disappears.
Or is there a way to always make the first one in the popup menu to be chosen by default?
Thanks.
Have a look at the Make Vim completion popup menu work just like in an IDE page on the Vim Tips Wiki. It describes the setup to achieve this. Especially these mappings should create the behavior you want: Always have one menu entry pre-selected.
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
\ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
\ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
Those three plugins have overlapping features and certainly conflicting mappings for the pop-up menu and autocommands and stuff.
For example, AutoComplPop does everything wordcomplete does in a smarter and more automated way. OmniCPPComplete, has an obviously better C++ completion algorithm than the default one in Vim (and thus AutoComplPop's one) and can be set to not perform autocompletion.
I'd suggest you remove wordcomplete from your config, disable OmniCPPComplete's "may complete" feature and let AutoComplPop deal with the "autocompletion" side of the problem.

Vimrc settings to emulate Sublime's parenthesis auto-completion

How do I emulate Sublime text's auto complete behavior for curly braces {} on vim? Basically, when a parenthesis is opened, it should auto close in the same line, and when <CR> is pressed the cursor should go to the next line with a block indentation and } should fall in line with the original indention of the line containing the {. If my question is not clear, this is the default behavior of most code editors when dealing with {}.
The Automatically append closing characters page on the Vim Tips Wiki has everything from simplistic mappings to complete plugin solutions. There seem to be issues with the latest Vim 7.4 version, though.
There exist many plugins with similar features as Ingo pointed out.
lh-brackets, that I'm maintaining, has the features you describe:
{ inserts {} and moves the cursor in between (and also inserts a placeholder after the closing bracket
hitting <cr> while within a pair of curly-brackets will insert another newline in-between (and indent correctly)

gvim: continuous lines at the beginning of the lines [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is vim drawing underlines on the place of tabs and how to avoid this?
Im getting continuous line when I write spaces or tabs at the beginning of the lines:
http://www.freeimagehosting.net/68r69
Any idea?
Javi
This isn't the result of using tabs at the beginning of a line, it's just vim's code-highlighting.
Vim underlines links, and you're still within the body of an <a> tag.It's nothing to worry about, it just shows where the content of the displayable part of the link starts and ends.
You could disable syntax highlighting for HTML, but I wouldn't recommend it.
The text inside a HTML hyperlink in underlined by Vim's syntax highlighting to show that it is a link. In this case the whitespace surrounding the tag is being highlighted.
Switching off such underlining is addressed in this question and boils down to setting
:let html_no_rendering=1
which you could put in your .vimrc.
It's a flaw in the syntax definition; I've posted a fix here; I'll send this as a patch to the maintainer now, as it seems to be a constant source of confusion.

Resources