vim: conflict between mapping quote - vim

I remapped single and double quotes to the following:
nnoremap " ci"
nnoremap ' ci'
This makes editing strings easier. However, before I had defined this alias to reopen the last modified vim file. But this no more works
alias vil='vim -c "normal '\''0"'
How can I resolve this or any other mapping that works without conflicts

Use normal! to avoid mappings. See :h :normal

Related

How to put current filename in vim commands in .vimrc

i want to create a shortcut to run typescript in vim how can i access the filename in .vimrc
i want to run short scripts.
nmap <leader>r :! ts-node current-file<return>
First try %, as this gets substituted by the file name (as explained in :help :!):
nmap <Leader>r :! ts-node %<CR>
But if the file name contains whitespaces or special characters, you might see some errors. In that case, you need to escape the file name as follows:
nmap <Leader>r :execute '!ts-node ' . shellescape(expand("%"))<CR>
You're strongly adviced to read :h cmdline.txt, section 6 :h cmdline-special from top down to bottom.
nnoremap <leader>r :!ts-node %:S<CR>

What is wrong with this line? nnoremap <C-[> :execute "normal! A{{{\<Esc>"<CR>

I am trying to add this to my vimrc but I am getting problems one it inserts {{{ at the beginning of my vimrc whenever I open it and also it apparently has 'c' in the regiser as the last pressed key so it deletes the first 2 lines when I press j
And when I run the command it complains that
"A{{{\ is missing a quotation mark and is not a command.
This gave me tip. map execute command vim
You don't need use :execute "normal! ..." if you are using nnoremap. This will work.
nnoremap <C-[> A{{{<Esc><CR>

vi, condition in vimrc

I am trying to personalize my ~/.vimrc file.
Here is what I want: when the file name opened with vi is titi45.tex, and when I press <space> in normal mode, I want that the command make toto45
is executed. And if the file opened with vi is called titi65.tex, I want that
make toto65 is executed, and so on.
I tried to use a command
au FileType tex nmap <space> :w<CR>:!make<CR><CR>
in my .vimrc but I don't know how to match the file name and use the number.
Can you help me?
Mathieu
You are looking for :make %<. BTW why don't you compile within vim? Avoid :!make. Prefer :make, and check the help related to the quickfix mode (:h quickfix).
Your mapping would then be:
nnoremap <buffer> <silent> <space> :update<cr>:make %<<cr>
(the <buffer> part is very important, it makes sure your mapping won't leak to other filetypes; the other are not critical here, but good practices)
EDIT: Sorry I missed the exact requirement.
Then, you'll have to transform the target name. You'll to play with substitute() and :exe. But your substitution won't be a simple substitution. It looks like a rotating substitution. Solutions for this kind of substitution have been described over there: How do I substitute from a list of strings in VIM?
And IIIRC, there exist a plugin that does similar things.
In your case, I guess I would use a dictionary to define how names are substituted:
let k_subs = { 'toto': 'titi', 'titi': 'toto' }
nnoremap <buffer> <silent> <space> :update<cr>:exe 'make '.substitute(expand('%'), join(keys(k_subs), '\|'), '\=k_subs[submatch(0)]', '')cr>
NB: I haven't tested it.
If you want to get rid of the extension, it'd better be done in expand() argument.
Hum...
finally i use an additionnal script
#!/bin/bash
maRegex='source_(enonce|corrige)([0-9]+)$'
if [[ "${1}" =~ $maRegex ]]
then
commande="make enonce${BASH_REMATCH[2]}"
else
commande="make plouf"
fi
echo commande de compilation lancée: $commande
$commande
This script in launched by vimrc.

Error when loading .vimrc containing substitution

How do I map a substitution to a key in my .vimrc without getting an error when I source it?
I'm trying to add
nnoremap <leader>re :'<,'>s/<%=*\s//g | '<,'>s/\s-*%>//g
to my .vimrc but when I do so and reload the file I get the following error:
The problem is that you're combining two :s commands, but the command separator | concludes the :map command, so that the second substitution is executed immediately, causing the error. You need to escape the |, or better use the special <Bar> notation inside mappings:
nnoremap <leader>re :'<,'>s/<%=*\s//g <Bar> '<,'>s/\s-*%>//g
PS: Wouldn't it be more natural to define the mapping in visual mode (as it works on the last selection, anyway)? With a :vmap, the first '<,'> range will be inserted automatically:
xnoremap <leader>re :s/<%=*\s//g <Bar> '<,'>s/\s-*%>//g

I want to update the file (if necessary) and run the system command on current file

I realize that I can :nmap <leader>rc :!cat %<CR> to provide an easy set of triggers, but I would like to do this instead.
nmap <leader>rc :up :!cat %<CR> but it complains about needing only one filename. How do I get vim to recognize both commands, in series?
You are missing a <CR> after :up. <CR> tells vim you want a carriage return here.
nmap <leader>rc :up<CR> :!cat %<CR>
The reason up is complaining about multiple file names is that it sees :!cat and %<CR> as two arguments to up.
So the new macro executes
:up
:!cat %
instead of
:up :!cat %
(Side Note: you should probably use nnoremap instead of nmap)
ZyX recommends using the following mapping instead.
nnoremap ,rc :up\|execute "!cat" shellescape(#%, 1)<CR>
This uses | to separate commands and escapes the %. Escaping the % leads to a more robust mapping just incase the filename contains special characters.
Help for :h execute and :h shellescape

Resources