vimrc configuration - vim

I'm sorry if my title seems vague. I wasn't sure how to make a succinct title.
I have 2 questions:
1) For tab movement in vim, when I map ctrl+pgup to ctrl+p and ctrl+pgdown to ctrl+n, it works fine moving between tabs but each time I move from a tab to different tabs and come back to said tab, the cursor moves one space to the right (it goes to the next line if it meets the end of the line)
The mapping looks like this in my .vimrc file
map ^N ^[[6;5~
map ^P ^[[5;5~
2) I want to check if the colorscheme is a certain one and if so then do sth. eg I want to do this:
if &colorscheme == desert256
highlight String ctermfg=217 ctermbg=235
endif

The answer is on https://superuser.com/questions/103293/vimrc-configuration

Related

Move to the last character of previous line vim

I have started using vim recently and was wondering if there's any keyboard shortcut to move the the last character of previous line( line number l-1 if I am currently on line number l) ?
One of the ways is to use the up arrow to move to the same column of previous line and then use $ to move to the end of the line.
I am looking for some shortcut to do that in one command.
UPDATE:
Here's also another solution to move to the beginning of the next line / the end of the previous line without the need to set any mappings. Add these three to your .vimrc:
set whichwrap+=<,h
set whichwrap+=>,l
set whichwrap+=[,]
(Credit to: Kevin H. Lin and garyjohn)
Original Answer:
I've not memorized all Vim shortcut combinations, so there might be one shortcut for what you're asking for, but I usually define a mapping whenever I need something I don't know how to do.
For what you need, you can simply define it with this:
nnoremap <S-L> <Up><S-4>
Just add it to your ~/.vimrc(if this file doesn't exist yet, create it yourself), then restart vim. Then the next time you open up your Vim, the "Shift-L" shortcut will do the job.
You can go straight into the insert mode as well and append characters after the last character of the previous line with this rule instead:
nnoremap <S-L> <Up><S-A>
Also in case you don't understand the structure of the above rules, you can read more about it here:
Understand Vim Mappings and Create Your Own Shortcuts.
There is a corner case: If your cursor is on the first line, pressing this mapping should not move the cursor.
Therefore, we can use the <expr> mapping:
nnoremap <expr> <F6> line('.')==1?'\<F6>':'k$'
In the example above, I used <F6>, you can choose the short-cut key you like.

Move to the beginning of New Line - VIM

while in insert mode in vim how do i move to a new line correctly indented.
In many editors this action would be CTRL+ENTER
There is a similar stackoverflow question here however this answer takes you back to the start of the current line SO start of current line
So assuming this is my code, my cursor is just at the T in POST and i want to go to the start of the next line or next line with correct indentation if its a function(using snippets for function so now great concern).
#app.route('/add', method=['POST | '])
expected result
#app.route('/add', method=['POST'])
|
I would use <esc>o. Assuming you have filetype plugin indent on in your vimrc o will automatically go to the correct indent level.
Search for indentation and map to some keys, so you cannot search for every time.
/^^I
^I is tab

Vim: Select all without scrolling away

is it possible in vim to select all lines in the current file, but leave the position where my cursor is unchanged?
Let's say I am currently at line 500 (of 3000) and want to quickly select everything (not yank), as my selection is simply set up to show whitespace characters. Can this be done without leaving my current line?
To achieve exactly what you like, you can press the following:
ggVG<Esc><Ctrl-O><Ctrl-O>
gg moves to the beginning of the file
V starts visual line mode
G moves to the and of the file (now you have selected the whole
file)
<Esc> leaves visual mode
<Ctrl-O> moves your cursor back to the prevois location (first to the beginning of the file, then the second time to your last position before pressing gg)
And if you like to select only the visible lines in you window (to not scroll away). You can use HVL instead of ggVG (H moves to the top of your window and L to the bottom).
You also could show whitespaces without using visual selection with something like this in your .vimrc:
set list listchars=tab:»·,trail:·,nbsp:·
This helps me to detect trailing whitespaces, and mixed (spaces/tabs) indentation.
usually pressing
ggVG
in normal mode will select all the lines, but it will leave your cursor at the last line of the file.
If you wants to highlights the whitespace characters then you can highlight this by using the below command in command mode (this white color chosen is for dark theme)
: hi ExtraWhitespace ctermbg=White guibg=White
Depending on what you are trying to achieve you can use something like :
%cmd
To apply the command to the whole file.
For example, %y will yank the whole file, %=will format the whole file, without moving your cursor. It does not really work if you do something like %d...
It is not a real selection though but rather a way to apply a command on the whole file.
To go further you can use something like
%norm Atest
To add 'test' at the end of each line. (Actually this is a bad example, because this command will move to the last line...)
It is not possible to have the cursor inside a visual selection. This caused by that, vim defines visual selection through two marks. As soon as you move the cursor one of the marks gets updated. Basically this means one of the marks is always lays where the cursor is(at least when using "v" to select). You cannot have the border in the middle of the region that the border defines :)

Cycling and/or highlighting through a vim selection

The feature I am thinking of is kind of inspired by a feature that I really like about sublime text.
In sublime text, if you select a sequence of characters, it automatically puts a little box around it (to distinguish it from the word that you just highlighted). For me, this is very helpful because I can see and find specific things of the code much faster.
It would be awesome to have something similar to my vim environment. It does not have to be exactly the same as the one in sublime though, but it would be awesome if it were as similar as possible plus the additional feature to easy cycling through similar words.
Currently, what I am doing is highlight the work I want and then manually typing it to the search command /. It would be much better if I can just highlight it in visual mode and then automatically highlight similar words on the current screen with a different colour from the highlighting in visual mode and then have a quick key short cut to cycling through them, if I wished to do that.
I am not sure if a there exists a plugin or something that already does that, but that would cool! Ideally, I would want to to know as many details of the commands/changes to the vimrc file, so that I have the most control over this feature and be able to customize it as I wish.
You can get the highlighting you are looking for by enabling the hlsearch option:
:set hlsearch
It will highlight every occurrence of the last search pattern and thus work after all the following commands (and their relatives):
/foo<CR>
?bar<CR>
:s/fizz/buzz/g
*
#
You can use n to jump to the next occurrence in the direction of your search and N to do the same in the opposite direction.
To highlight every occurrence of the current word "without" moving the cursor, you can simply do:
*N
or:
*``
to jump to the next occurrence and jump back immediately.
Doing the same for visually selected text is a bit trickier but still possible…
either via a lightweight plugin like visualstar or The Search Party,
or with a tiny bit of crude vimscript in your ~/.vimrc:
" this function cleans up the search pattern
function! GetVisualSelection()
let old_reg = #v
normal! gv"vy
let raw_search = #v
let #v = old_reg
return substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction
" map * and # in visual mode so that they do the same as *N and #N in normal mode
xnoremap * :<C-u>/<C-r>=GetVisualSelection()<CR><CR>N
xnoremap # :<C-u>?<C-r>=GetVisualSelection()<CR><CR>N
My SearchHighlighting plugin changes the * command so that it just toggles the highlighting for the current word, without the movement to the next match (for which you can press n, or pass a count). This also works in visual mode, using the selection. I find this very handy for highlighting all matches.
There's also a mode that automatically highlights the current word / selection, like what many IDEs offer.
Other plugins
If you want more permanent highlighting, separate from searching, the Mark plugin offers that.
To get an orientation about the number of matches (without highlighting them), I have the SearchPosition plugin.

How to change the preview result position of vim's taglist plugin (hitting 'p')

While using a preview [p] or jump-to [enter] command in the taglist window, the appropriate line is by default in the file-edit window. Because I mostly need to see more of what's right after the selected tag (functions), I'd love to change the line at which the tag is displayed from the center to let's say 1 third of the current page size (number of lines) or even to an explicit line (let's say 10th line from the top).
Is there a command/settings that would adjust the displayed position of a selected tag? (I couldn't find one in the manual).
Thanks
You're presumably looking for the zt (redraw with current line at the top of the window), and zz (redraw at center) commands, or something in between (which can be achieved with <C-Y> / <C-E> after those commands).
How to incorporate that into the plugin is best discussed with the plugin's author. Since you've found no configuration setting for this, you likely have to directly modify the source code. Write your suggestion to the author; you may get a configuration / hook for this in the next plugin version, or tips how to do this, or maybe the author's opinion of why this is a bad idea.
You could try to tweak the scrolloff setting. I don't know the tagbar plugin but I imagine setting :set so=5 should provide you some context when jumping around.
Well, regarding the Ingo Karkat's comment, I managed to locate the appropriate line in the taglist plugin (begginning at line 3357, version 4.6):
" Jump to the tag
if a:tagpat != ''
" Add the current cursor position to the jump list, so that user can
" jump back using the ' and ` marks.
mark '
silent call search(a:tagpat, 'w')
" Bring the line to the middle of the window
normal! z.
" If the line is inside a fold, open the fold
if foldclosed('.') != -1
.foldopen
endif
endif
Here the part normal! z. needs to be changed appropriately to something that would change a position of the selected line appropriately. As I'm no vim-plugin guru, I changed it in the most dumb way possible as
" Bring the line to the middle of the window
normal! zt
normal! 10k
silent call search(a:tagpat, 'w')
which just walks 10times up and than searches the right position again. Hope this might help someone who 'struggles' with the same issue at the moment until a better solution is suggested or Yegappan Lakshmanan, the author, includes this (or rather something nicer) to his plugin (in case he decides to do that of course :) )

Resources