Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, April 26, 2016

Traversing text in Insert mode

Traversing text in Insert mode


While in Insert Mode in Vim, is there any way to traverse the text moving some characters forward and backward other than using the arrow keys?

If I press h, j, k and l while in Insert mode, the actual characters are printed on screen instead of moving through the text.

The way I'm doing it at the moment is having to resort to Ctrl + [ (Esc) and traversing the text then; but obviously that is not productive.

Answer by Amber for Traversing text in Insert mode


I believe Home and End (and PageUp/PageDn) also work normally while in insert mode, but aside from that, I don't believe there are any other standard keys defined for text traversal.

Answer by Johan for Traversing text in Insert mode


Sorry but vim don't work that way.

You should switch to "normal" mode, navigate and then go back to insert again.

Answer by Carl Smotricz for Traversing text in Insert mode


vim lets you map any key to pretty much anything you want. Among the many capabilities is also the ability to switch in and out of command mode, or to move the cursor in insert mode... so if you're not restricted to a default-configured vim, anything can be done.

No, I'm not good enough to give an example. If I had to, I'd pick up the manual and figure it out.

Answer by heijp06 for Traversing text in Insert mode


You could use imap to map any key in insert mode to one of the cursor keys. Like so:

imap h   

Now h works like in normal mode, moving the cursor. (Mapping h in this way is obviously a bad choice)

Having said that I do not think the standard way of moving around in text using VIM is "not productive". There are lots of very powerful ways of traversing the text in normal mode (like using w and b, or / and ?, or f and F, etc.)

Answer by Pavel Shved for Traversing text in Insert mode


You seem to misuse vim, but that's likely that you're not very familiar with it.

The right way is to press Esc, go where you want to do a small correction, fix it, go back and keep editing. It is effective because Vim has much more movements than usual character forward/backward/up/down. After you learn more of them, this will happen to be more productive.

Here's a couple of use-cases:

  • you accidentally typed "accifentally". No problem, the sequence EscFfrdA will correct the mistake and bring you back where you've been editing it. Ff movement will bring you back to the first encountered "f" character. Compare with Ctrl+<-->->->->deldEnd, that does virtually the same in a casual editor, but takes more keystrokes, makes you move your hand out of alphanumeric space of the keyboard.
  • you accidentally typed "you accidentally typed", but want to correct it to "you intentionally typed". Then Esc2bcw will erase the word you want to fix and bring you to insert mode, so you can immediately retype it. To get back to editing, just press A instead of End, to reach which you should move your hand
  • you accidentally typed "mouse" instead of "mice". No problem - the good old Ctrl+W will delete the previous word without going out from insert mode. And it happens to be much faster to erase small word than to fix errors in it. I'm so used to it that I had closed the browser page when I was typing this message...
  • repetition count is largely underused. Before making a movement, you can type a number; and the movement will be repeated this number of times. For example, 15h will bring you 15 characters back and 4j will scroll you 4 lines down. Start using them and you'll get used soon and find out that pressing 10 times <- key is less fast than iterative approach to moving cursor, when you type 12h, notice that you made a mistake and immediately correct yourself with ll.

But, if you still want to do small text traversals without leaving insert mode, follow rson's advice and use Ctrl+O. As an example, Ctrl+OF+f will move you to previous f character and leave you in insert mode.

Answer by ammoQ for Traversing text in Insert mode


In GVim, you can use the mouse. But honestly, what's wrong with using the arrow keys? There's a reason why they are on a keyboard.

Answer by accolade for Traversing text in Insert mode


Insert mode

Movement

hjkl

Notwithstanding what Pavel Shved said - that it is probably more advisable to get used to Escaping Insert mode - here is an example set of mappings for quick navigation within Insert mode:

" provide hjkl movements in Insert mode via the  modifier key  inoremap  h  inoremap  j  inoremap  k  inoremap  l  

This will make Alt+h in Insert mode go one character left, Alt+j down and so on, analogously to hjkl in Normal mode.

You have to copy that code into your vimrc file to have it loaded every time you start vim (you can open that by typing :new $myvimrc starting in Normal mode).


Any Normal mode movements

