Hot answers tagged

29

Using multiple cursor isn't a Vimmer thing As I said in the comments using multi cursors (even with a plugin) isn't really "following the Vim way", I totally understand that it is attractive for someone coming from Sublime-Text but you can often find alternatives which are at least as efficient with Vim built-in features. Of course, finding these ...


16

You can use ctrlE to scroll down your file, when you are at the end of the file it will add "virtual" line so the last line of the file is not at the bottom of the windows. Additionally you can use the option scrolloff to always let some lines after your cursor, add this to your .vimrc: set scrolloff=10 For more details see :h 'scrolloff' and :h CTRL-E.


11

If you set :set revins you can insert backwards. See also :h ins-reverse: o Typing backwards ins-reverse ---------------- In lieu of using full-fledged the 'rightleft' option, one can opt for reverse insertion. When the 'revins' (reverse insert) option is set, inserting happens backwards. This can be used ...


8

Neovim does not use vimrc it uses $XDG_CONFIG_PATH/nvim/init.vim. Yet, there is more to it. In Vim you should use \<esc> to represent the escape key. I can change the cursor on Arch Linux on urxvt with the following in my vimrc: let &t_SI = "\<esc>[5 q" let &t_SR = "\<esc>[5 q" let &t_EI = "\<esc>[2 q" I also tried ...


8

You could define a custom command using :help winsaveview(): command! -range=% Equal let b:wv = winsaveview() | silent execute <line1> . ',' . <line2> . 'normal! ==' | call winrestview(b:wv) :Equal like gg=G :12,23Equal like :12,23normal! == v{motion}:Equal like v{motion}=


8

The default syntax script for vim doesn't have lightyellow listed for vimHiCtermColor. You could patch it and submit your change to the vim_dev mailing list. Or, better, ask them if that is intentional. Error as a lower priority than CursorLine but you can force it with this simple hack: hi Error cterm=reverse ctermbg=white ctermfg=red Instead of white ...


7

As of November 2017, all the terminals you are using support the same DECSCUSR escape sequences for changing the cursor shape1. So you don't need to test for the different terminals. As such, the only thing that requires different treatment is tmux, which will only forward escape sequences on to the terminal when surrounded by a DCS sequence. You already ...


6

We discussed it less then 10 days ago on reddit. One solution is matchstr(getline('.'), '\%'.col('.').'c.') which doesn't mess with any register. I've put the resulting functions (we can ask for the characters before, or after the cursor) in my vimscript library along with other functions on the subject.


6

