VIM keybinding to jump back to initial position after indenting the whole file - vim

I have created a keybinding that should indent a whole file.
My first solution looked like this:
map <F4> gg=G
The problem is that after pressing F4, the cursor jumped to the first line of the file. So I have tried to improve my solution with the feature of markers, look like this:
map <F4> mzgg=G'z<CR>
I expected this would have resolved my problem, but the command do the same as the first. When I try to jump to the z marker manually vim told me "marker not set".
After changing the keybinding, I have or course restarted vim! I am using the GVIM 7.3 on a WIN 7 machine.
Thank you in advance for your Help!
Edit:
After trying to get my keybinding working by tipping it directly to vim commandline. I find out that the keybinding was working quite nice. I think problem is that I create a session some times ago (with mksession) and if you load a session I think vim ignores the vimrc-file. Is this assumption right?
Solution:
In these thread I find a soultion to make mksession save less options.

Another lightweight approach: set the ` mark, format the buffer and jump back to the mark afterwards.
:nnoremap <key> m`gg=G``

I would recommend the use of CTRLo and CTRLi which allow to go respectively backward and forward in the jump list. See :help jumps.
The following mapping
map <F4> gg=G2<C-o>
works. (It jumps back two times in the jump list)
But generally, the jump list is a great way to navigate in a file, this is likely the shortcuts that use the most in my daily use. It is also works if you jump to a tag to go back to your original location.
You might also want to use nnoremap rather than map, this way it will only work in normal mode, and you could potentially reuse F4 in combination in another key binding without having recursive mappings.
so
nnoremap <F4> gg=G2<C-o>

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.

repurpose default key in vim

I heard that there is no internal function to call when typing a key combo in vim, unlike emacs. So I want to repurpose Ctrl-A to ^ (like, nnoremap < C-A> ^); however, the original function of Ctrl-A is also attractive, so I want to map it to some other keys, say Ctrl-S. How can I do this the vim way?
This question might have been asked but I searched around and did not find anything close. Thanks in advance.
PS: the stupid vim-emacs war is spamming everywhere and nowhere can i find a real solution that makes a desired feature to work in another environment.
The following should do:
nnoremap <C-A> ^
nnoremap <C-A>A <C-A>
This works because the noremap tells vim to do not apply any other mapping, thus the rhs of the second mapping above is the default <Ctrl-A>.
You will notice that the first mapping will take a delay to execute, because vim waits a while to allow you to type the second mapping. Vim-FAQ 20.16 may help if this bothers you.
But I'd advice against changing the default mapping because, as explained here, vim has many default mappings that may seems silly when you start using it. When you understand it better you will have a hard time finding another key to map it or to get used to the old/default mappings.

mapping # in vim

In my .vimrc I have mapped the #-key to a macro for commenting out/in lines of code.
Unfortunately # in vim already has a function - it searches backwards for the word beneath the cursor.
What I would now like to have is a way to map this functionality to another key-sequence (ideally I would like to have Control-* for that as * alone searches forward).
Does anyone know how to achieve this?
Many thanks!
Unfortunately, Ctrl + * cannot be used; I would propose \*; it's longer to type, but backwards searches are probably not that common.
:nnoremap <Leader>* #
Like Ingo Karkat said, mapping Ctrl+certain keys is impossible in vim. However, you can map Alt+8 instead:
noremap <A-8> #
I suggest Alt+8 instead of Alt+* because if you wanted to bend your hand in unnatural ways to press more than one modifier keys to perform a command, you would probably be using Emacs instead of Vim.
I use \+c for commenting and \+d for removing comments. The mappings are following :
:map \c <ESC>:s,^\(\s*\)[^/ \t]\#=,\1// <ESC>,e<CR>j$a
:map \d <ESC>:s,^\(\s*\)// \s\#!,\1<ESC>,e<CR>j$a
Above mappings are used in command mode. Taken from one answer on SO, which I am currently unable to find.

