I have remapped quite a few things so far in Neovim. I like to use f with , and ; to navigate to a specific character. But, I am trying to experiment with ; being a leader key and I wanted . to take its place. This will allow me to use f with , and . to navigate to the desired character. But, noremap . ; doesn't change the behavior of .. , and ; continue to work with f instead of , and ..
I expected to be able to use f and , to navigate backwards and f and . to navigate forwards.
,.; continue to behave normally despite this being in my init.vim file
noremap???? Since you said you have remapped quite a few things in Neovim. I assume that was a typo and what you actually meant was nnoremap there.
You must know the dangers of mapping . to ; first, which will make the normal behaviour of . to be lost (i.e. you will not be able to use the "repeat the last command with .") unless you map something else to . as well.
First step to your problem would be mapping with . to ;, which means whenever you type . in normal mode, it will behave as if you had actually typed ;(functionality of ; command in normal mode will be copied to . character) :-
nnoremap . ;
(This works for me, I use Neovim as well, It HAS to be the typo.)
, and ; continue to work with f instead of , and .
At this point, . should be able to use . as ;, but since you haven't mapped ; to anything at this point, ; will functoin as before. Now, The second step would be mapping ; as leader key. Which will make ; lose its normal functionality, and you will not be able to use ; to jump to the next search character.
let mapleader = ";"
Put the code above in your init.vim to make ; you leader key.
Related
I'm new to vim and I learned f key can be repeated by ; key today.
However I've already use ; key as Leader.
Is there any way to use . key instead?
Sure, you can map . to do the job of ;:
nnoremap . ;
However, then you lose the function of . (repeat the last change), unless you remap that.
Risking to sound preachy, but it will be long-term much better for you to adapt to Vim, than to try to adapt Vim to you — at least until you are no longer a beginner. In this case, for example, I'd say . (repeat last command) is a much more important function than ; (repeat last f-search) is, especially since leader can be \ as is the default, or Space, as keys with much less to lose.
For example, I have text like this:
I talked to a friend, I hiked a mountain, I am working with blah blah,
And I want to delete or substitute I hiked a mountain. Why doesn't ci, or di, work? Is there a way to grab, substitute, or delete between commas?
there are plugins to let you create customized text object. without installing plugins, assume your cursor is between two commas, you could do:
T,c,
or
t,c,
to simulate your ci, for di, change the above c into d
related help doc:
:h t
:h T
:h ,
There are a bunch of plugins that help you create custom text-objects like textobjectify.
I've had the following snippet in my vimrc for quite a while and it never failed me:
for char in [ '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '%' ]
execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
endfor
It creates all the custom operator-pending mappings required for di,, vi%, ci:, ya| and so on.
And it's easy to add new delimiters.
And I have cute smileys in my vimrc.
If you are in a section like that you could use T,ct,.
Or you if you want to install a plugin like vim-argumentative allows you to use ci, and di,
If you're willing to patch Vim for this feature, there was a proposal a while back (including a mostly working patch) to allow you to match arbitrary characters. In your example it would be cim, for "change inside matched comma". See https://groups.google.com/d/topic/vim_dev/pZxLAAXxk0M/discussion for the discussion and a few versions of the patch. I've been using it a long time and the only problem I've encountered is in visual mode; but then again, I don't use the feature as often as I expected to.
Using targets.vim:
da, to delete complete sentence and leaving one comma.
ci, to change inside the commas.
After using the f{char} motion, is it possible to change directions so ; will go backwards (as if you used F{char})? (And vice-versa and with tT.)
I'm going to try using , as LocalLeader and I have this to let me access , again:
let maplocalleader = ','
nnoremap <LocalLeader><LocalLeader> <LocalLeader>
But I wonder if I can map <LocalLeader>; to make ; switch between acting like ; and ,. I don't see anywhere the character for f is stored, so can I do this?
No, this cannot be done, unless you re-implement the f command yourself (which isn't so difficult as it sounds; there are actually plugins that enhance it to search in subsequent lines, too). It can be done for n / N (via v:searchforward), though.
In general, I would advise from employing such clever "tricks"; they retrain your muscle memory, and cause problems in other environments with vi-like keybindings (Pentadactyl, Bash, readline, some IDEs). Rather, rethink your approach to mappings.
I'm running a bit of code in Vim and having a problem (I think) with not being in the correct mode. I'm trying to get the text between either '< >' or '[ ]' into a register. At point code starts below I know the cursor is between a pair of brackets, but I don't know what kind the brackets are:
" put text in x reg if I'm in <> brackets
normal! vi<"xy
" put in x reg if I'm in [] brackets
if len(#x) < 7
normal! vi["xy
endif
[. .. . more code follows making use of
value in x register]
If I'm clicking between <> brackets the rest of the code works okay, alhough I may not be in normal mode. If I'm clicking between the [] brackets then it doesn't work at all, and the literal text ["xy gets inserted at cursor position.
I've tried a couple different workarounds, e.g., trying to put into the keystrings (or a separate normal command) or using 'visual!' command after each visual part. Can't get things going with and the Visual! command avoids the character inserts on second part but but also seems to screw up getting the visual match into the x register.
I'm a little confused and not sure what mode I'm in exactly at all parts of script: normal, visual, or insert. I run through things manually and it seems to work fine but if I do it programmatically it behaves unexpectedly.
Is there some simple thing I'm missing? I could do what I want another way, but this method (at first look) seemed like easiest and most straightforward. Is it?
Thanks for any help.
Okay, I figured out the problem. The mode was left in visual mode when first inner bracket command found no match. Needed to have an extra 'v' to bring back to normal mode before pressing the keys again. Like this:
" put text in x reg if I'm in <> brackets
normal! vi<"xy
" put in x reg if I'm in [] brackets
if len(#x) < 7
" EXTRA V IN LINE BELOW
normal! vvi["xy
endif
[. .. . more code follows making use of
value in x register]
I'm writing a script in which I want to control searches programmatically, and get them highlighted. The search() function results are not highlighted (I think), so using that function is not of use to me.
What I want to do is use the 'normal /' command to search for a variable, but that doesn't seem to be straightforward. I can script the command:
execute 'normal /' . my_variable . '\<CR>'
(or other variations as suggested in the vim tip here: http://vim.wikia.com/wiki/Using_normal_command_in_a_script_for_searching )
but it doesn't do anything. I can see the correct search term down in the command line after execution of the script line, but focus is in the document, the search register has not been altered, and the cursor has not done any search. (It seems as though the < CR > isn't getting entered, although no error is thrown -- and yes, I have tried using the literal ^M too.)
I can at least control the search register by doing this:
execute 'let #/ ="' . a:term .'"'
and then the obvious thing seems to be to do a:
normal n
But that 'normal n' doesn't do anything if I run it in a script. Setting the search register does work, if I manually press 'n' after the scrip terminates the search happens (and highlighting appears, since hlsearch is on). I don't even care if the cursor is positioned, I just want the register pattern to be highlighted. But various combinations of 'set hlsearch' in the script don't work either.
I know I could use 'match()', but I want to get it working with regular search highlighting, and I wonder what I'm doing wrong. It must be something simple but I'm not seeing it. Thanks for any help.
run:
let #/ = a:searchStr
from inside your function then run
normal n
from outside your function (inside it does nothing) eg.
command -nargs=* Hs call MySearch() | normal n
or you can use:
set hlsearch
instead of normal n if you don't want the cursor to move
(I cannot work out another way of doing this without having something outside the function.)
If your script is using functions, then this quote from :help function-search-undo is relevant:
The last used search pattern and the redo command "."
will not be changed by the function. This also
implies that the effect of :nohlsearch is undone
when the function returns.
Vim usually tries to reset the search pattern (and a few other things) when a function ends, often you can get around this by adding the n (next search) to the end of a mapping, or using :map <expr> and having your function return the key sequence to be executed.
On closer inspection, it seems \<CR> is not picked up inside single quotes. Try using this instead:
execute 'normal /' . my_variable . "\<CR>"