How to run Vim shortcuts without exiting insert mode - vim

For instance, I'd like to use $ to move to the end of the line, without going through Esc for it.
Just $, and not Esc then $.
Is there a way?
One solution could be: press another key(/combo), say, Ctrl+Shift, do what I want in "temporary normal mode", then when I release those keys I'm back in insert mode... if that's possible.

If you wanna perform one normal mode command in insert mode you can use Ctrl+O and then normal mode command. So Ctrl+O$ will move you to the end of line in insert mode (like End or Ctrl+OShift+A).

Is there any particular reason why you want to go to the end of the line? Vi is built to mainly be operated in normal mode, not insert mode (especially when it comes to movement).
For example, if you wanted to go to the end of the line and start adding text, you would type A (or ESCA if you were in insert mode at the time).
If you wanted to go to the end of the line so you could add a newline, you could type ESCo to start a new line and place you in insert mode.
If you gave us more context as to what you are ultimately trying to accomplish, we may be able to come up with a more efficient workflow.

inoremap $ <ESC>A
give this a try. It may do what you want, but make inserting $ harder.

Ctrl + Right
map! <C-Right> <Esc>A

Related

Vim does not like brackets

I have a problem that have been bothering me for days now.
Whatever I do, I simply cannot place any single {,},[ or ].
Every time I try, it takes me up to the next or previous paragraph. I've looked it up and I can't find a good way to unmap it and finally have my azerty keyboard to behave correctly so I can peacefully code
In the normal mode, press 'i' to enter the insert mode.
And you can start to edit your file.
After everything is done, press the 'ESC' key and then ':wq' to save your modification.
Vim / vi has several different modes. This dates back to the times when you would edit using a teletyper, i.e. no "visual" representation of the data you are editing, no mouse etc.
With only the "usual" keys at your disposal, you need to navigate in the data, you need to insert data, and you need to execute commands on the data. Not so coincidentially, these are the three modes of Vim / vi, and in each mode, the keys do something differently.
By default, the editor starts in the "normal mode", which you will use to navigate, and enter the other modes from.
In this mode, ) moves you to the end of the sentence, ( to the previous sentence. } and { do the same for paragraphs, combinations of [ and ] work with chapters.
If you enter "insert mode" (most simply by pressing i in "normal mode"), you can enter all the above letters normally. You exit "insert mode" by pressing <Esc>. You will recognize "insert mode" by -- INSERT -- being displayed in the bottommost line of the screen.
If you are in "insert mode", and pressing any of the above keys does move your cursor instead of entering the corresponding symbol, there is some (broken) configuration at work. Check your ~/.vimrc, and if necessary, rename it and try again with a "clean" configuration.
It is next to impossible to do Vim / vi justice in the scope of a SO answer. It is very much an expert-friendly editor, not a novice-friendly one. You need to actually learn how to use this editor, but it is absolutely worth it in the long run.
In insert mode, you should be able to insert brackets without trouble.
In normal mode, those correspond to motions.
To go to insert mode from normal mode, press i.
See: :h object-motions
Alright, Problem Solved ! I had to uninstall and reinstall the Ubuntu bash on windows 10 multiple times and it worked ! Thanks for the help everone !

Effective way to put ; at the end of line

Sorry for a noob question, but i find it struggling to just put a ";" at the end of line after writing a function. For example, I am coding in C and many time i need to write things like:
f(a);
what i usually type is (from normal mode, using bracket autopair-like feature):
if(a<ESC><SHIFT-a>;
and it need changing mode twice! Comparing to normal editor (sublime):
f(a<right>;
does anyone have more efficient way do do those typing? thanks for any help.
I think you have some "auto-close" plugin installed.
I have that kind of plugin too, and I don't press arrow keys either, since I don't have them on my keyboard. I have this:
" moving cursor out of (right of ) autoClosed brackets
inoremap <c-l> <esc>%%a
So with your example: it would be (assume already in INSERT mode)
f(a<ctrl-l>;
Thus, your fingers never leave the home row.
If you're a vim user, you can hit Shift-a.
Shift-a takes you from normal mode to insert mode, and starts your cursor at the end of the line.
(If you want to be an efficient vim user, you should remap esc to something like caps-lock.)
Comparing to normal editor (sublime):
f(a<right>;
Well… that's exactly how you would do it in Vim if you use Delimitmate or some other "autoclosing" plugin. Why do you insist on making things more complicated than they are?

PhpStorm and IdeaVim - shortcut to complete line?

I'm using the current PHP Storm EAP (138.1505) and IdeaVim 0.35.
Suppose I just want to type:
print_r($foo);
I press i to go into Vim insert mode.
I type p r and I can choose print_r via the autocomplete.
It'll add open and close braces () for me.
I start typing my variable, it'll let me autocomplete that too.
At this stage I have print_r($foo) with my cursor positioned before the closing bracket.
What I'd like now is a single key combination that'll finish the line for me - i.e. add a semi-colon and a carriage return. At the moment I either have to type );[return] by hand as if the autocomplete wasn't there, or for some reason I've got in the habit of doing esc to leave insert mode then A to continue at the end of the line and ;[return][esc]to finish it off, which is even longer.
Any better way?
I don't use PHP Storm. but I do use Intellij + ideavim.
Here I can press Ctrl-Shift-Enter to complete a line. It is a kind of default setting. You can find it under menu: Edit: complete current statement
You may want to give it a try.
You can add this line to ~/.ideavimrc:
imap <C-e> <Esc>A;<CR>
Replace <C-e> with your desired key.

Insert a word in normal mode as compared to executing a command in insert mode

I have discovered that it is possible in insert mode to execute a single command and return to insert mode by using Ctrl-O.
What about the possibility to insert a single word while in normal mode ?
I could, of course, switch to insert mode, write the word, escape and go back to normal mode.
But I wondered if it was possible to map a sequence (e.g. Ctrl-K) allowing without leaving normal mode to quickly add a single word at the cursor position ?
I tried mapping a function with a prompt (cf. question 11567596 - Ingo Karkat's answer - and Wikia tip 1616) but failed : the word was appended at the end of the line...
My best advice is to embrace insert mode; it's the vi way TM.
Seriously, I also had the idea of creating mappings that allow me to enter a word / sentence / whatever, and then automatically return to normal mode. But...
Whether you use the input() from romainl's answer, or a more elaborate approach (e.g. with a CursorMovedI hook), you have to press Enter or another key to indicate that you're done. That's no better than pressing Esc to leave insert mode the normal way!
You just make things more complex: you spend valuable (short) key sequences for it, you have to spend brain cycles to decide between both approaches, your muscle memory must learn to ways to enter text, and these specials aren't available in other vi-modes (e.g. in Bash).
So, I think it's just not worth it.
This simple function works, here:
function! InsertWord()
let l:user_word = input("Type something then hit ENTER: ")
execute "normal i".l:user_word
endfunction
command! InsertWord call InsertWord()
nnoremap <C-k> :InsertWord<CR>
The whole thing could probably be shortened to a one-liner, though, but I like it like that.

Vim / vi Survival Guide

What are the essential vim commands? What does a new-user need to know to keep themselves from getting into trouble? One command per comment, please.
What I find irreplaceable (because it works in vi also, unlike vim's visual mode) are marks. You can mark various spots with m (lower case) and then a letter of your choice (eg x). Then you go elsewhere, and can go back with ``x(backquote letter) to the exact spot, or with'x` (apostrophe letter) to go to the line.
These movements can be used as arguments to commands (yank, delete, etc). For example, you want to delete 10 lines; instead of counting and then moving to the topmost line and entering 10dd, you go to either the start or the end of the block, press mm (mark m), then go to the other end of the block, and press d'm (delete apostrophe m). If you use backquote instead of apostrophe in this example, then the deletion will work character-wise, not line-wise. Try marking in the middle of the line with "mark m", moving to the middle of another line, then entering "d backquote m" and you will see what I mean.
I was very happy the day I learned about using * or # to search, down or up respectively, for the word under the cursor. Make sure to :set incsearch and :set hlsearch first.
I like this QRC!
http://www.fsckin.com/wp-content/uploads/2007/10/vi-vim_cheat_sheet.gif
When you have some repetitive action to take Macros are usually faster than regex.
Just type
q[0-9a-z] in normal mode
Many people use
qq
because it's fast.
Press
q in normal mode
again to stop recording.
Then just type
#[0-9a-z] in normal mode
to repeat what you just recorded.
#q
for the example like above.
Edited to add: you can also repeat the macro. Let's say you programed a macro to jump to the head of a line, insert a tab, and then jump down one line. You then test your macro by typing "#q" to run it once. Then you can repeat the action nine more times by typing "9#q".
:q -> quit
:w -> save
:q! -> quit and don't save
:x -> save and quit
:[number] -> go to line number
G -> go to end of file
dd -> delete line
p -> "put" line
yy -> "copy" line
:s/[searchfor] -> search
I guess those are the basic one to start from
Use the 'J' (J for Join; upper-case) command to delete the newline at the end of a line. You'll find it tricky otherwise.
This recent Vim tutorial from IBM is pretty good
First of all you need to know how to close vi:
ctrl-c : q!
Rest can be found from vimtutor. Launch vimtutor by typing vimtutor at your command line
Although this is a matter of personal preference I've found that one of the essential things to do is to remap Esc to something else.
I find it very uncomfortable to reach for the Esc key to exit insert mode, but the beautiful thing about Vim is that allows key mappings.
I'm currently using the following mapping using Control + S:
inoremap <C-s> <Esc>:w<CR>
This has the advantage of being a key mapping I have already committed to memory and has the added value of saving my work every time I go to normal mode. Yeah, I know it is crazy but I would be hitting the save command that frequently anyway. It's like a bad habit, you know.
" ~/.vimrc
" Turn on line numbering
set nu
" Turn on syntax highlighting
syntax on
" Set 4 space expanding tabs
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
"turn off line wrapping
set nowrap
" Map CTRL-N to create a new tab
:map <C-n> <ESC>:tabnew<RETURN>
" Map Tab and CTRL-Tab to move between tabs
:map <Tab> <ESC>:tabn<RETURN>
:map <C-Tab> <ESC>:tabp<RETURN>
If you're using vim, the 'u' command (in command mode) will Undo the last command you typed. You can use this command repeatedly to undo mistakes you may have made before saving the file.
See http://www.rayninfo.co.uk/vimtips.html for a great collection of Vim tips, from the basic can't-live-without to very sophisticated stuff that you might never have thought of trying.
Lots of great commands are listed at the Vim Tips Wiki.
It's also good to run the vimtutor when learning these commands
alias vi nedit :)
all humor aside..
for vi WHEN NOT using nedit..
i (switch to insert mode)
a (append = move to end of line and switch to insert mode)
esc (exit insert mode)
dd delete a line
x delete a character
:wq (save and quit)
/ start a search
n find Next
? search backwards..
yy (yank) copy a line to the buffer
pp (paste) paste it here
r (replace a character)
<N> <command> this is a neat - but aggravating feature that lets you type digits and then a command so
5dd will delete 5 lines
but at this point you might as well
- man vi and refresh your memory
While there are LOTS more, I switched from Vi to nedit several years ago, which I find has more features I can use on a regular basis more easily. Tabbed editing, incremental search bar, column select, copy and paste. sort selected lines, search and destroy within selection, whole doc or all open docs..
tear-off drop down menus..
and it supports syntax highlighting for all the languages I use.. (with pattern files I've used a long time over the years. VIM many now be equivalent, but It has to introduce a feature that Nedit doesn't and an easy way to migrate my pattern files before I switch again.
I like the Vim 5.6 Reference Guide, by Bram Moolenaar and Oleg Raisky.
You can directly print it in booklet form, easy to read, I always have it laying around.
It's a tad old, but what are 8 years in Vi's lifespan ?
:set ignorecase smartcase
Makes searching case-insensitive, unless your search includes a capital letter. Not the most indispensable perhaps, but I find myself setting this option any time I'm editing in a new place. It's in any vimrc file I own.
:%!xxd
View the contents of a buffer in hexadecimal. To revert:
:%!xxd -r
My biggest tip: ctrl+q saves the day when you accidentally hit ctrl+s to save the file you are working on
I have this in my vimrc
set number
set relativenumber
This gives me a line numbering system which makes j, k keys really productive.
I use vi very lightly, and I only use the following commands:
a - switch to insert mode (after the cursor)
esc - return to command mode
:wq - save and quit
:q - quit (no save, only without modification)
:q! - force quit (no save, also with modification)
x - delete one character (in command mode)
dd - delete the whole line (in command mode)
I know there are many many more, but those are enough to get you by.
One of my favourite commands is %G which takes to directly to the end of a file. Especially useful in log-files.
How to switch between modes (i to enter insert mode (one of many ways), esc to exit insert mode, colon for command mode) and how to save and exit. (:wq)
Another useful command is to search something: /
e.g. /Mon will search (and in case of vim highlight) any occurences of Mon in your file.
As a couple of other people have already mentioned, vimtutor is the way to go. It will teach you everything you need to know in vim. The one piece of general advice I would give you is to stay out of insert mode as much as possible. There is enormous power in the other modes, it just takes a little bit of practice to get used to it.
i - insert mode (escape to exit)
dd - delete line
shift-y - 'Yank' (copy) line
p - 'Put' (paste) line(s)
shift-v - Visual mode used to select text (tryin 'yanking' this text and 'putting' it somewhere.
ctrl-w n - create new window (you can open a file or start new file here)
ctrl-w v - split existing window vertically
ctrl-n (in insert mode) - autocomplete (if supported)
:! to run a shell command, usually with standard in as the file or a selection (shift-V)
Useful plugins to look at:
* Buffer Explorer - use \be to view files in the buffer (and select to re-open)
NB vi is not vim! vim is rapidly turning into the emacs of the new century. nvi is probably the closest thing to the original vi. Here's a nice hint: "xp" will exchange two characters (try it).
replace 'foo' with 'bar' everywhere in the file
:%s/foo/bar/gc
The real power is in the searching. Here are the essential commands:
/Steve will find the first instance of "Steve" in the text.
n will find the next "Steve" in the text.
:%s//Stephen/g will replace all those instances of "Steve" you just searched for with "Stephen".
Not to promote myself, but I wrote a blog post on this subject. It focuses on the critical parts of Vim for a beginner.
My favorites:
% find matching bracket/brace
* and # next/previous match
gg top of page
G end of the page
<Ctrl-v> Change to visual mode and select column
<Ctrl-a> increase current number by 1
<Ctrl-x> decrease current number by 1
Running macros

Resources