How to move behind autopaired char in insert mode in vim - vim

I am using auto-pair in Vim (https://github.com/jiangmiao/auto-pairs). I am typing code:
= link_to 'Link name', some_path(#var|)
And the cursor in edit mode is at | position. Then I want to continue typing more params.
What is the fastest way to move cursor behind the auto-paired )?
I know I can press Ctr-O Shift-A but 4 key strokes to move cursor one column to the left is over-kill.

You could use the right/left arrow keys, but the way the plugin provides a better way of doing it. If your cursor is right next to the closing auto-paired character, you just need to type that character. For instance, if your cursor is in a position like this:
def some_func(args|)
You can just press ) to go to the right of the auto paired character. Same goes for any other closing characters that auto-pairs is compatible with.

Related

Changing case at the end of a word in Vim

I know gUw will turn properties to PROPERTIES and stay at the first character of the word.
But if I use gUb, it will be PROPERTIEs and cursor at the first charactoer.
If I use gUge, it will be PROPERTIES but cursor at last word.
What should I do if I want to turn the word to upper case, and stay at the end of the word(so that i can keep on typing without a w)
All operator+motion edits will leave the cursor on the first character of the motion. From :help motion.txt:
After applying the operator the cursor is mostly left at the start of the text that was operated upon. For example, "yfe" doesn't move the cursor, but "yFe" moves the cursor leftwards to the "e" where the yank started.
So, assuming your cursor is on the last character of the current word:
properties
^
there is no built-in way to ensure that the cursor will stay there after an operator+motion edit.
One can leave a mark and jump back to it after the edit:
m'gUiw``
but that's more work than just pressing w. It could be turned into a mapping, though, if that's a common need:
nnoremap <key> m'gUiw``
but that doesn't sound very scalable.
Another approach would be to record your edit:
qq
m'gUiw``
q
and play it back as needed:
#q
But your last sentence is puzzling:
What should I do if I want to turn the word to upper case, and stay at the end of the word(so that i can keep on typing without a w)
If you are at the end of:
properties
^
and your edit leaves the cursor at the beginning of:
PROPERTIES
^
w will move the cursor to the beginning of the next word:
PROPERTIES foo
^
and not to the end of the current word.
We are missing some context, here.

Undo cursor movement in Vim

Let's say I have my cursor lying in the code below:
internal static SingleSelectList<Country, int> CreateCountrySingleSelectList(List<Country> countries, List<Airport> airPorts)
And the cursor is in the head of the line. Now I want to move my cursor to the second < of this line of code, which is in the WORD List<Country>, and just doing the key sequence of f<; will bring me there.
But what if I did something wrong by pressing another ;, that will bring me to the next < in the line, which is in the word List<Airport>.
In this situation, how can I get back to the second < using the minimum key strokes?
Is there a fastest way to undo cursor movements, instead of F< or pressing the key hfor a very long time?
Try CtrlO to go back in cursor history.
http://www.rosipov.com/blog/open-previously-edited-file-in-vim/
The reversible action of ; is , so the answer to your question would be to use , one time.
You can look up what the command does by :h ;
; Repeat latest f, t, F or T [count] times.
, Repeat latest f, t, F or T in opposite direction
[count] times
Contrary to Ruslan Osipov's wrong answer, the f motion does not store the previous position in the jump list (only motions that usually go to non-adjacent lines do).
But nothing prevents you from explicitly setting a jump yourself with m'. Then, you can return to that position via ``, or <C-O>.

Difference between append and insert mode in Vim

I noticed this accidentally when playing around in vimtutor. What's the difference between append and insert mode in Vim? When I type a in normal mode (not A) I can insert text. When should I use one and not the other?
The append command will put the cursor after the current position, while the insert command will put the cursor before it.
Using the append command is like moving the cursor one character to the right, and using the insert command.
Using the insert command is like moving the cursor one character to the left, and using the append command.
You choose which depending on where you want to start typing.
Note that vimtutor doesn't initially make the case of the command obvious:
SHIFT+A (capital A, as opposed to a) the cursor moves to the end of the current line.
SHIFT+I (capital I, as opposed to i)moves to the start of the current line.
Another important aspect on Append is that if the position after the current position is a empty space followed by a word. After you are done writing it will concatenate both words.
E.g. A file with the following text:
Hi there.
With the cursor on i. After pressing the a button and then ESC you would have:
Hithere.

vim navigation - last character which is not a semi-colon or comma

