08 February 2019

when to use less instead of vim

i am baffled that so many people feel like vim is the correct tool for viewing files on linux systems. using vim to view files you'd edit is fine. when i am viewing code or a config file which i may change, i might use vim but only if i plan on making changes to the file.

but when i am going to view a file which i don't ever plan to edit or ever need to edit, i use less. log files are ones where i see this happening very often. unless you expect to change the log file, i see no reason for using vim.

even when viewing files which could be edited, it's best to use less. scenario: someone wants to view a file. they use vim. i jump on the server and need to actually change the file. i get the message that the file is already being edited. maybe i can say that i want to edit it anyway, but there are no guarantees someone won't :wq or ZZ out of the file and undo my changes. if they used less, there would be no threat of this.

less lets me move through a file much quicker for pure examination purposes. spacebar works like CTRL+F to scroll a page at time, yet all the vim moving commands are available too. searching text is the same as vim. and i can exit less with a simple tapping of the q key.

"but, but i need line numbers and vim lets me do that!" so does less. use the -N option on less to get line numbers.

"what if learn that i need to edit a file after i have entered less?" just press v in less to enter vim mode.

and a side annoyance are any leftover swap files vim might leave behind on a dirty exit of vim. swap files for log files? how first-year cadet is that?

if you feel the outstanding need to use vim to view files which should never be edited, then use the -R argument to ensure the file is never changed. if you practice using the -R, i am willing to bet you'd find typing less much easier.

and before you ask me, "why not use more?" i will let you figure out why less is more. (and if you are viewing files with nano, there are no words to describe that insanity.)

bonus assignment: find out what the view command does.