I want to map the F2 button to manage the date in vim.
I'm using vim snippets so I want to be able to write down the date while a certain text is selected, I came up with this:
vmap <F2> :s/currentdate/\=strftime("%d-%m-%Y")/<CR>.
It works fine but, here is the problem, I also want to be able to update the time:
vmap <F2> :s/..-..-....\|currentdate/\=strftime("%d-%m-%Y")/<CR>.
It doesn't work so I tested it directly on vim, without the F2 mapping and my command (when text is selected):
:'<,'>s/..-..-....\|currentdate/\=strftime("%d-%m-%Y")/ works fine.
It seems like the map command make the use of the 'OR' (\|) impossible.
Also, if there's another way to update the date, I'd be interested too.
Thanks in advance.
So thanks to #yolenoyer we have the solution (escape the backslash)
vmap <F2> :s/..-..-....\\|currentdate/\=strftime("%d-%m-%Y")/<CR>
Don't know why we have to escape it but it works.
Related
I am using easymotion plugin (https://github.com/easymotion/vim-easymotion) with vim.
If you use f/F motions in VIM (with easymotion plugin), easy motion highlights all possible positions when there are multiple matches, that way you can easily jump to the position you want.
But it doesn't work with y/c/d commands, how can I achive that ?
I have provided an example below for clarification:
This is some line.
Say I am working on the above line in vim and the cursor is at the i in "This". If I do "yfs" in vim, I would like easy motion to mark the three "s"s present to the right of the cursor. That way, I can easily yank/change/delete upto the s I want.
Thanks in advance !
You can, use something like this in your .vimrc:
" Find next occurence of a char using easymotion
map <Leader>f <Plug>(easymotion-bd-f)
nmap <Leader>f <Plug>(easymotion-overwin-f)
Now you can do something like y<Leader>fs and it will highlight the three s-characters. Selecting one will then yank from your cursor's position to that character.
If this does not work
This means that there are other key bindings that are using the same combination. You can check this with :map and then look for the key combination you are trying to map to the easy-motion. Removing that keybinding from your .vimrc or removing the plugin that created the binding should solve the problem.
If it's the YankRing plugin that's hijacking the y/c/d keystrokes, you can add the following to your vimrc to prevent it from doing that (Check :h yankring for more info):
let g:yankring_zap_keys = ''
I am writing a vim script which may need to map the key to trigger a function, but it looks like whatever I write in the imap command, the mapping is not worked.
For testing purpose , I set map the to like:
imap <Space> <Esc>
So I should escape from the insert mode when I press a space.But it seems not working... Is it possible to imap in vim? If not, I will find another way to work around.
It's so strange to answer my own question , but I found a solution after all. mapping-enter-key-in-vim In this question, the answer is
inoremap <buffer> <Space> <esc>
Another problem is that AutoPairs is overriding setting.... So my setting is not working.
During the implementation process of a program I generally insert many append code lines, mainly with print command, to help me understand and debug the implemented program. Unfortunately, it is common to me to forget which lines are from the code and with were appended and should be deleted some time after. This problem gets worst with large programs.
Well, I found this article that teaches how to keep one arbitrary user selected line highlighted (see section: Highlighting that stays after cursor moves). The solution given by the article is to include in .vimrc the following code:
:nnoremap <silent> <Leader>l ml:execute 'match Search /\%'.line('.').'l/'<CR>
So, every time when I press \l the current line is highlighted and kept so, and the previous highlighted line, if there are one, is unhighlighted.
This isn't the behavior that I would like. Instead, I would like to be able to highlight as many arbitrary lines as I want without unhighlighting the previous highlighted lines. And if it is possible, with a unique command like \l.
Someone knows a solution for this?
Thanks in advance.
EDITED:
The command proposed by yolenoyer solved the initial problem. But, now other problem raised. The following command:
:call clearmatches()
proposed to clean the highlighted lines cleans all lines and I would like to be able to clean specific highlighted lines, instead off all of them at once. Is it possible?
I program in C quite alot, and when debugging tend to pepper the code with debug prints.
I use the vim command
:syntax match Error /\<debug_printf\>/
to ensure the word 'debug_printf' is highlighted in the default 'Error' colors for the particular colorscheme.
This doesn't help you bookmarking a series of lines, but for that you should check out the 'bookmark' plugin which allows you to create and remove bookmarks throughout the file.
VIM Bookmarks Plugin
:match accepts only one match.
Use the matchadd({highlight-group}, {pattern}) function instead, for example:
nnoremap <silent> <leader>l :call matchadd('Search', '\%'.line('.').'l')<cr>
To clear the matches you added, run :call clearmatches().
I used the answers here to come up with this combo, which I think is nice:
" \l to highlight a line
nnoremap <silent> <leader>l :call matchadd('Search', '\%'.line('.').'l')<CR>
" \L to remove highlighted line
nnoremap <silent> <leader>L :
\for m in filter(getmatches(), { i, v -> has_key(l:v, 'pattern') && l:v.pattern is? '\%'.line('.').'l'} )
\<BAR> :call matchdelete(m.id)
\<BAR> :endfor<CR>
I think your first paragraph, which explains your problem, has nothing to do with vim, so maybe you don't need to use vim to solve your problem.
What about not debugging with regular print statements, but with a function that wraps print? That would be really easy to search for program wide and also file wide (just search with * or # for all occurrences of your debug printing function).
I like to use hlsearch but I hate to keep everything highlighted after searching, to solve this problem I could simply use :nohlsearch or an abbreviation of it but that is still to much effort so I decided to try to do that on pressing escape. What I came up with is:
nnoremap <ESC> :nohlsearch<CR>
This works exactly as I want it to in GVim which I usually use for development but it does not work in vim.
If I search something in vim, press escape to deactivate the highlighting and use one of the arrow keys to navigate vim goes directly into insert mode and inserts a character on a new line.
As I never really came around to using h, j, k and l for navigation this is really annoying and I would like to know how I can make vim behave like gvim.
If you need more information you can find my entire vim configuration here.
Your problem is that when you press <Up> terminal sends something like <Esc>OA (you will see it if you type <C-v><Up> in insert mode) which is remapped to :nohlsearch<CR>OA. I do not know any solution except not mapping a single <Esc>, try either mapping to double <Esc>.
I created this map to disable search when press double <Esc>
nnoremap <silent> <Esc><Esc> :let #/ = ""<CR>
is :noh still too much work?
EDIT: I don't know about you, but I personally think :noh is easier than pressing Esc key, since I can press all the buttons without stretching my pinky too far (which is why I think the default mapping of Esc for going back to Command Mode from Insert Mode is a bit unfortunate). If you really use the :nohlsearch that much, you probably should remap it to something you can reach from the Home Area (i.e. regular letters, or numbers, or perhaps Ctrl-letters).
Anyway, typing the exact command you give works in my vim (on gnome-terminal). Are you sure you put the rule in .vimrc file, instead of .gvimrc? No luck after restarting vim? Try :source /path/to/config/file and see if that makes it to work.
I get the following 10X times a day by accident.
Entering Ex mode. Type "visual" to go to Normal mode.
How can you disable the combo which causes it in Vim?
<Nop> is meant for use in mapping keys to "nothing". See :h <Nop>.
:map Q <Nop>
Or put it in your ~/.vimrc:
map Q <Nop>
.
This answer is based on #NielsBom's comment 4. October 2012 and on #BrianCarper's answer 13. August 2009.
I think NielsBom is completely right, please see the article.
The command map is really evil in Vim and has caused me a lot of problems during years.
I did not realize the thing before NielsBom's comment. So please use the following command instead:
:nnoremap Q <Nop>
The "combo" is Q. To disable it, simply map Q to something else:
:map Q <whatever>
I use gq, which is used to format text.
If you don't want it do do anything map it to <Nop>:
:map Q <Nop>
If you don't want to map it to something else, just use :unmap. If you do have something else in mind, :map will work - take a look at the help pages to see the variations to specify what modes the map will be used in.
Another, albeit slightly more extreme option, would be to switch to https://neovim.io, which has removed ex-mode entirely, due to no one ever actually using it for anything ever.