I am running Ubuntu 19.10, and I am trying to alias the command "vv" to open the last edited file in Vim. Below is my results from running the command 'alias'.

~/Documents/code$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias cls='clear'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias sl='ls --color=auto'
alias vv='!vi'

I receive the error:

~/Documents/code$ vv
Command '!vi' not found, but there are 15 similar ones.

!v and !vi are already working commands to open the last edited file in Vim, but I want vv to also be a command to do the same function, because sometimes I'm lazy.

Things I've tried:

  • Updating ~/.bashrc with new alias
  • Restarting computer
  • Open new terminal

Any help would be appreciated, thank you.


This happens because history expansion is performed before alias expansion.

You can force history expansion in your alias by using the history command, using command substitution to replace a command that queries the history with its output:

alias vv='$(history -p !vim)'

but this will not handle filenames correctly if they contain characters that might trigger an expansion, such as spaces. You can avoid this using eval:

alias vv='eval $(history -p !vim)'

Use this:

alias vv="vim -c 'norm! ^O'"

norm execute normal mode command ^O as if you type it. See :help :normal for more information.

^O is one character, you can input it in insert mode by typing <C-V><C-O>. It brings you to last cursor position, which is in the last file you edited.

So the alias above is the same as opening vim and press ctrl-o.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.