vi on multiple files

— 2019 xp: I find it useful to quickly exit vi, examine another file, then use bash to recycle previous (long) vi-launch command.

I rely heavily on the fact that vi remembers the last cursor (and search pattern) per file.

Whenever I run a vi-launch command, I quickly decide on file1 file2 file3 names, picking and shuffling among the hottest files.


[3/4] means vi receives 3 keystrokes; we hit 4 keys including shift or ctrl …

–“split” solution by Deepak M

vi file1 # load 1st file

  • :sp file2 # to show 2nd file upstairs
  • :vsp file3 # to show 2nd file side by side
  • You end up with  — file2 and file3 side by side upstairs, and file1 downstairs!
  • [2/3] ctrl-ww # To move cursor to the “next” file, until it cycles back

–the q( :e ) solution

vi file1 # load 1st file

  • :e file2 # to put 2nd file on foreground
  • [1/3] ctrl-^ — to switch to “the other file”
  • This solution is non-ideal for moving data between files, since you must save active file before switching and you can’t see both files

–editing 3 or more files

  1. vi file1 file2 file3
  2. q(:n) to switch to next, q(:N) for previous…
  3. q(:args) shows all files with current file highlighted
  • –Suppose now you are at file2.
  • q(:e file4) works. q(^) will toggle between file2 and file4
  • However, q(:n :N  :args) only work on the original list, not new files from q(:e)

q(:n :N ^) always shows the current filename in status bar:)

## vi (+less) cheatsheet

Note q[less] not q[more[ can open *.gz files 🙂 has some tricks including how to make vim remember last edit location, but make sure your ‘vi’ command actually runs viM !

  • ~~~~ command mode #roughly ranked
  • [2/3] :↑ (i.e. up-arrow) — cycle through previous :commands
  • [3] dt — “dta” delete until the next “a”
  • [2]: 6x — delete 6 chars
  • [2] 9s — wipe out 9 characters (including current) and enter insert-mode. Better than R when you know how many chars (9) to change
    • to delete 5 characters … there is NO simpler keystroke sequence
  • R — Overwrite each character one by one until end of line. Useful if the replacement content is similar to original?
  • Ctrl-R to re-do
  • cw — wipe out from cursor to end of word and puts you into insert mode
    • c2w or 2cw
  • :se list (or nolist) to reveal invisible chars
  • C — wipe out from cursor to END of line and puts you into insert-mode
  • capital O — open new line above cursor
  • A — to append at END of current line
  • :sy off  — get rid of color scheme
  • :syntax off # is the long form
  • from inside q(LESS), type a single “v” to launch vi

[3/4] means vi receives 3 keystrokes; we hit 4 keys including shift or ctrl …

–paging commands in vi and less

  • jump to end of file: capital G == in both vi and LESS
  • jump to head of file: 1G == in both vi and LESS
  • page dn: Ctrl-f == in both; LESS also uses space
  • page up: Ctrl-b == in both; LESS also uses b


## tips: search in q[less]

  • after you have searched for “needle1”, how do you expand on the pattern? You can hit 2 keys
    • [2]  /↑ (i.e. <upArrow>) to load “needle1.” Now you an edit it or add an alternative like
    • [2+]/↑ (i.e. <upArrow>) |needled2|needle3
  • In a q(less) screen, You can highlight not only the same AAA occurrences, but AAA or BBB. I used


  • [2/3] /ctrl-R — search without metacharacters
  • -i, then /needle — case insensitive

[3/4] means vi/less receives 3 keystrokes; we hit 4 keys including shift or ctrl …

–search can fail with super-long lines. See also

  • Symptom — if you navigate to the correct region of the big text file, then the search does hit the target word.
  • Solution — use grep to confirm


bookmarking in vi

— based on

Any line can be “Book Marked” for a quick cursor return. Type the letter “m” and any other letter to identify the line. This “marked” line can be referenced by the keystroke sequence “‘” and the identifying letter.

Example: “mt” will mark a line by the identifier “t”. “‘t” will return the cursor to this line at any time. I prefer mm and ‘m

A block of text may be referred to by its marked lines. i.e.’t,’b