Since the Alt modifier key is not mapped (to something important) by default, you can in the same fashion pull other (or all) functionality from Normal mode to Insert mode. E.g.:
Moving to the beginning of the current word with Alt+b:

inoremap  b  inoremap  w  


(Other uses of Alt in Insert mode)

It is worth mentioning that there may be better uses for the Alt key than replicating Normal mode behaviour: e.g. here are mappings for copying from an adjacent line the portion from the current column till the end of the line:

" Insert the rest of the line below the cursor.  " Mnemonic: Elevate characters from below line  inoremap        \      \jl          \y$      \hk          \p          \a  " Insert the rest of the line above the cursor.  " Mnemonic:  Y depicts a funnel, through which the above line's characters pour onto the current line.  inoremap        \      \kl          \y$      \hj          \p          \a  

(I used \ line continuation and indentation to increase clarity - the commands are interpreted as if written on a single line.)


Built-in hotkeys for editing

CTRL-H     delete the character  in front of the cursor (same as )  CTRL-W     delete the word       in front of the cursor  CTRL-U     delete all characters in front of the cursor (influenced by the 'backspace' option)  

(There are no notable built-in hotkeys for movement in Insert mode.)

Reference: :help insert-index


Command-line mode

This set of mappings makes the upper Alt+hjkl movements available in the Command-line:

" provide hjkl movements in Command-line mode via the  modifier key  cnoremap    cnoremap    cnoremap    cnoremap    

Alternatively, these mappings add the movements both to Insert mode and Command-line mode in one go:

" provide hjkl movements in Insert mode and Command-line mode via the  modifier key  noremap!    noremap!    noremap!    noremap!    


The mapping commands for pulling Normal mode commands to Command-line mode look a bit different from the Insert mode mapping commands (because Command-line mode lacks Insert mode's Ctrl+O):

" Normal mode command(s) go? --v <-- here  cnoremap   &cedit. 'h' .''  cnoremap   &cedit. 'j' .''  cnoremap   &cedit. 'k' .''  cnoremap   &cedit. 'l' .''    cnoremap   &cedit. 'b' .''  cnoremap   &cedit. 'w' .''  


Built-in hotkeys for movement and editing

CTRL-B       cursor to beginning of command-line  CTRL-E       cursor to end       of command-line    CTRL-F       opens the command-line window (unless a different key is specified in 'cedit')    CTRL-H       delete the character  in front of the cursor (same as )  CTRL-W       delete the word       in front of the cursor  CTRL-U       delete all characters in front of the cursor    CTRL-P       recall previous command-line from history (that matches pattern in front of the cursor)  CTRL-N       recall next     command-line from history (that matches pattern in front of the cursor)           recall previous command-line from history (that matches pattern in front of the cursor)         recall next     command-line from history (that matches pattern in front of the cursor)         recall previous command-line from history       recall next     command-line from history       recall previous command-line from history     recall next     command-line from history         cursor one word left       cursor one word left      cursor one word right      cursor one word right      cursor at mouse click  

Reference: :help ex-edit-index

Answer by Alex for Traversing text in Insert mode


To have a little better navigation in insert mode, why not map some keys?

imap    imap    imap    imap    "  is used to repeat last entered text. Override it, if its not needed  

If you can work around making the Meta key work in your terminal, you can mock emacs mode even better. The navigation in normal-mode is way better, but for shorter movements it helps to stay in insert mode.

For longer jumps, I prefer the following default translation:

    maps to       

This shifts to normal-mode and goes back a word

Answer by pfries for Traversing text in Insert mode


You can create mappings that work in insert mode. The way to do that is via inoremap. Note the 'i' at the beginning of the command (noremap is useful to avoid key map collisions). The corollary is 'n' for 'normal' mode. You can surmise what vim thinks is 'normal' ;)

HOWEVER, you really want to navigate around in text using 'normal' mode. Vim is super at this kind of thing and all that power is available from normal mode. Vim already provides easy ways to get from normal mode to insert mode (e.g., i, I, a, A, o, O). The trick is to make it easy to get into normal mode. The way to do that is to remap escape to a more convient key. But you need one that won't conflict with your regular typing. I use:

inoremap jj   

Since jj (that's 2 j's typed one after the other quickly) doesn't seem to appear in my vocabulary. Other's will remap to where it's comfortable.