While moving the cursor across a vim process open in a tmux session, every now and then, why does it leave a trail of ghost characters - ^[OB, ^[OA?

These disappear if I do a page-up and page-down. Why does this happen and how do I rectify it?
http://i.stack.imgur.com/DnMYl.png
I recently fiddled around with my vimrc. Could that be the cause?
UPDATE:
I figured out the cause. I had added to functions that automatically updated the cwd name and the current git branch in my vim statusline. This caused vim to be laggy and everytime it lagged on a up/down/left/right key hit, it printed out the ghost characters ^[OA, etc.
It's possible that stuff got complicated because I was using vim in tmux, but I saw the ghost characters outside tmux also. So, probably not.
I've turned off those two functions in my .vimrc and my vim statusline is presently less awesome than it used to be :(
Wish there's a way out.
^[OB and ^[OA are how your terminal represent <down> and <up>.
I've seen these (and their friends ^[OC and ^[OD) appear in the "command line" or omni-completion menus and during usage of a couple of plugins in vim in tmux.
My solution was to map them to the directional keys:
map ^[OA <up>
map ^[OB <down>
map ^[OC <right>
map ^[OD <left>
In case you don't know, you must not type ^[ as two characters, you must do <C-v><Esc>.
That is, while in --INSERT-- mode press the Control key and the V key together then hit the Escape key. Doing so will input the correct single character that looks like ^[.
This issue is discussed at length on the Vim Wiki article. There seem to be multiple causes, I personally encountered this issue when running vim under tmux.
A solution from there that worked for me and seems less hacky than mapping the keys was the following config:
set term=cons25
Hard to say without knowing what's in your vimrc, but you can confirm whether it's something in there by starting it up without running it and seeing if it still happens using the following...
vim -u NONE
Do you happen to be using zsh? I had this issue with vim + zsh/oh-my-zsh. Moving back to vanilla bash solved this issue (amongst others) that I was having with vim.

Vim: NERD_tree Plugin. Need help understanding a bloggers .vimrc addition to streamline this plugin

So I'm basically a beginner when it comes to Vim, nonetheless I do know the basic things (open files, edit, move around, basic grep, .vimrc, etc)
I would submit this link first
http://weblog.jamisbuck.org/2008/11/17/vim-follow-up
If you scroll down to where it says "NERD___tree", it explains what it is and gives a link to the home page. I have already gotten NERD_tree installed, so far so good.
Only thing is, this guy (JamisBuck) adds a line to the .vimrc file to streamline it's usage (I'm guessing to toggle between NERD_tree and the actual file, because as far as I can tell, there is no quick way to do it other than typing in:
:NERDTree
Every time which is less than desirable. The follwing is the code he adds to the .vimrc file:
map <leader>d :execute 'NERDTreeToggle ' . getcwd()<CR>
He doesn't explain exactly what is is and/or how to use it, so If someone could give me a short explanation and/or point me towards a resource to learn more about this, that would be appreciated.
I'd say :help leader will give you what you need, is an anti-slash by default.
Thus, map <leader>d will be launched when you do \d.
According to the vim documentation, the
<Leader>
Is a special variable that is replaced with the value of "mapleader" at the time the mapping is defined. So:
map <leader>d :execute 'NERDTreeToggle ' . getcwd()<CR>
Is mapping the mapleader and "d" to the toggle. If you look at the page you linked, earlier in the page he says:
I’ve got my <Leader> character (:h mapleader) mapped to the comma
(since it’s easier to reach than the backspace character).
let mapleader = ","
So the toggle should be ",d" as far as I can tell.
In addition to what others have said (d mapped to the command), the command, itself:
:execute 'NERDTreeToggle ' . getcwd()<CR>
Is simply executing the NERDTreeToggle command with the first argument as the current working directory. The at the end is a carriage return, and is just simulating a press of the enter key.
This means that when NERD tree opens, it will be in the current working directory.

Resources