You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code终端光标位置异常问题(Zsh+Agnoster主题)

Fixing Long Cursor Position in VS Code Terminal with Zsh Agnoster Theme

Hey there! I’ve dealt with this exact frustration before—Agnoster’s default prompt is packed with useful info, but it can hog way too much space in VS Code’s terminal, shoving your command cursor way to the right (or even wrapping to a new line). Let’s fix this with some simple tweaks to the theme and VS Code settings.

Agnoster shows things like your username, hostname, full directory path, and detailed git status by default. Most of these aren’t necessary for local development, so we can hide or shorten them directly in your ~/.zshrc file (no need to edit the original theme file, which gets overwritten when updating Oh My Zsh).

Add these lines to your ~/.zshrc:

# Hide the username@hostname section (useless for local work)
prompt_context() {}

# Shorten directory path to only the last 2 folders (adjust number as needed)
prompt_dir() {
  prompt_segment blue $CURRENT_FG '%2~'
}

# Simplify git status to just branch name + dirty indicator
prompt_git() {
  local ref dirty
  if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
    dirty=$(parse_git_dirty)
    ref=$(git symbolic-ref HEAD 2>/dev/null) || ref=$(git rev-parse --short HEAD 2>/dev/null) || return 0

    prompt_segment green $CURRENT_FG
    echo -n "${ref#refs/heads/}$dirty"
  fi
}

What each tweak does:

  • prompt_context() {}: Completely removes the user@hostname segment from your prompt.
  • %2~: Changes the directory display to show only the last 2 folders (replace 2 with 1 for just the current folder, or 3 if you want a bit more context).
  • Simplified prompt_git: Strips extra git symbols and only shows your branch name plus a * if there are uncommitted changes.

2. Use a Compact Nerd Font

Agnoster relies on icon fonts, so switching to a compact Nerd Font can save horizontal space while keeping all the theme’s icons intact. Here’s how to set it up in VS Code:

  1. Install a compact Nerd Font like Fira Code Nerd Font (it’s popular and has great icon support).
  2. Open VS Code’s settings (Ctrl+, or Cmd+,), search for Terminal > Integrated: Font Family, and set it to:
    Fira Code Nerd Font
    
  3. Adjust the font size slightly (e.g., 12 or 11) if you still need more space.

3. Apply Changes & Test

  1. Save your ~/.zshrc file and run this command in your terminal to load the new config:
    source ~/.zshrc
    
  2. Restart VS Code to make sure the terminal picks up the new font and prompt settings.

If you ever need to see the full directory path temporarily, just run pwd—no need to revert the prompt changes.

Give these steps a shot, and your cursor should be back in a nice, usable position while keeping all the parts of Agnoster that make it great.

内容的提问来源于stack exchange,提问作者Johnmark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:33:31