VIM: Jump Back To Previous or Last Cursor Position

Writing shell scripts in vim I usually go search for functions or something. Copy lines and then go back to where I was and modify the code. However, I need to manually navigate way back to the previous / last position. How can I automatically go back to where I was when I started my search operation under vim?

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time N/A
Vim is a text editor that is upwards compatible to Vi. It supports the following markers and moving about keys: [a] '. : Jump to last modification line. [b] `. : Jump to exact spot in last modification line [c] CTRL-O : Retrace your movements in file in backwards. [d] CTRL-I : Retrace your movements in file in forwards.
From the vim help files:

Jumps are remembered in a jump list. With the CTRL-O and CTRL-I command you can go to cursor positions before older jumps, and back again. Thus you can move up and down the list. There is a separate jump list for each window. The maximum number of entries is fixed at 100.

Example

Open a text file called functions.bash, run:
$ vim functions.bash
Move cursor or go to line # 300 by typing the following vim command
300 shift g
Search for a shell function called _addNfsClientUser
/_addNfsClientUser
To jump back to line # 300 or previous position press CTRL-O (press and hold Ctrl key and press letter O). To jump back forwards press CTRL-I (press and hold Ctrl key and press letter I).

See also

Type the following command to get helpful info about jumping:
:help jumplist


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Allen Sep 9, 2013 @ 15:22

    Great! Thanks!
    I have looking for these 2 short cuts for a while and this is what I need!

  • Profpatsch Apr 5, 2014 @ 14:08

    Thanks, I always forget these.

  • Sérgio Araújo Sep 15, 2020 @ 19:40

    ” move to the last change
    ” once gi moves to the last insert and gv last visual

    nnoremap gl `.

    ” The mnemonics is ‘go to the last change point’

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum