How to exit automatically at end of file on vim? - vim

I tried vim/less.sh as pager with syntax highlighting, but there is a little issue:
when displaying small file, vim uses 'full screen' and waits for a command from user.
Can I let vim to act like a less --quit-at-eof?
In other words, is there a way to automatically quit vim if displayed file is several lines length?
I found one solution with a shell script: count file lines by wc -l, then get terminal height, if size is small - use custom vim config file, where custom config file ending with :quit string.
However, this solution looks terrible and leave extra lines with ~ after end of small file, so I'm looking for better way to do this.

Vim switches to the alternate terminal page, so when you exit it, its contents are gone. Even if you turn that off:
$ vim --cmd 'set t_ti= t_te='
UI stuff like the ~, ruler and statusline would remain, too. Therefore, the solution you've found looks like a reasonable workaround. Either use that or (better) get used to quitting the Vim pager.

Related

Cannot disable vim line break symbols

I had recently updated some of the plugins in vim.After that a "$" sign is appearing in all the line ends of the file. I understand this is a line break issue . I added the following lines in vimrc
set wrap
set linebreak
set nolist
But still the $ sign is there.
The funny part is ,if I use set nolist in any of the file it removes the $ sign.
But it is not picking from vimrc. any solution for this?
The scriptnames command in vim is particularly useful for this. It shows the scripts that loaded successfully.
To show the ones that don't load for some reason, you can start vim with the -V option.
That should hopefully be enough for you to identify either:
that your script is failing; or
scripts that may load after your script, that could change the behaviour back with set list.

How to know commands I've been typing?

I'm using gVim and I would like to know if there is a way to see the commands I've been typing.
For example, when I pressed the visual mode (v) I've got message -- Visual --, but I don't know which letters I've been pressing so far.
Is there a way to permanent see which characters/commands I've typing?
You can use this setting:
:set showcmd
Type :help 'showcmd' to read more.
You could set this up:
alias vim="vim -W ~/.last_vim_session_key_pressed"
But this file is written only when you exit vim. You can source it with vim -s but beware, with vim gui versions you can have problems.
Check your home directory for a .viminfo file.
This will have, among other things, a history from newest to oldest of recent commands you've typed.
There is a tricky way to show all vim keystrokes which were pressed by using -w parameter which record all the characters that you type in the file. The problem is, that vim writes keystrokes only when you exit Vim as Benoit already said.
To workaround this, Kana Natsuno came up with this single-line patch, which disables buffering of the -w option, so you have access to realtime stream of keystrokes. Then it's a matter of reading them (e.g. tail -f), parsing or you can try to display them in the statusbar (:set statusline).
Check out a custom build of Vim using Drew's live-stream-keystrokes branch of MacVim, to get the realtime stream of keystrokes.
Source: Vimprint - a Vim keystroke parser at Drew Neil blog
This is useful if you'd like to reveal the Vim pressed keystrokes in live video tutorials (or GIFs).

Vim response quite slow

If I open a file containing 5,000 lines of code and continue to input, I found that my vim became very slow, it displays my input after about 1s.
It even won't become any better after I start up with --noplugin. But after switching my .vimrc file, everything gets fine again. The .vimrc file is written by myself and after checking for some time, I still can't locate the error. I have clear all the key maps, but the problem still exists.
So can you give my any advise or tell my how to debug in vim? I found there is a debug option but can't get how to work.
You can use the --startuptime option when start vim:
--startuptime {fname} *--startuptime*
During startup write timing messages to the file {fname}.
This can be used to find out where time is spent while loading
your .vimrc, plugins and opening the first file.
When {fname} already exists new messages are appended.
(Only available when compiled with the |+startuptime|
feature).
Take following steps to diagnose the problem:
type vim --startuptime log.txt main.java in bash to start vim
type :tabe log.txt in vim to view the log.
The reason for slowness is often the not set or wrong set ruby_path on compile time of vim (see also discussion on google vim/ruby google group). It is easier to set it in vimrc, because you can change it without recompiling vim. You can set the path through the g:ruby_path variable in your .vimrc file. Don't copy and paste both, use the right one.
If you setup RBENV you have to use this one:
" ruby path if you are using rbenv
let g:ruby_path = system('echo $HOME/.rbenv/shims')
If you setup RVM you have to use this one:
" ruby path if you are using RVM
let g:ruby_path = system('rvm current')
You can also use the vim-rbenv plugin, which sets the path too.
For me the part on loading ruby specific functions in vim got 10 times faster.
If you are using jruby, than the start up slowliness can be even bigger. See examples for fixing it here.
If running vim 7.4,
put this in your .vimrc
set regexpengine=1
vim 7.4 has a new regex engine that appear not to work well in some situations. Previous version vim 7.3 used the old engine ( set regexpengine=1 ).
The "slow response" from syntax highlighting problem affects the vim help files as well ( and .vimrc file too ).
Something like this is usually caused by syntax colouring. Try with :syntax off.
Add these lines to your ~/.vimrc or ~/.config/nvim/init.vim:
set lazyredraw " don't redraw everytime
set synmaxcol=128 " avoid slow rendering for long lines
syntax sync minlines=64 " faster syntax hl
Also if you're using tmux, consider adding this to your ~/.tmux.conf:
set -sg escape-time 10

