How would I have Vim remember and position the cursor at the previous line position and column position when a specific file is reloaded? Right now I am able to have vim remember the line position, but vim starts at the beginning of the line instead of the previous column position.
You could use :mkview to save the cursor position (folds, etc.) when you close a file. Then use :loadview next time you use the file.
To make this automatic, add these lines in your vimrc:
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview
You may also find the restore_view.vim plugin useful.
Related
I have been using vim for a couple of months now however my current workflow involves continuously scrolling down and up. For that i use Ctrl+f and Ctrl+b
So whenever I use the Ctrl+f combination the new text appears on the center of the screen. I wanted to know if there was a way for vim to always have a light coloured line in the center of screen (so i could use the line as a reference to the new content whenever i press ctrl+f (scroll half screen)). I know this request sounds odd but I am curious if such a feature exists?
You can try this smooth scroll plugin: https://github.com/terryma/vim-smooth-scroll I think it may solve your problem.
Put this in your .vimrc or in a script:
augroup HlMid
autocmd!
autocmd CursorMoved * :call HighlightMiddle()
augroup END
function! HighlightMiddle()
normal! M
execute "match search /\\%".line('.')."l/"
normal! ^O
endfunction!
EDIT: As rgoliveira stated in the comment that could interfere with match command. You need only to remove the autocommand and make it back whenever you use it:
Remove:
:autocmd! HlMid CursorMoved *
Reload:
:autocmd HlMid CursorMoved * :call HighlightMiddle()
For the last command in the function normal! ^o you get ^o by typing ctrl+v ctrl+o
While inserting a new line below a comment in vim, the result tends to insert a " at the start of the new line. It's probably a simple solution or reason why this is happening, but I am unable to find an exact solution.
If you’re editing a file of the vim filetype, Vim might by default insert the comment character (in Vimscript, this would be ") at the beginning of each new line you enter after a comment. As already mentioned, this is a result of Vim’s formatoptions setting.
To turn this behavior off in the current file, run
:set formatoptions-=ro
To turn it off by default, add this to your ~/.vimrc:
set formatoptions-=ro
To turn it off for Vimscript files, add this to your ~/.vimrc:
augroup filetype_vim
autocmd!
autocmd FileType vim setlocal formatoptions-=ro
augroup END
r and o are options which can be given to formatoptions. For the full list of possible options, run :help fo-table.
This behaviour is governed by the formatoptions variable.
Use :h formatoptions to find out more.
The following article might also be helpful: Disable automatic comment insertion.
I think this should work, regardless of your formatoptions settings.
inoremap <CR> <CR><C-U>
what command are you using to insert below?
If you use the standard "o" keystroke while in Navigation mode, it should insert a new line immediately below whatever the cursor is on, and automatically place you into Insert mode, without inserting an extra "
Similarly an uppercase "O" will insert a new line above whatever line the cursor is on, and place you in insert mode.
I have the following autocmd in my .vimrc:
autocmd FocusLost,BufLeave,BufWritePre *.py :exe "normal! ma" | :%s/\s\+$//e | :exe "normal `a"
This command has the purposes to remove all trailing whitespace from my buffer while keeping the cursor at the current position.
The problem is that when the command is invoked and some text is selected, the text is replaced by ma. How can I modify my autocmd declaration in order to:
Still work when some text is selected
Keep the text intact
Keep the selection if some text was selected
Keep the cursor position if no text was selected
Note: I'm using MacVim.
You can save the cursor position (and overall window “view”) with winsaveview(). The saved position (and view) can be restored with winrestview().
The following code uses the buffer-local variable b:spacestrip_view to store the view (instead of overwriting the mark a):
autocmd FocusLost,BufLeave,BufWrite *.py let b:spacestrip_view=winsaveview()|%s/\s\+$//e|call winrestview(b:spacestrip_view)
What is the best way to highlight the whole current line in VIM? In some IDEs I am able to set some sort of 20% opacity of the line I am editing, this is great to find where I am rather quickly.
To highlight the current line use
:set cursorline
To highlight the current column use
:set cursorcolumn
The visual appearance of this can be modified. Have a look around :help hl-CursorLine or hl-CursorColumn.
See the Vim wiki on this subject for the complete story.
I especially found the following bit handy (when working with more than one window).
autocmd WinEnter * setlocal cursorline
autocmd WinLeave * setlocal nocursorline
Only highlight the current line in the active window.
Shift + V puts you in visual mode, highlighting the entire line.
when I open a file using vim, I'd like vim put the cursor to the place where I last edited so that it can save me lot of searching time to go back to the last edited place in order to, might be, continue the unfinished edit work from that place.
http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session
I think what you want is mkview and loadview. See :help :mkview :mkview outputs different information about your current window including:
The scroll position and the cursor position in the file.
A common mapping for your .vimrc is:
au BufWinLeave *.py mkview
au BufWinEnter *.py silent loadview
to automatically run make and load views. Replace *.py accordingly.