I work with PHP and Javascript a lot and I find that I am often adding an open parenthesis ( in the middle of a line somewhere (calling a function or something) and then I need to move to the end of the line to close it...
example (before edits):
array(
'key' => 'value sometimes long and with \',() in string',
example (after initial edit - exit insert mode on the close paren )):
array(
'key' => Class::method('value sometimes long and with \',() in string',
^ cursor here after exiting insert mode
desired cursor position in insert mode:
array(
'key' => Class::method('value sometimes long and with \',() in string',
cursor before comma in insert mode ^
obviously, I could do $hi to move to the end of the line, move back one character and enter insert mode... or I could do A< to enter insert mode at the EOL and left arrow one place.
I was wondering if anyone knew of any easier/better way to do this?
Better yet, I am imagining a simple function to "know" if the last character is a comma or semicolon, and move to the last character and only move left if the last character is a comma or semicolon.
pseudo code idea: $("am I on a comma or semicolon" ? h : )i
I would do iClass::method<Esc>lva'S) with the excellent Surround.vim plugin by Tim Pope.
The only standard key sequence I can think of is using Ctrl-o to do a one-off normal command. E.g. Ctrl-o$Left would get you into the right position. If you have imap <C-b> <Left> you can do this without leaving the home row.
I got used to automatic character pairing in my TextMate days. There are a bunch of plugins offering that functionality in Vim; I use DelimitMate.
The idea is to automatically close ()[]{}''"" pairs when you type the first character. That way, you never have to do the gymnastic you are talking about.

In Vim, what is the best way to select, delete, or comment out large portions of multi-screen text?

Selecting a large amount of text that extends over many screens in an IDE like Eclipse is fairly easy since you can use the mouse, but what is the best way to e.g. select and delete multiscreen blocks of text or write e.g. three large methods out to another file and then delete them for testing purposes in Vim when using it via putty/ssh where you cannot use the mouse?
I can easily yank-to-the-end-of-line or yank-to-the-end-of-code-block but if the text extends over many screens, or has lots of blank lines in it, I feel like my hands are tied in Vim. Any solutions?
And a related question: is there a way to somehow select 40 lines, and then comment them all out (with "#" or "//"), as is common in most IDEs?
Well, first of all, you can set vim to work with the mouse, which would allow you to select text just like you would in Eclipse.
You can also use the Visual selection - v, by default. Once selected, you can yank, cut, etc.
As far as commenting out the block, I usually select it with VISUAL, then do
:'<,'>s/^/# /
Replacing the beginning of each line with a #. (The '< and '> markers are the beginning and and of the visual selection.
Use markers.
Go to the top of the text block you want to delete and enter
ma
anywhere on that line. No need for the colon.
Then go to the end of the block and enter the following:
:'a,.d
Entering ma has set marker a for the character under the cursor.
The command you have entered after moving to the bottom of the text block says "from the line containing the character described by marker a ('a) to the current line (.) delete."
This sort of thing can be used for other things as well.
:'a,.ya b - yank from 'a to current line and put in buffer 'b'
:'a,.ya B - yank from 'a to current line and append to buffer 'b'
:'a,.s/^/#/ - from 'a to current line, substitute '#' for line begin
(i.e. comment out in Perl)
:'s,.s#^#//# - from 'a to current line, substitute '//' for line begin
(i.e. comment out in C++)
N.B. 'a (apostrophe-a) refers to the line containing the character marked by a. ``a(backtick-a) refers to the character marked bya`.
To insert comments select the beginning characters of the lines using CTRL-v (blockwise-visual, not 'v' character wise-visual or 'V' linewise-visual). Then go to insert-mode using 'I', enter your comment-character(s) on the first line (for example '#') and finally escape to normal mode using 'Esc'. Voila!
To remove the comments use blockwise-visual to select the comments and just delete them using 'x'.
Use the visual block command v (or V for whole lines and C-V for rectangular blocks). While in visual block mode, you can use any motion commands including search; I use } frequently to skip to the next blank line. Once the block is marked, you can :w it to a file, delete, yank, or whatever. If you execute a command and the visual block goes away, re-select the same block with gv. See :help visual-change for more.
I think there are language-specific scripts that come with vim that do things like comment out blocks of code in a way that fits your language of choice.
Press V (uppercase V) and then press 40j to select 40 lines and then press d to delete them. Or as #zigdon replied, you can comment them out.
The visual mode is the solution for your main problem. As to commenting out sections of code, there are many plugins for that on vim.org, I am using tComment.vim at the moment.
There is also a neat way to comment out a block without a plugin. Lets say you work in python and # is the comment character. Make a visual block selection of the column you want the hash sign to be in, and type I#ESCAPE. To enter a visual block mode press C-q on windows or C-v on linux.
My block comment technique:
Ctrl+V to start blockwise visual mode.
Make your selection.
With the selection still active, Shift+I. This put you into column insert mode.
Type you comment characters '#' or '//' or whatever.
ESC.
Or you may want to give this script a try...
http://www.vim.org/scripts/script.php?script_id=23
For commenting out lines, I would suggest one of these plugins:
EnhancedCommentify
NERD Commenter
I find myself using NERD more these days, but I've used EnhancedCommentify for years.
If you want to perform an action on a range of lines, and you know the line numbers, you can put the range on the command line. For instance, to delete lines 20 through 200 you can do:
:20,200d
To move lines 20 through 200 to where line 300 is you can use:
:20,200m300
And so on.
Use Shift+V to go in visual mode, then you can select lines and delete / change them.
My usual method for commenting out 40 lines would be to put the cursor on the first line and enter the command:
:.,+40s/^/# /
(For here thru 40 lines forward, substitute start-of-line with hash, space)
Seems a bit longer than some other methods suggested, but I like to do things with the keyboard instead of the mouse.
First answer is currently not quite right?
To comment out selection press ':' and type command
:'<,'>s/^/# /g
('<, '> - will be there automatically)
You should be aware of the normal mode command [count]CTRL-D.
It optionally changes the 'scroll' option from 10 to [count], and then scrolls down that many lines. Pressing CTRL-D again will scroll down that same lines again.
So try entering
V "visual line selection mode
30 "optionally set scroll value to 30
CTRL-D "jump down a screen, repeated as necessary
y " yank your selection
CTRL-U works the same way but scrolls up.
v enters visual block mode, where you can select as if with shift in most common editors, later you can do anything you can normally do with normal commands (substitution :'<,'>s/^/#/ to prepend with a comment, for instance) where '<,'> means the selected visual block instead of all the text.
marks would be the simplest mb where u want to begin and me where u want to end once this is done you can do pretty much anything you want
:'b,'ed
deletes from marker b to marker e
commenting out 40 lines you can do in the visual mode
V40j:s/^/#/
will comment out 40 lines from where u start the sequence

Resources