Prevent vim from opening binary files accidentally?

I frequently accidentally open a binary executable, i.e. "foo", when I mean to open the associated source code "foo.cpp". The root of the problem is that tab completion, i.e. :e fo<tab> typically lands on the binary instead of the source code.
Is there a way to get vim to only tab complete names of text files? Or alternatively, change the tab completion order?
Sometimes my hasty tab completion error happens outside of vim; for those cases, what is the best way to prevent vim from opening files that are not text?
Not exactly what you need, but I have something like this in my .vimrc
" ignore these files when completing names and in Ex
set wildignore=.svn,CVS,.git,*.o,*.a,*.class,*.mo,*.la,*.so,*.obj,*.swp,*.jpg,*.png,*.xpm,*.gif,*.pdf,*.bak,*.beam
" set of file name suffixes that will be given a lower priority when it comes to matching wildcards
set suffixes+=.old
For tab completion outside of vim, that will depend on your shell. Most shells have some form of autocompletion support. In particular, Zsh has the ability to autocomplete e.g. remote hosts for ssh. I'm not a wizard with these things, but it would probably be relatively simple to get your shell to drop files with certain suffixes from the autocompletion list when the command you are typing starts with "vim".
A quick google search turn up this page, which has this:
# Filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o' '*?.c~' \
'*?.old' '*?.pro'
It should not be too difficult to modify this logic to get what you want (if you use Zsh).
Maybe you can find this useful:
set wildmenu
set wildmode=longest,list
(taken and using from How do I make vim do normal (bash like) tab completion for file names?)

How do I fix the indentation of an entire file in Vi?

In Vim, what is the command to correct the indentation of all the lines?
Often times I'll copy and paste code into a remote terminal and have the whole thing messed up. I want to fix this in one fell swoop.
=, the indent command can take motions. So, gg to get the start of the file, = to indent, G to the end of the file, gg=G.
Before pasting into the terminal, try :set paste and then :set nopaste after you're done. This will turn off the auto-indent, line-wrap and other features that are messing up your paste.
edit: Also, I should point out that a much better result than = indenting can usually be obtained by using an external program. For example, I run :%!perltidy all the time. astyle, cindent, etc. can also be used. And, of course, you can map those to a key stroke, and map different ones to the same keystroke depending on file type.
The master of all commands is
gg=G
This indents the entire file!
And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.
To indent the all the lines below the current line
=G
To indent the current line
==
To indent n lines below the current line
n==
For example, to indent 4 lines below the current line
4==
To indent a block of code, go to one of the braces and use command
=%
If you want to reindent the block you're in without having to type any chords, you can do:
[[=]]
You can use tidy application/utility to indent HTML & XML files and it works pretty well in indenting those files.
Prettify an XML file
:!tidy -mi -xml %
Prettify an HTML file
:!tidy -mi -html %
press escape and then type below combinations fast:
gg=G
1G=G. That should indent all the lines in the file. 1G takes you the first line, = will start the auto-indent and the final G will take you the last line in the file.
if you do not want to use :set paste, middle-click, set nopaste, you can also paste the content of the clipboard:
"*p
"+p
That way you don't have to leave normal mode.
if you have to paste + or * depends on how you selected the text, see :help quoteplus.
In Vim, use :insert. This will keep all your formatting and not do autoindenting. For more information help :insert.
:set paste is your friend I use putty and end up copying code between windows. Before I was turned on to :set paste (and :set nopaste) copy/paste gave me fits for that very reason.
For complex C++ files vim does not always get the formatting right when using vim's = filter command. So for a such situations it is better to use an external C++ formatter like astyle (or uncrustify) e.g.:
:%!astyle
Vim's '=' function uses its internal formatter by default (which doesn't always gets things right) but one can also set it use an external formatter, like astyle, by setting it up appropriately as discussed in this question.
vim-autoformat formats your source files using external programs specific for your language, e.g. the "rbeautify" gem for Ruby files, "js-beautify" npm package for JavaScript.
For XML files, I use this command
:1,$!xmllint --format --recover - 2>/dev/null
You need to have xmllint installed (package libxml2-utils)
(Source : http://ku1ik.com/2011/09/08/formatting-xml-in-vim-with-indent-command.html )
You can create a mapping to do this for you.
This one will auto indent the whole file and still keep your cursor in the position you are:
nmap <leader>ai mzgg=G`z
Just go to visual mode in vim , and select from up to down lines after selecting just press = , All the selected line will be indented.
For vi Editor, use :insert. This will keep all your formatting and not insert auto-indenting.Once done press escape to view the actual formatted file otherwise you'l see some garbage characters. like ^I
e.g:
public static void main(String[] args) {
^I
^I System.out.println("Some Garbage printed upon using :insert");
}

Resources