If you simply want the cursor to be able to go one past the end of the line for the purpose of starting visual mode, you want the setting set virtualedit=onemore (see :help 've). However, this does not let you remove the new line character in normal mode (in vim you usually use J to do this).


6

If vim-surround were implemented as 's' and 'S' (pseudo-)text-objects instead of operators ds, dS, cs, cS etc, this wouldn't be an issue. I don't know if there's some technical reason for this design choice, but anyway one solution would be to write your own wrapper omap: let g:surround_no_mappings = 1 function! SurroundOp(char) if v:operator ==# 'd' ...


6

You can use the atom /\&. This is like forcing two different regular atoms to match at the same position. Now it becomes a bit complicated, since the cursor position can be anywhere inside the <<..>> and then you also want to allow whitespace inside those <<..>>. This makes it a bit complex to match the correct item if there are ...


5

Expanding on Karl Yngve Lervåg's answer: maHmbgg=G`bzt`a Explanation: ma - Set mark 'a' H - Jump to the top of the window mb - Set mark 'b' gg=G - Indent from the first line to the last `b - Jump to mark 'b' zt - Scroll the cursor line to the top of the window `a - Jump to mark 'a' This will preserve your scroll position after indenting. If ...


5

Your given use-cases to me sound like to want to know the . key command (repeat last Editing command) Go to first place, press i, insert your text, <ESC> move to location 2, press . (Dot) ==> Identical Text is inserted To move down one line here and insert again (when inserting into consecutive lines), press <ALT>-j. Your tex example for me is ...


5

Vim doesn't remember where you've been in a file unless you made a change there (see :help changelist) or jumped there (see help jumplist). Also, Vim's cursor is always somewhere in a window, that is, in the part of the buffer visible to the user, so you can't leave the cursor on some line and not have it follow you as you scroll elsewhere in the buffer. ...


5

Can it be written? Yup. command! -nargs=* -complete=command KeepCursor \ let [s:view, s:win] = [winsaveview(), win_getid()] | \ try | \ execute <q-args> | \ finally | \ if win_id2win(s:win) | \ call win_gotoid(s:win) | \ endif | \ keepjumps call winrestview(s:view) | \ endtry Can it ...


4

Thanks to the comment by @muru I was able to figure this out. I turns out in my .vimrc I had this line to remap jk to <esc> inoremap jk <esc> but there was a space inserted at the end of the line. If I remove the space the problem is fixed. I did not realize that white space characters were important when making these customization. This ...


4

More Vim ways. First task: \section[foobar baz]{foobar baz} Workflow: \section[]{} hh ifoobar baz<Esc> $. Second task: variable1 = 2 my_variable2 = 12 var3 = 14 Command: 3:norm ea_somestuff Result: variable1_somestuff = 2 my_variable2_somestuff = 12 var3_somestuff = 14


4

You could achieve '' on an autocmd using the following: :call setpos('.', getpos("''")) Check :help function-list for built-in functionality on VimL. Edit okay, how to combine it with search and replace? Another autocmd after the search and replace one? Vim documentation is very comprehensive and well-written, you should definitively try reading it. ...


4

I am used from other editors to use the cursor as a visual bookmark, so that after scrolling by letting the cursor off screen I can come back to its place later. If you're just scrolling through the file and want to bookmark specific place, there's really no way around using Vim's marks. Sorry about that. However, if you want to bookmark a place where you'...


4

You can use 'virtualedit' for this: :set virtualedit=onemore Note however the warnings in :help 'virtualedit': "onemore" is not the same, it will only allow moving the cursor just after the last character of the line. This makes some commands more consistent. Previously the cursor was always past the end of the line if the line was ...


4

This should be fixed in the release after 0.2.2. This was reported in neovim/neovim#7641 and the issue was that st's terminfo incorrectly specifies how to change the cursor shape. The fix in neovim was to ignore what st reports and use the typical values for those escape sequences.


4

The problem you're having here is that by the time show_position() is running you're already out of visual mode, since the : switched to command-line mode before that function gets called. If you call getpos("v") before you leave visual mode, it will correctly return where you actually started the visual selection, instead of the top of the selection ('<)...


4

One solution is to use a lookahead before the regex and a lookbehind after the regex. That ensures the cursor is after the start of the match, and before the end of the match. One issue with the regex you're using is .\{-} which is a non-greedy operator. If you add anchors for the cursor position, this will end up matching from the beginning of <<one ...


3

You should use a mark, e.g.: magg=G`a Here ma will set the a mark at the current position, then gg=G indents the file, and `a will jump to the mark. In order to put the cursor at the center of the screen afterwards, you could finish this off with zz. See :h scroll-cursor for more info and for similar scroll mappings.


3

zz does what you want, sort of, but it doesn't work near the top of the file, nor near the bottom (when the file is too short). There is another way to keep track of the cursor, by highlighting cursor's line and column: :set cursorline cursorcolumn But there is a price to pay for it though, these settings can slow down scrolling for some filetypes.


3

The right mental model is that the cursor is before the current character. So, any {motion} that moves to the right (e.g. $, w) includes the character under the cursor, whereas any motion to the left (e.g. ^) excludes it. Likewise, i inserts before the current character, whereas a appends after it. Same for pasting with P (before) / p (after).


3

I think I figured it out, although I don't totally understand the fix. This appears to work for mintty, xterm, and rxvt: " Set up vertical vs block cursor for insert/normal mode if &term =~ "screen." let &t_ti.="\eP\e[1 q\e\\" let &t_SI.="\eP\e[5 q\e\\" let &t_EI.="\eP\e[1 q\e\\" let &t_te.="\eP\e[0 q\e\\" else let &...


3

You (also) need to address the visual mode map (onoremap doesn't do that): :xnoremap <silent> f( :<C-U>normal! f)vF(b<CR> See also: http://vim.wikia.com/wiki/Creating_new_text_objects (where the omap is made based on the vnoremap).


3

This updates the cursor when entering/exiting operator pending mode. While it is a bit of a hacky solution I don't think it has any performance problems. " This is esentially: " exec 'silent !printf "\e[" . a:t . ' q' " redraw! " but without the screen flash function! s:setCursor(t) " Save existing values. let [l:title, l:t_ts, l:t_fs, l:...


3

Can it be written? Possibly, but it might be a lot harder than expected. Because, what do you do with commands that close the current window? Or commands, that add lines above the current cursor position? One problem you are running into is described unter :h function-search-undo. The last used search pattern and the redo command "." will not be changed ...


Only top voted, non community-wiki answers of a minimum length are eligible