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
Related
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/.*/(&)
When I type two left braces {{, Latex-Suite expands them into \left\{ \right\}<++>. I use markers to organize my files, so needless to say this is very vexing. Does Latex-Suite provide a way to type two left braces, or is there a way to temporarily toggle its bindings?
You can disable this function globally by commenting out the line
call IMAP ('{{', '\left\{ <++> \right\}<++>', "tex")
in the main.vim of the vim latex plugin.
In my case, it was line 62 in my ~/.vim/ftplugin/latex-suite/main.vim .
Btw., I have taken this opportunity to also disable the function called when typing $$.
You can also just simply add a imap to do this for you:
inoremap \{{ {{
If you define it this way it won't expand the braces in the imap definition, it will just insert the braces.
You can avoid the triggering of the mapping defined by the Latex-Suite by inserting the second brace literally via <C-v> (often remapped to <C-q> on Windows): {<C-v>{.
With the DelimitMate, it auto generates the closing parentheses. When I'm finished typing inside the parenthesis, what key strokes do I press to quickly go to the right of the closing parenthesis? (Right now I have to manually press ESC then 'a')
The idea of these auto-closing plugins (like the original feature implementation found in IDEs like Eclipse) is that you just type the closing character to go over it. The plugin should detect this situation and instead of inserting the character jumps over the existing, auto-inserted one.
If that's not working for you, there are several plugin alternatives on offer. The Vim Tips Wiki has a list of them.
With delimitMate, Shift-tab will jump out of the current delimiter and Control-G g will get you out of nested delimiters. No need to remap anything.
You could try auto-pairs's Fly Mode
eg:
( hello| world )
press ) at |
( hello world )|
If jump incorrect, use <M-b> to do the back insert.
eg:
(hello| world()
press ) at |
(hello world()|
press <M-b>
(hello)| world()
Repository: https://github.com/jiangmiao/auto-pairs
Plugin: http://www.vim.org/scripts/script.php?script_id=3599
add
let g:AutoPairsFlyMode=1
to .vimrc to turn Fly Mode on
You can do a custom map. I guess you want to go to the right of the closing parenthesis while you're in insert mode. Just add to your .vimrc this mapping:
:inoremap <F8> <ESC>f)a
In this way, while your in insert mode and you've finished to write inside the parenthesis, F8 will bring your cursor ad the right of the closing parenthesis.
If you want you can change the mapped key, using another key instead of F8.
As Kent said in the comment a more general solution would be:
:inoremap <F8> <ESC>%%a
Which will work for [ and { brackets.
I kinda agree with Atropo on this one: if you want to stick with DelimitMate then the least disruption to your workflow might be to make a custom imap to get to the other side of the auto inserted character.
Personally I prefer to have more control over where/when the characters are auto-inserted, and how I can navigate around the auto-inserted characters; UltiSnips or SnipMate does that for me. Maybe they're more what you're looking for.
If you typing on a new line, you try A which will append text at the end of the line.
I have autoClose installed. what I am doing currently is ("I" is cursor)
- (xxxxI)
- ( xxxxI )
- ( xxxxIxx )
- text (xxxxI) other text
- text ( xxxxI ) other text
- text ( xxxxIxx ) other text
I just make a mapping, to <esc>%%a then in above case, the cursor will move to (...)I..whatever
it doesn't work for quotes.
A little late to the party, but note that it can be done easily without any custom mappings. In insert mode, you can press <C-O> (a default mapping) to enter a "one shot" normal mode where you can enter a single normal mode command.
So to answer your question, what you could do is <C-O>a.
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.
Is possible to insert a line break where the cursor is in Vim without entering into insert mode? Here's an example ([x] means cursor is on x):
if (some_condition) {[ ]return; }
Occasionally, I might want to enter some more code. So I'd press i to get into insert mode, press Enter to insert the line break and then delete the extra space. Next, I'd enter normal mode and position the cursor before the closing brace and then do the same thing to get it on its own line.
I've been doing this a while, but there's surely a better way to do it?
For the example you've given, you could use rEnter to replace a single character (the space) with Enter. Then, fspace. to move forward to the next space and repeat the last command.
Depending on your autoindent settings, the above may or may not indent the return statement properly. If not, then use sEnterTabEsc instead to replace the space with a newline, indent the line, and exit insert mode. You would have to replace the second space with a different command so you couldn't use '.' in this case.
A simple mapping to break the line at the cursor by pressing Ctrl+Enter:
:nmap <c-cr> i<cr><Esc>
essentially enters 'insert' mode, inserts a line break and goes back to normal mode.
put it in your .vimrc file for future use.
Here's how to create a macro that inserts a newline at the cursor whenever you press 'g' while not in insert mode:
From within vim, type:
:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]
Where:
[Ctrl+V] means hold the Ctrl key and press 'v'
[Enter] means press the Enter key
[Esc] means press the Esc key
You'll see the following at the bottom of your vim window until you press the final Enter:
:map g i^M^[
Explanation:
[Ctrl+V] means "quote the following character" -- it allows you to embed the newline and escape characters in the command.
So you're mapping the 'g' key to the sequence: i [Enter] [Escape]
This is vim for insert a newline before the cursor, then exit insert mode.
Tweaks:
You can replace the 'g' with any character that's not already linked to a command you use.
Add more to the command, e.g. f}i^M^[O -- This will find the } and insert another newline, then escape from insert mode and Open an empty line for you to enter more code.
You can add the command to your .vimrc or .exrc file to make it permanent. Just omit the colon from the beginning, so the command starts with "map"
Enjoy!
If you're usually expanding a one line block to three lines, try substitution. Change the opening bracket into bracket/return, and the closing bracket into return/bracket.
The command for substituting bracket/return for bracket looks like this:
:s/{/{\r/
Since you want to use this often, you could map the full sequence to an unused keystroke like this:
:map <F7> :s/{/{\r/ ^M :s/}/\r}/ ^M
Where you see ^M in the sequence, type [Ctrl-V], then press enter.
Now with your cursor anywhere on your sample line, press the mapped key, and the carriage returns are added.
Check :help map-which-keys for advice on selecting unused keystrokes to map.
Assuming you're okay with mapping K to something else (choose a different key of your liking), and using marker ' as a temporary marker is okay why not do this?
:nmap K m'a<CR><Esc>`'
now pressing K in normal mode over the character after which you want the line break to occur will split the line and leave the cursor where it was.
Basically, when you split a line you either want to just insert a carriage return, or in the case that you're on a space, replace that with a carriage return. Well, why settle for one or the other? Here's my mapping for K:
"Have K split lines the way J joins lines
nnoremap <expr>K getline('.')[col('.')-1]==' ' ? "r<CR>" : "i<CR><Esc>"
I use the ternary operator to condense the two actions into one key map. Breaking it down, <expr> means the key map's output can dynamic and in this case hinges on the condition getline('.')[col('.')-1]==' ' which is the long winded way to ask vim if the character under the cursor is a space. Finally, the familiar ternary operator ? : either replaces the space with linebreak (r<CR>) or inserts a new one (i<CR><Esc>)
Now you have a lovely sister key map to the J command.
Vim will automatically kill any whitespace to the right of the cursor if you break a line in two while autoindent (or any other indentation aid) is enabled.
If you do not want to use any of those settings, use s instead of i in order to substitute your new text for the blank rather than just inserting. (If there are multiple blanks, put the cursor on the leftmost and use cw instead.)
In fact you need the following combined operations:
Press v to enter Visual Mode
Select the line you want to split
Press : to enter in Command Mode
s/\s/\r/g
Done
If you have the input:
aaa bbb ccc ddd
and want to output
aaa
bbb
ccc
ddd
You can use the command
f r<ENTER>;.;.
o ESC command will do it for you.
Set this key mapping in your vimrc
:map <C-m> i<CR><Esc>h
Then press Ctrl+m if you want to use it in your vim.
IMHO, the built-in mapping gs is not a useful mapping (put vim to sleep), one could use this for splitting:
nmap gs i<CR><ESC>
In Vrapper you can use gql which will split a line without entering insert mode, but may not always maintain indentation.
I found this to be the most faithful implementation of what I'd expect the opposite behaviour to J
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
It does the simplistic new line at cursor, takes care of any trailing whitespace on the previous line if there are any present and then returns the cursor to the correct position.
i <cr> <esc> - this is one of the most common solutions suggested, it doesn't delete non-whitespace characters under your cursor but it also leaves you with trailing whitespace
^mw - goto start of new line and create a mark under w
gk - go up one line
:silent! s/\v +$//<cr> - regex replace any whitespace at the end of the line
:noh<cr> - Clear any search highlighting that the regex might have turned on
`w - return the the mark under w
Essentially combines the best of both r<esc><cr> and i<cr><esc>
Note: I have this bound to S which potentially overwrites a useful key but it is a synonym for cc and since I don't use it as often as I do splits I am okay with overwriting it.
This mapping will break up any one-line function you have. Simply put your cursor on the line and hit 'g' in normal mode:
:map g ^f{malr<CR>`a%hr<CR>`a
This assumes that you have a space after the opening brace and a space before the closing brace. See if that works for you.