Vim normal mode in command pallete - vim

When i write / or : vim opens command mode - i can write command like:
vimgrep /abc/
If i click shift+p it fill the previous command.
In this case if i want to change only letter a to b I should use arrow key until i get to the letter and then rename it
abc -> bbc.
Is there any faster way - arrow keys are far away and slower to write.
Can I use normal mode when I im in the command pallete(bottom place where i write the commands).
Thank you

You are confusing "command mode" (another name for normal mode) with "command-line mode".
Shift+P (or simply P should insert P. Did you mean <C-p>?
How large is your keyboard for your arrow keys to be "far away"?
See :help cmdline-editing.

Yes, Vim's command-line has limited editing capabilities; for complex edits there, you'd have to use cursor keys. Fortunately, there's a command-line window, which allows edits just like in any other buffer.
You'll find the complete explanation at :help command-line-window. Here's just a quick guide:
If you already know this is going to be a complex edit, open via q: / q/ instead of : / /.
If you only realize this while editing, you can switch via <C-f>.

Related

vim - navigating characters in command mode

Relatively new to VIM and having a great time using it.
One very minor annoyance I've been having is command mode character navigation when I want to revise a command. I'm used to using readline shortcuts on the regular (non-vim) command line but these shortcuts don't seem to work in : command mode.
For example, using ctrl + b to go back a character ends up sending me to the start of the line, or using alt + f to go forward a word ends up clearing the line and exiting command mode.
The only way I've found to navigate in command mode is to use the arrow keys, but I'm under the impression you should avoid the arrow keys in vim for max efficiency.
What is the standard way to navigate around in : command mode? Do vim users usually use the arrow keys here? Is there a different way to modify commands?
As a more concrete example, I've been using vimgrep a lot to search through files. I'll do a command like:
:vimgrep /font-family/j my-project/**/*.less | cope
Later, I'll want to use the same search but look for a different property, so I hit : then ctrl + p to access my previous vimgrep. Now here I have to use the arrow keys to navigate backwards to the search string and modify it. It would be much faster if I could use readline to navigate backwards by word then delete by word.
For small edits, Backspace and light use of the cursor keys should be fine. For anything larger, I would advise to use the command-line window:
In the command-line window the command line can be edited just like editing
text in any window.
So, there's no need to mentally switch to readline key mappings; just use the full editing power (including any custom mappings) of Vim!
You can switch via <C-F> from the command-line, or directly enter it (from normal mode) by pressing q: instead of :. All the details can be found at :help cmdline-window.
I like this question. Long time vim user, but new-ish here, so I can't vote it up. But indeed, perhaps unofficially, many vim fans feel that most of the time the goal is to not have your hands leave home row position (fingers move, hands relatively still).
I will admit, when it comes to command mode, I use the arrows. With P being on my pinky finger, I would miss-hit ctrl-P a lot, and it's faster to slide my right hand down (on my Natural keyboard) and find the up-arrow by quick feel, instantly, to do the same thing. Once I'm there, left-right arrows are also easy to find without looking or delay. Also Ctrl-arrows let you skip by word.
One of the great things about vim is the :help. I have easily spent tens of hours over the years reading through it, and it solves 95% of my problems if I have enough time and working-memory to push deep enough into it.
Here is what I found for :help readline:
READLINE readline.vim ft-readline-syntax
The readline library is primarily used by the BASH shell, which adds
quite a few commands and options to the ones already available. To
highlight these items as well you can add the following to your
|vimrc| or just type it in the command line before loading a file with
the readline syntax:
let readline_has_bash = 1
This will add highlighting for the commands that BASH (version 2.05a
and later, and part earlier) adds.
Give it a try! (I am using vim 7.4)
You can see a list of the default key binds with :help cmdline-history (scroll down a bit) and :help ex-edit-index.
You can remap this with cnoremap:
cnoremap <C-b> <Left>
cnoremap <C-a> <C-b>
" .. Probably more
Note that using cmap will probably get you into problems here since the right-hand side is the currently mapped action, whereas with cnoremap the right-hand side it will always use the native Vim action.

Vim suggestions for search commands / and?

Is it possible to have search suggestions for vim while typing in ? or / command?
Im looking something like... aka neocomplete, fuzzyfinder... see the image.
After typing /, you can press <C-f> to open the command-line window. In it, you can select / search for previous search patterns, edit them (just like in a normal buffer), and use the current one via <Enter>. You can also directly enter this mode via q/. To recall a previously used search, <Up> is quicker, though.
There are plugins that allow to complete words from the open buffer(s) into command-line mode. I use CmdlineComplete.

How can I map these five keyboard actions to a single key in Vim?

I want to map a key so that it will do the following actions in Vim. Suppose I am editing a file; I want it set up so that if I press F2, I will accomplish the same thing I would if I did the following:
press ESC
type colon (:)
type w
press Enter
press ESC again
type i to go back to insert mode
Is this possible?
Yes it's possible, but it doesn't do what you want if the cursor is at the end of line.
To get file saved on F2 in insert mode, use the following mapping:
:imap <F2> <C-O>:w<CR>
Literal answer: Yes. You can use this:
:inoremap <F2> <Esc>:w<CR>I
but it won’t do exactly what you want (the cursor will be at the wrong place).
Anton beat me to the less literal (but correct) answer.
The best answer, though, is this: Don't use Vim incorrectly. You should never spend so much time in insert mode that you need a shortcut to get out of it, save the file, and then get back in. With all other editors, you’re in “insert mode” all the time, and only temporarily pop into a menu or dialog or whatever; in Vim, you should learn to reverse this. Only pop into insert mode to edit or add something; never use arrow keys to move the cursor while in insert mode; spend the majority of your time in command (normal) mode, and after a bit of adjustment to the new paradigm, you will find that your editing speed has increased.
Writing the mapping is almost easier than your description.
First, you need to determine from which mode the mapping will be used, because that determines what :map variant you will use. You’ll probably want to use this in insert mode, so you’ll use :inoremap.
The format of the mapping is:
:..noremap {keys} {rhs}
You want <F2> (see :help key-notation) for keys. For {rhs}, just concatenate the keys listed in your description.
To persist the mapping, add it to ~/.vimrc. (See :help vimrc.)
P.S. The alternative given by Anton Kovalenko is probably better for what you’re trying to do, but here I’ve given you the general recipe for future key mappings.

Traversing directories with vim file name completion in insert mode (Ctrl-X Ctrl-F)

I’m trying to use vim’s compl-filename feature (Ctrl-XCtrl-F) to complete paths in INSERT mode, but I can’t work out how to traverse into directories without (temporarily) ending the completion mode:
Let’s say I want to complete the path /etc/sysconfig/network-scripts/ifup.
I would like to be able to do something like:
/eCtrl-XCtrl-F
/etc/
/etc/sysCtrl-F
/etc/sysconfig/
/etc/sysconfig/netCtrl-F
/etc/sysconfig/netconsoleCtrl-N
/etc/sysconfig/networkCtrl-N
/etc/sysconfig/network-scripts/
/etc/sysconfig/network-scripts/ifupCtrl-Y
/etc/sysconfig/network-scripts/ifup
The issue is, as soon as I start typing* after a path match (like /etc/), it ends file name completion. I would like it to stay in file name completion, so that I can still use Ctrl-F, Ctrl-N, etc. Since it ends completion, I have to type Ctrl-XCtrl-F again to restart it, and the helpful completion popup menu disappears in the meantime.
Is there an option I can set to change this?
* By ‘typing’ here, I am referring to characters in 'isfname' -- of course, typing other characters (like space or punctuation) should not continue file name completion.
I'm not sure exactly what you're saying, but you can just press Ctrl-XCtrl-F again on a directory while you're in the completion menu to expand it. You don't have to close out of the menu first. I just keep Ctrl held down and tap xf to traverse a directory, n and p to move up and down and w to go back up.
If you don't use :h i_CTRL-F then you could remap it. For example,
inoremap <C-f> <C-x><C-f>
Simple remap would be
inoremap / /<C-x><C-f>
So when you type slash(/) in insert mode you will get that auto completion popup :)
Place it in your .vimrc file (for vim) or in init.vim (for neovim)
Vim doesn't do auto-completion.
For that, you'll need a dedicated plugin like AutoComplPop or NeoComplCache
Please use insert "i" first before using cntr+x+f. I was in similar situation. :)

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