I am trying to use to remap the emmet-vim functions, but I have some problems :
using :
map <c-e>e <plug>(EmmetExpandAbbr)
nmap <c-e>e <plug>(EmmetExpandAbbr)
or
map! <c-e>e <plug>(EmmetExpandAbbr)
nothing appens in any mode,
and using :
imap <c-e>e <plug>(EmmetExpandAbbr)
then comes a weird problem :
first, doing the sequence e in editmode,
div_
( '_' = cursor position)
becomes :
div<Plug>(EmmetExpandAbbr)_
And moreover, strange thing, the original map <c-y>, give the same result...
I heard that the map using is a better way to permit the user to custom the shortcuts rather than using global variables... So how to do this with the emmet plug-in (I already succeed to custom shortcuts with other plugins)?
Thank you !
Related
Note: I'm currently using Neovim v0.2.2 (But I believe this shouldn't change anything related this post)
I'm currently attempting to create a function within vim that allows for easily replacing text.
I understand I can create a shortcuts and macros and all that, but ideally I just want to give 2 args, and not think about what specifics go where as this can interupt my thought process.
So I decided to just have a simple wrapper disguised as a function (Which I will create a command wrapper for as well, once I figure out what I did wrong here)
function! VisualReplace(query, replacement)
" Example = '<,'>s/query\%V/replacement/g
'<,'>s/a:query\%V/a:replacement/g
endfunction
As you can see, it's a very simple function that just applies the args in it's respective position, Yet, this fails even when called as a function using : call VisualReplace('some_query', 'some_replacement'
Alternatively, if you simply use the Example I have commented out directly, there's no issue, So I was hoping someoen could enlighten me on a potential fix
If need be, I could possibly look into string building & build it incrementally
Error msg:
Pattern not found: a:query\%V
General theory
Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R>= to insert variable contents:
:sleep <C-R>=timetowait<CR>m<CR>
... but in a script, :execute must be used. All the literal parts of the Ex command must be quoted (single or double quotes), and then concatenated with the variables:
execute 'sleep' timetowait . 'm'
Your function
In order to get the a:query and a:replacement arguments into :substitute, use :execute and either string concatenation or printf():
function! VisualReplace(query, replacement)
execute "'<,'>s/" . a:query . '\%V/' . a:replacement . '/g'
endfunction
Additional critique
Passing a range to a function is so common, there's special syntactic sugar for it: The range attribute to :function, and a:firstline and a:lastline implicit arguments. Read more about it at :help function-range-example. While your use case here seems to be specifically for visual mode, in general it's useful to keep the scope of functions as broad as possible.
#Ingo Karkat answered perfectly. However, I feel like there might be some workflow alternatives which might help. (Assuming you aren't trying to script this behavior)
Visual Star
It looks like you are build a search based on a visual section. You may want to consider using a visual-star plugin to simplify the process. Here is a an example of a visual star mapping:
xnoremap * :<c-u>let #/=#"<cr>gvy:let [#/,#"]=[#",#/]<cr>/\V<c-r>=substitute(escape(#/,'/\'),'\n','\\n','g')<cr><cr>
This mapping will allow you to visually select text and then execute * to make it a search pattern. Similar to how * works in normal mode on the current word.
Search refining
I get the impression that you are trying to refine your search pattern. Vim has a nice way of doing this with q/ or pressing <c-f> while searching with /. See :h q/. This will bring up the command-line window which will allow you to edit the query/command-line with all your normal Vim keys.
Search and Replace with gn motion
Sometimes doing a substitution is just overkill or doesn't quite fit the situation right. You can mimic a search and replace by using the gn motion to operate on a search pattern. By using an operator and the gn motion together you can use the dot command, ., to repeat the action easily.
Example:
/foo
cgnbar<esc>
Now you can use . to repeat the foo -> bar replacement. Use n to skip. You can use other operators as well, e.g. gU to uppercase.
See :h gn and :h operator for more help.
Related Vimcasts episodes:
Refining search patterns with the command-line window
Operating on search matches using gn
Search for the selected text
Currently, I'm attempting to organize some keyboard shortcuts for IdeaVim v0.48. I've run into a problem when I try to map shortcut "leader + ev" to an Intellij IDE action "ExtractVariable."
There is no official IDE action listed for extracting a variable, strangely enough, so I'm trying to map it to a function key. Here's what I put into my .ideavimrc:
set <F15> = ^[,
map <F15> <leader>ev
where my IDE shortcut for extracting a variable is "Ctrl + Alt + ,"
I've followed multiple guides on this, and still don't understand how to do it. Any ideas?
Answering my own question for others as the IJ Idea documentation is a little unclear:
The IDE action for "Extract Variable" is actually called "IntroduceVariable" if you want to call it from command mode (all other variable extraction actions begin with "Introduce" also). So to map this to a shortcut in IdeaVim, simply put this in your .ideavimrc:
map <what you want to use> :action IntroduceVariable<CR>
I'm trying to duplicate the functionality in vim of IDEs like IntelliJ that do the following, letting | be the cursor:
I type an open paren: (
It automatically closes it and puts my cursor in the middle: (|)
I type some text in the middle: (Some text here|)
I type a close paren, and rather than inserting a close paren, it simply moves my cursor over the already inserted one: (some text here)|
I have the following set of remappings that duplicate the functionality of 1-3 and when I just type ():
inoremap ( ()<Esc>i
inoremap () ()<Esc>i
It seems I need a way to detect if the character after my cursor is a ) in order to make a function to do what I want in 4. Does vim support this?
There are many pitfalls to implement this functionality properly; several plugins have attempted to do this. At least you should check them out to learn what they did; maybe one of them already fulfills all of your requirements! You'll find a complete discussion of implementation details, and a list of plugins at Automatically append closing characters.
I don't believe you can do this through a simple map since you would need to incorporate some conditional logic to decide when and when not to add the closing parenthesis. Instead, you would have to define a function to do the heavy lifting and then map the closing parenthesis to call the function. Vim's help has some information about functions, but I've found this page to be an invaluable resource: http://learnvimscriptthehardway.stevelosh.com/
That said, there are number of vim plugins out there that already perform this type of functionality. The one that I use is called "auto-pairs" and can be found here: https://github.com/jiangmiao/auto-pairs. As a bonus, you can see the code if you're actually interested in how it works as opposed to just being interested in having the functionality itself.
Try this code
inoremap <expr> ) getline('.')[col('.') -1] == ')' ? "\<Right>" : ')'
But I recommend to use plugin, because such settings make your vimrc in confusion.
I like vim-smartchr and vim-smartinput
https://github.com/kana/vim-smartchr
https://github.com/kana/vim-smartinput
I am using the kana / vim-textobj-user for defining some custom user objects but the problem is I can't jump over them : case in point
let's say I am using the same indent text object which is mapped by ai and ii
I want to jump around the text in normal mode something like ]i and [i
currently I am using a very hacky way of selecting and exiting visual mode
So is there a simple way to do that and have some kind of mappings for all the other user text-objects as well .
Something like ]{text-object}
I am using the kana / vim-textobj-user for defining some custom user objects
[...]
let's say I am using the same indent text object which is mapped by ai and ii
I want to jump around the text in normal mode something like ]i and [i
Vim has a bunch of built-in commands like ]m, [M, etc. So I thought you meant ]i/[i to move the cursor to the next/previous text object. If so, vim-textobj-user supports both selecting and moving to a text object since its first release. But it's not automatic. At least you have to declare what keys (such as ]i/[i) to be used for the commands.
But I wonder about the following sesntence:
currently I am using a very hacky way of selecting and exiting visual mode
So you typed like vaio<Esc> and vai<Esc>? What you want to do is to move the cursor to the first/last line of the text object under the cursor? If so, vim-textobj-user currently doesn't provide API to define such commands.
In this case, it is probably possible to automate defining key mappings like nmap ]i vai<Esc>. But it seems to be fragile and overrides several built-in commands.
Text objects are only for applying a command (e.g. gU) or visually selecting an area of text. Motions over / to the next occurrence are highly related, but different commands. I think the vim-textobj-user plugin only provides the former, but not the latter.
My CountJump plugin is quite similar, and provides commands to set up both text objects and jumps based on regular expressions.
Is it possible to map over the 'n' and 'N' keys without overriding the time they're used for skimming search results?
Basically you could be searching for something any given time, so remapping n in normal mode without modifications would override the skimming.
After a little considering, if you always quit your searchings with <C-l> (clearing the highlights, I'm not sure if that is the default), you could indeed do something like this with a few extra helper functions:
/,?,*:
--> set s:searching = true
and do the builtin things
<C-l>:
--> set s:searching = false
and do the builtin things
n,N:
--> if s:searching then use the builtin n
else activate your own functionality
Yes you can map a key in a certain mode. You could read over the documentation for map for all of the available options to fine tune your mappings.
Specifically, the map-overview for a list of the mappings and what mode they are used in.