Vim normal editing in Visual line not working as expected? - vim

So this is the first time i am experimenting around visual line editing in vim. I have a couple of words as following
cat
dog
and i want to surround them with brackets (without using surround.vim) for learning. The commands that i use after visual selection is as follows
:'<,'>norm 0i(<esc>$a)
and also
:'<,'>norm 0i(<C-v><esc>$a)
The problem is that vim seems to print out as well and the output i get is
(<esc>$a)cat
(<esc>$a)dog
Any hint to where i might be going wrong will be greatly appreciated.
Also worth noting that i am running vim using vim -u NONE so vimrc file doesn't get loaded.

The :normal command does not parse special characters. As you may have guessed, <esc> is inserted literally as five different characters.
You will need to wrap it in :execute with an escape on those special characters:
:exe "'<,'>norm 0i(\<esc>$a)"
Alternative ways of doing this (using the full buffer range %):
:exe "%norm I(\eA)" - an alternative way of writing the esc character
:%norm I(^[A) - where ^[ is ctrl-v and then esc, to insert a literal esc character which doesn’t need to be parsed.
:%s/.*/(&)

Related

Vim normal! sending literal <Space> string instead of whitespace character

I'm not sure what I'm doing wrong here.
:normal! I###<Space><Esc>
The end result I want when I execute this command over a word is this:
### word
But instead I get this:
###<Space>word
Vim's :normal is a nightmare with anything moderately complicated. I don't remember why, but this is the easiest way to get what you want. You need to escape the opening < in a string passed to execute.
execute "normal! I###\<Space>\<Esc>"
Edit: Vim doesn't really explain why, but :help normal
An alternative is to use |:execute|, which uses an
expression as argument. This allows the use of
printable characters to represent special characters.
Lets first explain the vim command:
:normal! run the rest of the commands as normal mode
I enter insert mode from the beginning of the line <shift> + i
###<Space><Esc> - type out the characters ###<Space><Esc> literally since we are in insert mode (notice that if you just enter insert mode, and type ###<space><esc>, you'll get just that literally.)
When this command finishes, you'll notice how it's still in normal mode. This means that your command can just omit the <space><esc> and enter the space literally:
:norm! I###
(There is an extra space after the last hash)

vim command for adding Parentheses efficiently

what is the vim command can put something into Parentheses efficiently and then I can use . to repeat it? ?
for example, data['max'] to (data['max'])
visually select your text:
viW
change it with the opening parenthesis, followed by the selected text, followed by the closing parenthesis:
c(<C-r><C-o>")<Esc>
If you are confident with text-objects, this can be done in one step:
ciW(<C-r><C-o>")<Esc>
which can be repeated with ..
I'd recommend using two plugins for this. Tim Pope's vim-surround and vim-repeat.
Just follow the links and install those plugins.
Inorder to put parenthesis around a word, just do ysiw)
Otherwise, select text in visual mode, press S(Capital S) and type in paranthesis
You can use a map for that.
:nmap \. I(<ESC>A)
You can put this line in your ~/.vimrc file.
When you press \ and . in normal mode, it will add a opening bracket at the start of the line and closing bracket at the end of the line.
Assuming that you are using a language like Tcl and surround the hash element with a bracket,
You can try this by keeping the cursor anywhere on the hash name,
: nmap \. bi(<Esc>f]li)
This will surround the expected one with circular braces.
You can use a map in vimrc:
xnoremap <leader>a <ESC>`>a)<ESC>`<i(<ESC>
And use <leader> and a to add parentheses efficiently

Auto closing curly quotation marks in Vim

I'd like to set up Vim to auto close the pair of ‘ and ’ (curly quotation marks).
I've tried to configure all the five plugins I could find (two autoclose's, surround, closepairs and delimitmate) but with no success. I don't even seem to be able to remap ‘ at all (with :imap ‘ ‘’<left> or similar).
I use Vim 7.3 on Arch Linux and uim 1.7.0 input method; I insert ‘ and ’ through a shortcut defined in .XCompose. Remapping works just fine for my other compose-key shortcuts, like ¡! or ¿?.
It looks much like a vim bug, in particular, bug with internal vim escape sequences which all start with \x80 (second byte of the character in question is \x80) and encode various things like NULLs, function keys or \x80 byte itself. I guess you can dig into vim source code and find there how this byte is escaped and try to replace last byte of ‘ with this escape code or wait until this will be fixed (though I won't expect fix to come soon: here is quote from todo.txt
UTF-8: mapping a multi-byte key where the second byte is 0x80 doesn't appear
to work. (Tony Mechelynck, 2007 March 2)
So, you see problem is known for four years and is not yet fixed.)
Avoid recursion with
inoremap ' ''<left>
You can achieve this with a small function:
function! CloseQuotes()
normal! i''
startinsert
endfunction
and then map it to ' in the following way:
inoremap ' <ESC>:call CloseQuotes()<CR>
The important thing is the exclamation mark in normal!, which prevents the mapping being recursive.

VIM: how to add search/replace command to vimrc and map to a shortcut

I have two search/replace commands that I find myself running in vim fairly often to clean up html code so I can copy/paste it online. The commands are:
:%s!<!\<!g
:%s!>!\>!g
I wanted a way I could map both of these commands to be run together ... I did some searching for how to use the :map commands in vimrc, however, I can't see how to combine the two lines into a single command that is run with a single keystroke (or a single sequence of strokes).
Thanks!
You can put the commands on a single line separated with a bar.
:%s!<!\<!g|%s!>!\>!g
But you'll have to escape it in the map command
:map <F3> :%s!<!\<!g\|:%s!>!\>!g<CR>
:TOhtml
will create a new buffer containing your previous buffer HTML-ized, including entity escaping (and syntax highlighting, if you had that enabled). See :h TOhtml for more information.
If you are using this search/replace pattern to HTML encode entities, you might want to check out the unimpaired plugin. Amongst other things, this provides shortcuts for encoding and decoding XML, URL and C strings.
:map <F3> :%s!<!\<!<cr>:%s!>!\>!<cr>
of course can be replaced with whatever key you wish
I've not tried it, but can you not put them on the same line separated by "<CR>"?

Issue with smartindent in Vim

In vim with smartindent on:
Press Enter after say an if-statement
Type in {
Press Enter twice
Type in }
If you hit ↑ and go to the previous line, indentation is removed from the blank line.
Even the vim documentation says that:
If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again.
Is there any way to keep this indentation and not have it deleted?
Use Shift+S to start editing on a blank line (from command mode, obviously). This will start your cursor off with the expected level of indentation.
Another doesn't-answer-the-question-as-asked-but-is-a-better-solution-overall:
When typing an opening brace in insert mode, this will insert a matching set of braces
and leave the cursor on a new line in the middle.
:imap { {<CR>}<Esc>O
Similarly, this will auto-insert matching parens and square brackets.
:imap ( ()<Left>
:imap [ []<Left>
(Strip off the leading : when adding to vimrc.)
As I commented on Victor's answer, changing Vim's indentation behavior will leave "empty" lines containing extraneous spaces throughout your files. IMO, this is completely intolerable.
When this happens to me, I sometimes use ddko (or ddO) to delete the line without enough spaces and open a new line with the correct indent. Or, I'll just press A and then Tab enough times to get to the correct indent.
the article here talks about you're very same problem, and what to put in vimrc to fix it.
inoremap <CR> <CR><Space><BS>
nnoremap o o<Space><BS>
nnoremap O O<Space><BS>
I havn't exactly tested this tho.
also the same article links to a shorter alternate solution.
My preferred method is {<CR>}<esc>shift+o as it outpaces {<CR><CR>}<esc>k shift+s by several strokes. I get in a rut with it, though, and end up just using o or O to grab new, properly-indented lines off an empty when I should be using S.
That is, set up your bracing structure and open line-above:
if (true) {
}//cursor here, press shift-o
And you get the indenting you expect.
The open-above trick isn't any fewer keypresses than <up><end><cr>, but with escape remapped and shift being chorded, you can throw it in quite fast.
Also, don't forget your manual indent reset and block-movement. If you're inside a mangled curly brace block, simply use ={ (or =i{ if you're on top of one of the braces). I use that when I have a Good Idea that needs to see text asap, and I don't worry about any formatting frippery until I take a breather.

Resources