The other essential change I make is to switch the CAPSLOCK and CONTROL keys on my keyboard (using the host computer's keyboard configuration) since I almost never use CAPSLOCK and it has that big, beautiful button right where I want it. (This is common for Emacs users. The downside is when you find yourself on an 'unfixed' keyboard! Aaarggh!)

Once you remap CAPSLOCK, you can comfortably use the following insert mode remappings:

Keeping in mind that some keys are already mapped in insert mode (backwards-kill-word is C-w (Control-w) by default, you might already have the bindings you want. That said, I prefer C-h so in my .vimrc I have:

inoremap    

BUT, you probably want the same muscle memory spasm in normal mode, so I also map C-h as:

nnoremap  db  

(d)elete (b)ackwards accomplishes the same thing with the same key chord. This kind of quick edit is one that I find useful in practice for typos. But stick to normal mode for moving around in text and anything more than killing the previous word. Once you get into the habit of changing modes (using a remap of course), it will be much more efficient than remapping insert mode.

Answer by felix for Traversing text in Insert mode


While in insert mode, use CtrlO to go to normal mode for just one command:

CTRL-O h  move cursor left   CTRL-O l  move cursor right  CTRL-O j  move cursor down  CTRL-O k  move cursor up  

which is probably the simplest way to do what you want and is easy to remember.

Other very useful control keys in insert mode:

CTRL-W    delete word to the left of cursor  CTRL-O D  delete everything to the right of cursor  CTRL-U    delete everything to the left of cursor  CTRL-H    backspace/delete  CTRL-J    insert newline (easier than reaching for the return key)  CTRL-T    indent current line  CTRL-D    un-indent current line  

these will eliminate many wasteful switches back to normal mode.

Answer by everett1992 for Traversing text in Insert mode


Many people in the Vim community argue that you should not navigate in Insert mode, that it is not the Vim way. I think this is an incorrect sentiment learned when transitioning from standard editors to Vim.

Vim is most powerful when you use its tools to create atomic, repeatable actions or finds.

It is ok to navigate while in Insert mode if you are fixing a mistake you made in the same Insert session. You should not navigate outside of the range of text you modified.

If you make a mistake while entering text and escape out of Insert mode to fix it you will not be able to repeat the intended action, . will repeat the correction.

Vim does support many Insert mode navigation keys. Obviously there are the arrow keys, Home, and End, but there are also many other shortcuts. See :h ins-special-keys.

Answer by Shyam Habarakada for Traversing text in Insert mode


If you are a vim purist, skip reading this answer. OTOH, if you are new to vim and are looking for a few helpful tips you wont find in the many hundred of vim tutorials and blogs, read on... :-)

A few un-orthodox (vim) ways

It's 2014, and as someone who's recently gone back to vim I can offer a few, potentially contrarian, points of view and tips.

Use shift+left or shift+right to traverse words

While repetition is a powerful concept in vim, I (personally) find it strange that using it either forces me to count (lines, characters, words, etc.) or make guesses. My brain usually works like "I want the cursor there" and not like "I want the cursor _5_words_to_the_left_". Quickly being able to move the cursor, and visually observe where the insertion point this way allows me to keep my mind on what I'm editing instead of having to count how many hops I need to make to get to where I need to edit.

Turn on mouse mode, and use the mouse wheel and clicking

...to navigate large bodies of text.

Most (all) modern computers have a touchpad that is closely integrated with the keyboard (e.g. MacBooks). Industrial designers have spent many man years optimizing these designs so that the old problem of having to move the hand away from the keyboard is no longer a real issue. Okay, it is if you are used to a mouse and don't like to switch, but for anyone new to vim (like those that might find this post via a search), this should not be much of an issue.

As a bonus, click + drag puts you in visual mode

With mouse enabled, clicking and dragging has the effect of switching to visual mode and marking a region for yanking.

And use the scroll wheel

Using the mouse (wheel) to scroll around, and clicking to position the cursor (duh) just works. See http://usevim.com/2012/05/16/mouse/ for more on this.

And so...

These are what I'd call more modern (using mouse, scroll wheel, etc.) ways of navigating in vim, equally effective depending on your preference of input.

HTH


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.