I learned that using this:
let mapleader=','
if exists(":Tabularize")
nmap <Leader>a= :Tabularize /=<CR>
vmap <Leader>a= :Tabularize /=<CR>
endif
would give me a shortcut to tabularize with the '=' char. But I'd like to generalize it, so that I could use some shortcut like:
<Leader>a$
<Leader>a*
And it would read the '$' or '*' char and use it as the "char to tabularize". I.e., pass this char to the :Tabularize /CHAR function
Any ideas?
If you put that if block in your vimrc it will never work, because vimrc is sourced before any plugins, so the :Tabularize command doesn't exist yet when the expresion exists(':Tabularize') is evaluated and it will always be false.
You could use these mappings:
nnoremap <Leader>a :Tabularize /
vnoremap <Leader>a :Tabularize /
So when you press ,a* you'll be left in in command line mode with :Tabularize /*, ready to press Enter.
Related
I manually replace ^M with
:%s/<ctrl-v><ctrl-m>//g after I open a log file. I have to do this several times a day so I tried to assign F11 key to perform this action.
I tried the following in .vimrc but it did not work.
:nnoremap <silent> <F11> :let _s=#/ <Bar> :%s/^M//e <Bar> :let #/=_s <Bar> :nohl <Bar> :unlet _s <CR>
Note: I pressed ctrl+vctrl+m to get ^M in the above statement.
I have a similar assignment for F10 to remove EOL whitespace and that works.
:nnoremap <silent> <F10> :let _s=#/ <Bar> :%s/\s\+$//e <Bar> :let #/=_s <Bar> :nohl <Bar> :unlet _s <CR>
What am I doing wrong here?
Check that your terminal actually forwards F11 to the commands it’s running instead of listening to the key itself. For instance, the default configuration of Windows Terminal intercepts F11 and does not forward it, so you can’t rebind the key inside Vim.
Furthermore, your command looks quite convoluted; my Vimscript knowledge is limited but if the purpose of your command is to change Windows file endings into UNIX file endings, the following, simpler command works:
:nnoremap <silent> <F11> :set fileformat=unix<CR>
There’s also a widely installed utility program, dos2unix, that does the same, and which might be even simpler for your use-case.
I would like Vim to place my cursor in the middle of screen after search. I have achieved for *, #, n and N commands with the following lines in .vimrc
nmap * *zz
nmap # #zz
nmap n nzz
nmap N Nzz
My question is: how to map / and ? the same way? I.e. I would like to position cursor after some text has been found using
/some-text-to-find-forward
?some-text-to-find-backward
Edit: Threw away my initial answer as it was too much of a kludge. Here's a much better solution.
function! CenterSearch()
let cmdtype = getcmdtype()
if cmdtype == '/' || cmdtype == '?'
return "\<enter>zz"
endif
return "\<enter>"
endfunction
cnoremap <silent> <expr> <enter> CenterSearch()
The way this works is to remap Enter in command-line-mode to a custom expression.
The function performs the current search followed by zz if the command-line is currently in a search. Otherwise it just executes whatever command was being done.
It's not very pretty, but
:nnoremap / :execute "normal! /\<lt>cr>zz"<c-left><right>
will get the job done. (Puts an :execute "normal! /" command on the commandline, then adds a <cr>zz to the end to it so that you automatically zz when you issue the command. The final <c-left><right> just steps into the search pattern at the right spot
Solution from Randy Morris but as a oneliner:
cnoremap <silent><expr> <enter> index(['/', '?'], getcmdtype()) >= 0 ? '<enter>zz' : '<enter>'
I want to construct a command line within a mapping, using the :execute normal "commandstring" techinique. But I can't do it from within a mapping as vim immediately interprets the "" as a keypress. So this doesn't work:
nnoremap <leader>jj :execute "normal :tabnew foo.txt\<cr>"<cr>
I tried doubly escaping the backslash, to no effect:
nnoremap <leader>jj :execute "normal :tabnew foo.txt\\<cr>"<cr>
Now vim types in the backslash and then interprets the as a keypress.
Any way out of this?
That's indeed tricky. You have to escape the < character as <lt>, so that the <cr> is not parsed as special key notation:
nnoremap <leader>jj :execute "normal :tabnew foo.txt\<lt>cr>"<cr>
Note that your example doesn't need any of this; :normal :...<CR> is the same as ...
nnoremap <leader>jj :tabnew foo.txt<cr>
I am trying to set some key mapping/macros to comment/uncomment blocks of text in my .vimrc but I can't manage to make it work.
I define some variables:
let g:comment_id='~'
autocmd BufRead,BufNewFile *.c,*.cpp,*.cxx,*.h,*.hpp,*.hxx,*.ipp let b:comment_open='//'
autocmd BufRead,BufNewFile *.f90,*.F90 let b:comment_open='!'
And then I try differents ways to use them but everytime it fails:
noremap <silent> ,cc :s/^/<C-R>=(b:comment_open.g:comment_id)/<CR>:nohlsearch<CR>
noremap <silent> ,uc :s/<C-R>=(b:comment_open.g:comment_id)//<CR>:nohlsearch<CR>
->
E15: Invalid expression: /,
E15: Invalid expression: (b:comment_open.g:comment_id)//
noremap <silent> ,cc :s/^/<C-R>=(b:comment_open.g:comment_id)<CR>/<CR>:nohlsearch<CR>
noremap <silent> ,uc :s/<C-R>=(b:comment_open.g:comment_id)<CR>//<CR>:nohlsearch<CR>
->
E488: Trailing characters
noremap <silent> ,cc :s/^/\=(b:comment_open.g:comment_id)/<CR>:nohlsearch<CR>
noremap <silent> ,uc :s/\=(b:comment_open.g:comment_id)//<CR>:nohlsearch<CR>
->
cc: ok,
uc:
E64: \= follows nothing,
E476: Invalid command
I fact I can't understand how 'C-R' and \= work and mean...
Thanks for your help
Use:
nnoremap <silent> ,cc :s/^/\=b:comment_open.g:comment_id/<CR>:nohlsearch<CR>
nnoremap <silent> ,uc :s#\V<c-r>=escape(b:comment_open.g:comment_id,'\#')<cr>##<cr>:nohlsearch<cr>
Reference: :help sub-replace-expression
It is necessary in the pattern to turn verynomagic on to escape all special characters, and even with that you need to escape both the delimiter and the backslash.
In vim I have this nmap
nmap <silent> ,mu : marks ABCDEFGHIJKLMNOPQRSTUVWXYZ<CR>
If I don´t have Upper marks and try ,mu I get
E283: No marks matching "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
why don't show the Error output ?
Try
nnoremap <silent> ,mu :execute "try\nmarks ABCDEFGHIJKLMNOPQRSTUVWXYZ\ncatch /\\V\\^Vim(marks):E283:/\nendtry"<CR>
By the way, is there a reason for writing :nmap instead of :nnoremap? You should not do this if you don't have a reason unless you want to run in the situation where you can't predict what will be the result of adding another mapping (directly to vimrc or by installing a plugin).
Edit (sehe)
To make things more readable, I'd suggest using a snippet like this in your $MYVIMRC:
function! ShowGlobalMarks()
try
marks ABCDEFGHIJKLMNOPQRSTUVWXYZ
catch /E283:/
endtry
endfu
nnoremap <silent> ,mu :call ShowGlobalMarks()<CR>